import { replacementTimelineAssetId } from "@hyperframes/core/timeline-asset-id"; import { parseHTML } from "@hyperframes/parsers/linkedom"; import { removeElementWithGsapCascade } from "@hyperframes/parsers"; import { readMediaOffsetSeconds, readPlaybackRate } from "@hyperframes/parsers/media-duration"; import postcss from "postcss"; import { parseCss } from "@hyperframes/parsers/postcss"; import selectorParser from "postcss-selector-parser"; import { escapeCssIdentifier } from "@hyperframes/core/compiler/selector-id-tokens"; import { isAllowedHtmlAttribute, isSafeAttributeValue } from "@hyperframes/core/html-attr-safety"; import { sanitizeRichTextChildren } from "@hyperframes/core/rich-text-sanitize"; import { assignHfIds, ensureHfIds, isNonContentElement, mintHfId, walkCompositionDescendants, } from "@hyperframes/parsers/hf-ids"; import { readClipTiming, SPLIT_BOUNDARY_EPSILON_S, writeClipTiming, type ReferenceExpression, } from "@hyperframes/core/composition-contract"; import { relinkSplitHalves } from "@hyperframes/core/media-link"; import { parseStyleDecls, patchStyleAttrString } from "./sourceStyleMutation.js"; import { isHtmlTemplate } from "@hyperframes/parsers/sub-composition-validity"; export interface SourceMutationTarget { id?: string | null; hfId?: string; selector?: string; selectorIndex?: number; } const pinnedInSource = new WeakMap>(); export function parseSourceDocument(raw: string): { document: Document; wrappedFragment: boolean; } { const source = ensureHfIds(raw); const hasDocumentShell = /]/i.test(source); const document = hasDocumentShell ? parseHTML(source).document : parseHTML(`${source}`).document; pinnedInSource.set( document, new Set(Array.from(raw.matchAll(/\bdata-hf-id\s*=\s*["']?([^"'\s>]+)/gi), (m) => m[1]!)), ); return { document, wrappedFragment: !hasDocumentShell }; } export function serializeSourceDocument(page: { document: Document; wrappedFragment: boolean; }): string { return page.wrappedFragment ? page.document.body.innerHTML || "" : page.document.toString(); } /** New elements get their data-hf-id, and the page's ids count as in its source, as if parsed again. */ function settleSourceDocument(document: Document): void { if (document.body) assignHfIds(document.body); const ids = querySelectorAllWithTemplates(document, "[data-hf-id]").map( (el) => el.getAttribute("data-hf-id") ?? "", ); pinnedInSource.set(document, new Set(ids)); } /** Drops the rules that style `id` alone (every selector in the list names it), in each style sheet of the page. */ function dropCssRulesForId(document: Document, id: string): void { const idToken = `#${id}`; const namesId = (selector: string) => { let found = false; selectorParser((selectors) => { selectors.walkIds((node) => { // an id inside :not() excludes the element, so the rule styles the others let up = node.parent; while (up && !(up.type === "pseudo" && up.value === ":not")) up = up.parent as typeof up; if (node.value === id && !up) found = true; }); }).processSync(selector); return found; }; for (const styleEl of document.querySelectorAll("style")) { const css = styleEl.textContent ?? ""; if (!css.includes(idToken)) continue; let root: postcss.Root; try { root = parseCss(css); } catch { continue; } let dropped = false; root.walkRules((rule) => { if (rule.selectors.length > 0 && rule.selectors.every(namesId)) { rule.remove(); dropped = true; } }); if (dropped) styleEl.textContent = root.toString(); } } // A clone's rules replace whatever an earlier piece of that id left behind, so repeated cuts keep one set per id. function duplicateCssRulesForId(document: Document, originalId: string, newId: string): void { dropCssRulesForId(document, newId); const idToken = `#${originalId}`; const transform = selectorParser((selectors) => { selectors.walkIds((node) => { if (node.value === originalId) node.value = escapeCssIdentifier(newId); }); }); for (const styleEl of document.querySelectorAll("style")) { const css = styleEl.textContent ?? ""; let root: postcss.Root; try { root = parseCss(css); } catch { continue; } const clones: postcss.Rule[] = []; root.walkRules((rule) => { if (!rule.selector.includes(idToken)) return; const newSelector = transform.processSync(rule.selector); if (newSelector === rule.selector) return; const clone = rule.clone({ selector: newSelector }); clones.push(clone); }); if (clones.length > 0) { for (const c of clones) root.append(c); styleEl.textContent = root.toString(); } } } function querySelectorAllWithTemplates(root: Document | Element, selector: string): Element[] { const matches = Array.from(root.querySelectorAll(selector)); if (matches.length > 0) return matches; // querySelectorAll doesn't traverse