astro-ghostcms/.pnpm-store/v3/files/c8/58d26b2f17fb1eb1fad5f4300e5...

8 lines
7.9 KiB
Plaintext

{
"version": 3,
"sources": ["../../src/transformers/sanitize.ts"],
"sourcesContent": ["import { ElementNode, ELEMENT_NODE, Node, walkSync } from \"../index.js\";\n\nexport interface SanitizeOptions {\n /** An Array of strings indicating elements that the sanitizer should not remove. All elements not in the array will be dropped. */\n allowElements?: string[];\n /** An Array of strings indicating elements that the sanitizer should remove, but keeping their child elements. */\n blockElements?: string[];\n /** An Array of strings indicating elements (including nested elements) that the sanitizer should remove. */\n dropElements?: string[];\n /** An Object where each key is the attribute name and the value is an Array of allowed tag names. Matching attributes will not be removed. All attributes that are not in the array will be dropped. */\n allowAttributes?: Record<string, string[]>;\n /** An Object where each key is the attribute name and the value is an Array of dropped tag names. Matching attributes will be removed. */\n dropAttributes?: Record<string, string[]>;\n /** A Boolean value set to false (default) to remove components and their children. If set to true, components will be subject to built-in and custom configuration checks (and will be retained or dropped based on those checks). */\n allowComponents?: boolean;\n /** A Boolean value set to false (default) to remove custom elements and their children. If set to true, custom elements will be subject to built-in and custom configuration checks (and will be retained or dropped based on those checks). */\n allowCustomElements?: boolean;\n /** A Boolean value set to false (default) to remove HTML comments. Set to true in order to keep comments. */\n allowComments?: boolean;\n}\n\nfunction resolveSantizeOptions(\n sanitize?: SanitizeOptions\n): Required<SanitizeOptions> {\n if (sanitize === undefined) {\n return {\n allowElements: [] as string[],\n dropElements: [\"script\"],\n allowComponents: false,\n allowCustomElements: false,\n allowComments: false,\n } as Required<SanitizeOptions>;\n } else {\n const dropElements = new Set<string>([]);\n if (!sanitize.allowElements?.includes(\"script\")) {\n dropElements.add(\"script\");\n }\n for (const dropElement of sanitize.dropElements ?? []) {\n dropElements.add(dropElement);\n }\n return {\n allowComponents: false,\n allowCustomElements: false,\n allowComments: false,\n ...sanitize,\n dropElements: Array.from(dropElements),\n } as Required<SanitizeOptions>;\n }\n}\n\ntype NodeKind = \"element\" | \"component\" | \"custom-element\";\nfunction getNodeKind(node: ElementNode): NodeKind {\n if (node.name.includes(\"-\")) return \"custom-element\";\n if (/[\\_\\$A-Z]/.test(node.name[0]) || node.name.includes(\".\"))\n return \"component\";\n return \"element\";\n}\n\ntype ActionType = \"allow\" | \"drop\" | \"block\";\nfunction getAction(\n name: string,\n kind: NodeKind,\n sanitize: Required<SanitizeOptions>\n): ActionType {\n if (sanitize.allowElements?.length > 0) {\n if (sanitize.allowElements.includes(name)) return \"allow\";\n }\n if (sanitize.blockElements?.length > 0) {\n if (sanitize.blockElements.includes(name)) return \"block\";\n }\n if (sanitize.dropElements?.length > 0) {\n if (sanitize.dropElements.find((n) => n === name)) return \"drop\";\n }\n if (kind === \"component\" && !sanitize.allowComponents) return \"drop\";\n if (kind === \"custom-element\" && !sanitize.allowCustomElements) return \"drop\";\n\n return (sanitize.allowElements?.length > 0) ? 'drop' : 'allow';\n}\n\nfunction sanitizeAttributes(\n node: ElementNode,\n sanitize: Required<SanitizeOptions>\n): Record<string, string> {\n const attrs: Record<string, string> = node.attributes;\n for (const key of Object.keys(node.attributes)) {\n if (\n (sanitize.allowAttributes?.[key] &&\n sanitize.allowAttributes?.[key].includes(node.name)) ||\n sanitize.allowAttributes?.[key]?.includes(\"*\")\n ) {\n continue;\n }\n if (\n (sanitize.dropAttributes?.[key] &&\n sanitize.dropAttributes?.[key].includes(node.name)) ||\n sanitize.dropAttributes?.[key]?.includes(\"*\")\n ) {\n delete attrs[key];\n }\n }\n return attrs;\n}\n\nfunction sanitizeElement(\n opts: Required<SanitizeOptions>,\n node: ElementNode,\n parent: Node\n) {\n const kind = getNodeKind(node);\n const { name } = node;\n const action = getAction(name, kind, opts);\n if (action === \"drop\")\n return () => {\n parent!.children = parent!.children.filter(\n (child: Node) => child !== node\n );\n };\n if (action === \"block\")\n return () => {\n parent!.children = parent!.children\n .map((child: Node) => (child === node ? child.children : child))\n .flat(1);\n };\n\n return () => {\n node.attributes = sanitizeAttributes(node, opts);\n };\n}\n\nexport default function sanitize(opts?: SanitizeOptions) {\n const sanitize = resolveSantizeOptions(opts);\n return (doc: Node): Node => {\n let actions: any[] = [];\n walkSync(doc, (node: Node, parent?: Node) => {\n switch (node.type) {\n case ELEMENT_NODE: {\n actions.push(sanitizeElement(sanitize, node, parent!));\n return;\n }\n default: return;\n }\n });\n for (const action of actions) {\n action();\n }\n return doc;\n };\n}\n"],
"mappings": "AAAA,OAAsB,gBAAAA,EAAoB,YAAAC,MAAgB,cAqB1D,SAASC,EACPC,EAC2B,CAvB7B,IAAAC,EAwBE,GAAID,IAAa,OACf,MAAO,CACL,cAAe,CAAC,EAChB,aAAc,CAAC,QAAQ,EACvB,gBAAiB,GACjB,oBAAqB,GACrB,cAAe,EACjB,EACK,CACL,IAAME,EAAe,IAAI,IAAY,CAAC,CAAC,GAClCD,EAAAD,EAAS,gBAAT,MAAAC,EAAwB,SAAS,WACpCC,EAAa,IAAI,QAAQ,EAE3B,QAAWC,KAAeH,EAAS,cAAgB,CAAC,EAClDE,EAAa,IAAIC,CAAW,EAE9B,MAAO,CACL,gBAAiB,GACjB,oBAAqB,GACrB,cAAe,GACf,GAAGH,EACH,aAAc,MAAM,KAAKE,CAAY,CACvC,CACF,CACF,CAGA,SAASE,EAAYC,EAA6B,CAChD,OAAIA,EAAK,KAAK,SAAS,GAAG,EAAU,iBAChC,YAAY,KAAKA,EAAK,KAAK,EAAE,GAAKA,EAAK,KAAK,SAAS,GAAG,EACnD,YACF,SACT,CAGA,SAASC,EACPC,EACAC,EACAR,EACY,CA/Dd,IAAAC,EAAAQ,EAAAC,EAAAC,EAgEE,QAAIV,EAAAD,EAAS,gBAAT,YAAAC,EAAwB,QAAS,GAC/BD,EAAS,cAAc,SAASO,CAAI,EAAU,UAEhDE,EAAAT,EAAS,gBAAT,YAAAS,EAAwB,QAAS,GAC/BT,EAAS,cAAc,SAASO,CAAI,EAAU,UAEhDG,EAAAV,EAAS,eAAT,YAAAU,EAAuB,QAAS,GAC9BV,EAAS,aAAa,KA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D,IAAMrB,EAAWD,EAAsBsB,CAAI,EAC3C,OAAQI,GAAoB,CAC1B,IAAIC,EAAiB,CAAC,EACtB5B,EAAS2B,EAAK,CAACpB,EAAYiB,IAAkB,CAC3C,OAAQjB,EAAK,WACNR,EAAc,CACf6B,EAAQ,KAAKN,EAAgBpB,EAAUK,EAAMiB,CAAO,CAAC,EACrD,MACJ,SACS,OAEb,CAAC,EACD,QAAWC,KAAUG,EACjBH,EAAO,EAEX,OAAOE,CACT,CACF",
"names": ["ELEMENT_NODE", "walkSync", "resolveSantizeOptions", "sanitize", "_a", "dropElements", "dropElement", "getNodeKind", "node", "getAction", "name", "kind", "_b", "_c", "_d", "n", "sanitizeAttributes", "_e", "_f", "_g", "_h", "attrs", "key", "sanitizeElement", "opts", "parent", "action", "child", "doc", "actions"]
}