# All files / src/html scriptAndStyleTagProcessor.ts

20% Statements 2/10
0% Branches 0/15
0% Functions 0/1
20% Lines 2/10

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
    28
    299x

    9x

     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[] | undefined) {
      // 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');
      // Do not process script/style tags that are from External
      const isExternal = userScriptsAndStyles === undefined;
      Iif (isHeadOrBottom || isExternal) {
        return;
      }

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