import { parseHTMLContent } from "@hyperframes/core/compiler/html-document";
import { markupOutsideAllowlist } from "@hyperframes/core/html-attr-safety";
import { EXCLUDED_TAGS, mintHfId, walkCompositionDescendants } from "@hyperframes/parsers/hf-ids";
import { knownCompositionFile } from "../components/editor/domEditingDom";
const REFUSED = "Couldn't paste: the copied element holds markup that can't be pasted here.";
export function copiedFile(
claimed: string | undefined,
activeCompositionPath: string | null,
): string {
const file = knownCompositionFile(claimed, activeCompositionPath);
if (file) return file;
throw new Error("Couldn't use this element: its file isn't one of this project's compositions.");
}
export interface PreparedCopy {
html: string;
root: Element;
originalHfId: string | null;
renames: Map;
}
function fragment(html: string): HTMLTemplateElement {
const template = document.createElement("template");
template.innerHTML = html;
return template;
}
/** Fresh data-hf-ids minted in the edit (else the server stamps them behind undo's back); taken ids renamed. */
export function prepareCopy(
html: string,
takenHfIds: Set,
takenIds: Set,
place?: (root: Element) => void,
): PreparedCopy {
const root = fragment(html).content.firstElementChild;
if (!root) throw new Error(REFUSED);
const originalHfId = root.getAttribute("data-hf-id");
const elements = [root];
walkCompositionDescendants(root, (el) => elements.push(el));
for (const el of elements) {
if (EXCLUDED_TAGS.has(el.localName)) el.removeAttribute("data-hf-id");
else el.setAttribute("data-hf-id", mintHfId(el, takenHfIds));
}
const renames = new Map();
for (const el of [root, ...Array.from(root.querySelectorAll("[id]"))]) {
const id = el.getAttribute("id");
if (!id) continue;
if (takenIds.has(id)) {
let counter = 2;
while (takenIds.has(`${id}-${counter}`)) counter++;
el.setAttribute("id", `${id}-${counter}`);
renames.set(id, `${id}-${counter}`);
}
takenIds.add(el.getAttribute("id")!);
}
place?.(root);
return { html: root.outerHTML, root, originalHfId, renames };
}
const parsed = (html: string) => new DOMParser().parseFromString(html, "text/html");
// The page as served: the preview server and render compiler re-read a file with linkedom before a browser does.
const served = (html: string) => parsed(parseHTMLContent(html).toString());
const htmlTemplate = (node: Node): node is HTMLTemplateElement =>
node instanceof Element && node.localName === "template" && node.namespaceURI === HTML_NS;
const HTML_NS = "http://www.w3.org/1999/xhtml";
function eachElement(nodes: ParentNode, visit: (el: Element) => void): void {
for (const el of Array.from(nodes.querySelectorAll("*"))) {
visit(el);
if (htmlTemplate(el)) eachElement(el.content, visit);
}
}
const signature = (el: Element) =>
JSON.stringify([
el.namespaceURI,
el.localName,
Array.from(el.attributes, (a) => JSON.stringify([a.namespaceURI, a.name, a.value])).sort(),
]);
function subtree(root: Element): Element[] {
const elements = [root];
eachElement(root, (el) => elements.push(el));
return elements;
}
// Raw