← Files RivetARCHIVED FILE

node_modules/rivet-design/dist/utils/htmlDocument.js

1.97 KB · Nov 7, 2026 · 13:16 UTC

↓ Download file

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertHtmlHeadElements = void 0;
const parse5_1 = require("parse5");
const insertHtmlHeadElements = (source, elements) => {
    if (elements.length === 0)
        return source;
    const document = (0, parse5_1.parse)(source, { sourceCodeLocationInfo: true });
    const missingElements = new Set(elements);
    const pending = [document];
    while (pending.length > 0) {
        const node = pending.pop();
        if (!node)
            break;
        if (parse5_1.defaultTreeAdapter.isElementNode(node) &&
            node.namespaceURI === parse5_1.html.NS.HTML) {
            for (const element of missingElements) {
                if (node.tagName === element.tagName &&
                    (element.id === undefined ||
                        node.attrs.some((attr) => attr.name === 'id' && attr.value === element.id))) {
                    missingElements.delete(element);
                }
            }
            if (missingElements.size === 0)
                return source;
        }
        if ('childNodes' in node) {
            pending.push(...node.childNodes);
        }
    }
    const root = document.childNodes
        .filter(parse5_1.defaultTreeAdapter.isElementNode)
        .find((node) => node.tagName === 'html');
    const head = root?.childNodes
        .filter(parse5_1.defaultTreeAdapter.isElementNode)
        .find((node) => node.tagName === 'head');
    const doctype = document.childNodes.find(parse5_1.defaultTreeAdapter.isDocumentTypeNode);
    const insertAt = head?.sourceCodeLocation?.startTag?.endOffset ??
        root?.sourceCodeLocation?.startTag?.endOffset ??
        doctype?.sourceCodeLocation?.endOffset ??
        0;
    const markup = Array.from(missingElements, (element) => element.markup).join('');
    return source.slice(0, insertAt) + markup + source.slice(insertAt);
};
exports.insertHtmlHeadElements = insertHtmlHeadElements;
//# sourceMappingURL=htmlDocument.js.map

SHA-256: 6905451302f467374544bee50cf3b6ab6291edb384118ad7f402b7a9e0b25c80