All files / processors script.tsx

93.75% Statements 15/16
85.71% Branches 12/14
100% Functions 3/3
100% Lines 14/14

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  3x                       3x             3x   10x         5x   5x 1x 1x     5x 2x 2x     5x   5x       3x  
import { Processor, Element, Text } from "../types";
import Script from "@frontity/components/script";
 
interface ScriptElement extends Element {
  props: Element["props"] & {
    type?: string;
    src: string;
    code: string;
    "data-src": string;
  };
  children: Text[];
}
 
const validMediaTypes = [
  "application/javascript",
  "text/javascript",
  "application/ecmascript",
  "module",
];
 
const script: Processor<ScriptElement> = {
  test: ({ node }) =>
    node.component === "script" &&
    !("type" in node.props && !validMediaTypes.includes(node.props.type)),
  priority: 20,
  name: "script",
  processor: ({ node }) => {
    Iif (node.parent?.component === "noscript") return node;
 
    if (node.props["data-src"]) {
      node.props.src = node.props["data-src"];
      delete node.props["data-src"];
    }
 
    if (node.children.length > 0) {
      node.props.code = node.children.map((child) => child.content).join("");
      node.children = [];
    }
 
    node.component = Script;
 
    return node;
  },
};
 
export default script;