# All files / src/html scriptAndStyleTagProcessor.ts

22.22% Statements 2/9
0% Branches 0/13
0% Functions 0/1
22.22% Lines 2/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    287x

    7x

     import cheerio from 'cheerio';
    import { DomElement } from 'htmlparser2';

    /**
     * Removes every script and style node (written by the user) encountered in the main app,
     * and records all these script nodes in an array so that they can be hoisted to after
     * <body> tag at a later stage.
     *
     * This is to prevent Vue compilation of script/style tags (as they are not meant to be compiled).
     *
     * @param node from the dom traversal
     * @param userScriptsAndStyles to store scripts and style tags for hoisting
     */
    export function processScriptAndStyleTag(node: DomElement, userScriptsAndStyles: string[]) {
      // Do not process script/style tags that are meant to be inserted in head/bottom of HTML
      const isHeadOrBottom = node.parent && (node.parent.name === 'head-top'
        || node.parent.name === 'head-bottom' || node.parent.name === 'script-bottom');

      Iif (isHeadOrBottom) {
        return;
      }

      const nodeSiblings = node.parent?.children ?? [node];
      nodeSiblings.splice(nodeSiblings.indexOf(node), 1);
      node.parent = undefined;
      userScriptsAndStyles.push(cheerio.html(node as cheerio.Element));
    }