mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
perf(lint): rules scan script, style and comment blocks in linear time (#7770)
* perf(lint): rules scan script, style and comment blocks in linear time Several always-on rules matched open/any/close regexes over the whole document, which rescan to the end from every unclosed opener, so lint time grew with the square of the page on pages of unclosed <script, <style, <pre or /* openers. Those scans are now forward passes in lint/src/linearScans.ts that return exactly what the regexes did: block-comment stripping, the malformed </script check, the style-tag balance count, the visible-comment rule's protected blocks and tag tracking, the structural line count, the script-src check, and the slideshow island lookup. A fuzz test compares each scan with its regex on 20,000 generated pages, and 4 MB pages of unclosed openers finish in milliseconds. On a 140 KB page of unclosed <script> openers lintHyperframeHtml drops from about 2.6 s to 45 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(lint): compare each fuzz page in one assertion so the run fits the default timeout Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(player): raise the packed size budget for the bundled slideshow island scan Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
06cf964646
commit
12832e3a98
@@ -0,0 +1,156 @@
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
blockComments,
|
||||
emptyStyleBlocks,
|
||||
hasInvalidScriptClose,
|
||||
hasScriptWithSrc,
|
||||
htmlTagTracker,
|
||||
protectedBlockRanges,
|
||||
replaceBlockComments,
|
||||
styleTagTokens,
|
||||
} from "./linearScans";
|
||||
|
||||
const PATTERNS = {
|
||||
blockComment: /\/\*[\s\S]*?\*\//g,
|
||||
invalidScriptClose: /<script[^>]*>[\s\S]*?<\s*\/\s*script(?!>)/i,
|
||||
scriptWithSrc: /<script\b[^>]*\bsrc\s*=/i,
|
||||
styleBlock: /<style\b[^>]*>[\s\S]*?<\/style>/gi,
|
||||
styleTokens: /<script\b[\s\S]*?<\/script[^>]*>|<style\b|<\/style\s*>/gi,
|
||||
protectedBlock:
|
||||
/<(style|script|template|title|noscript|pre|code|textarea|text)\b[^>]*>[\s\S]*?<\/\1(?:\s[^>]*)?>/gi,
|
||||
};
|
||||
|
||||
function insideHtmlTag(source: string, index: number): boolean {
|
||||
let inTag = false;
|
||||
let quote: string | null = null;
|
||||
for (let i = 0; i < index; i++) {
|
||||
const char = source[i];
|
||||
if (!inTag) {
|
||||
if (char === "<") inTag = true;
|
||||
continue;
|
||||
}
|
||||
if (quote) {
|
||||
if (char === quote) quote = null;
|
||||
continue;
|
||||
}
|
||||
if (char === '"' || char === "'") quote = char;
|
||||
else if (char === ">") inTag = false;
|
||||
}
|
||||
return inTag;
|
||||
}
|
||||
|
||||
const TOKENS = [
|
||||
"<script",
|
||||
"<SCRIPT ",
|
||||
"<script>",
|
||||
"</script>",
|
||||
"</script >",
|
||||
"</scripts",
|
||||
"< / script",
|
||||
"<style",
|
||||
"<STYLE x>",
|
||||
"</style>",
|
||||
"</style >",
|
||||
"</STYLE\n>",
|
||||
"<template>",
|
||||
"</template x>",
|
||||
"<text>",
|
||||
"</text>",
|
||||
"</textarea>",
|
||||
"<textarea>",
|
||||
"<pre>",
|
||||
"</pre\t>",
|
||||
"</prex>",
|
||||
"<code>",
|
||||
"</code>",
|
||||
"/*",
|
||||
"*/",
|
||||
"/*/",
|
||||
"a",
|
||||
" ",
|
||||
"\n",
|
||||
">",
|
||||
"<",
|
||||
'"',
|
||||
"'",
|
||||
"=",
|
||||
" src=",
|
||||
"xsrc =",
|
||||
"\tSRC\n=",
|
||||
];
|
||||
|
||||
function pages(count: number): string[] {
|
||||
let seed = 11;
|
||||
const next = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648;
|
||||
return Array.from({ length: count }, () =>
|
||||
Array.from(
|
||||
{ length: 1 + Math.floor(next() * 24) },
|
||||
() => TOKENS[Math.floor(next() * TOKENS.length)],
|
||||
).join(""),
|
||||
);
|
||||
}
|
||||
|
||||
function disagreements(page: string): string[] {
|
||||
const tracked = htmlTagTracker(page);
|
||||
const checks: [string, unknown, unknown][] = [
|
||||
[
|
||||
"blockComments",
|
||||
[...blockComments(page)].map((c) => [c.index, c.text]),
|
||||
[...page.matchAll(PATTERNS.blockComment)].map((m) => [m.index, m[0]]),
|
||||
],
|
||||
[
|
||||
"replaceBlockComments",
|
||||
replaceBlockComments(page, " "),
|
||||
page.replace(PATTERNS.blockComment, " "),
|
||||
],
|
||||
["hasInvalidScriptClose", hasInvalidScriptClose(page), PATTERNS.invalidScriptClose.test(page)],
|
||||
["hasScriptWithSrc", hasScriptWithSrc(page), PATTERNS.scriptWithSrc.test(page)],
|
||||
[
|
||||
"emptyStyleBlocks",
|
||||
emptyStyleBlocks(page),
|
||||
page.replace(PATTERNS.styleBlock, "<style></style>"),
|
||||
],
|
||||
[
|
||||
"styleTagTokens",
|
||||
styleTagTokens(page),
|
||||
[...page.matchAll(PATTERNS.styleTokens)].map((m) => m[0]).filter((t) => !/^<script/i.test(t)),
|
||||
],
|
||||
[
|
||||
"protectedBlockRanges",
|
||||
protectedBlockRanges(page),
|
||||
[...page.matchAll(PATTERNS.protectedBlock)].map((m) => ({
|
||||
start: m.index,
|
||||
end: m.index + m[0].length,
|
||||
})),
|
||||
],
|
||||
];
|
||||
for (let i = 0; i <= page.length; i += 3)
|
||||
checks.push([`htmlTagTracker@${i}`, tracked(i), insideHtmlTag(page, i)]);
|
||||
return checks
|
||||
.filter(([, actual, expected]) => !isDeepStrictEqual(actual, expected))
|
||||
.map(([name]) => `${name}: ${JSON.stringify(page)}`);
|
||||
}
|
||||
|
||||
describe("linear scans", () => {
|
||||
it("answer every page exactly as the regexes they replace", () => {
|
||||
expect(pages(20_000).flatMap(disagreements)).toEqual([]);
|
||||
});
|
||||
|
||||
// At 4 MB each regex above runs for minutes or more on these pages; the scans take milliseconds.
|
||||
it.each(["<script ", "<script>", "<style ", "<style>", "/*", "<pre>", "<text ", "< / script"])(
|
||||
"finish a 4 MB page of unclosed %j in well under a second",
|
||||
(unit) => {
|
||||
const page = unit.repeat(Math.floor(4_000_000 / unit.length));
|
||||
const started = performance.now();
|
||||
replaceBlockComments(page, "");
|
||||
hasInvalidScriptClose(page);
|
||||
emptyStyleBlocks(page);
|
||||
styleTagTokens(page);
|
||||
protectedBlockRanges(page);
|
||||
htmlTagTracker(page)(page.length);
|
||||
hasScriptWithSrc(page);
|
||||
expect(performance.now() - started).toBeLessThan(1000);
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,130 @@
|
||||
// Forward scans that answer what an open/any/close regex answers, without its rescan to the end from every unclosed opener.
|
||||
|
||||
function execFrom(pattern: RegExp, source: string, from: number): RegExpExecArray | null {
|
||||
pattern.lastIndex = from;
|
||||
return pattern.exec(source);
|
||||
}
|
||||
|
||||
/** Every `/* … *\/` match of `/\/\*[\s\S]*?\*\//g`, in order. */
|
||||
export function* blockComments(source: string): Generator<{ index: number; text: string }> {
|
||||
for (let from = 0; ; ) {
|
||||
const start = source.indexOf("/*", from);
|
||||
if (start < 0) return;
|
||||
const close = source.indexOf("*/", start + 2);
|
||||
if (close < 0) return;
|
||||
yield { index: start, text: source.slice(start, close + 2) };
|
||||
from = close + 2;
|
||||
}
|
||||
}
|
||||
|
||||
/** `source.replace(/\/\*[\s\S]*?\*\//g, replacement)`. */
|
||||
export function replaceBlockComments(source: string, replacement: string): string {
|
||||
let out = "";
|
||||
let kept = 0;
|
||||
for (const { index, text } of blockComments(source)) {
|
||||
out += source.slice(kept, index) + replacement;
|
||||
kept = index + text.length;
|
||||
}
|
||||
return out + source.slice(kept);
|
||||
}
|
||||
|
||||
/** `/<script[^>]*>[\s\S]*?<\s*\/\s*script(?!>)/i.test(source)`. */
|
||||
export function hasInvalidScriptClose(source: string): boolean {
|
||||
const open = execFrom(/<script/gi, source, 0);
|
||||
const gt = open ? source.indexOf(">", open.index + open[0].length) : -1;
|
||||
return gt >= 0 && execFrom(/<\s*\/\s*script(?!>)/gi, source, gt + 1) !== null;
|
||||
}
|
||||
|
||||
/** `source.replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, "<style></style>")`. */
|
||||
export function emptyStyleBlocks(source: string): string {
|
||||
let out = "";
|
||||
let kept = 0;
|
||||
const opener = /<style\b/gi;
|
||||
for (let open = execFrom(opener, source, 0); open; open = opener.exec(source)) {
|
||||
const gt = source.indexOf(">", open.index + 6);
|
||||
const close = gt < 0 ? null : execFrom(/<\/style>/gi, source, gt + 1);
|
||||
if (!close) break;
|
||||
out += `${source.slice(kept, open.index)}<style></style>`;
|
||||
kept = close.index + close[0].length;
|
||||
opener.lastIndex = kept;
|
||||
}
|
||||
return out + source.slice(kept);
|
||||
}
|
||||
|
||||
/** The `<style` and `</style>` tokens of `/<script\b[\s\S]*?<\/script[^>]*>|<style\b|<\/style\s*>/gi`, scripts skipped. */
|
||||
export function styleTagTokens(source: string): string[] {
|
||||
const tokens: string[] = [];
|
||||
const token = /<script\b|<style\b|<\/style\s*>/gi;
|
||||
let scriptsClose = true;
|
||||
for (let match = token.exec(source); match; match = token.exec(source)) {
|
||||
if (!/^<script/i.test(match[0])) {
|
||||
tokens.push(match[0]);
|
||||
continue;
|
||||
}
|
||||
if (!scriptsClose) continue;
|
||||
const close = execFrom(/<\/script/gi, source, match.index + 7);
|
||||
const gt = close ? source.indexOf(">", close.index + 8) : -1;
|
||||
if (gt < 0) scriptsClose = false;
|
||||
else token.lastIndex = gt + 1;
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
const PROTECTED_NAMES = "style|script|template|title|noscript|pre|code|textarea|text";
|
||||
|
||||
/** The spans of `/<(style|script|template|title|noscript|pre|code|textarea|text)\b[^>]*>[\s\S]*?<\/\1(?:\s[^>]*)?>/gi`. */
|
||||
export function protectedBlockRanges(source: string): Array<{ start: number; end: number }> {
|
||||
const ranges: Array<{ start: number; end: number }> = [];
|
||||
const unclosed = new Set<string>();
|
||||
const lastGt = source.lastIndexOf(">");
|
||||
const opener = new RegExp(`<(${PROTECTED_NAMES})\\b`, "gi");
|
||||
for (let open = opener.exec(source); open; open = opener.exec(source)) {
|
||||
const name = open[1]!.toLowerCase();
|
||||
if (unclosed.has(name)) continue;
|
||||
const gt = source.indexOf(">", open.index + open[0].length);
|
||||
if (gt < 0) break;
|
||||
const end = protectedBlockEnd(source, name, gt + 1, lastGt);
|
||||
if (end < 0) {
|
||||
unclosed.add(name);
|
||||
continue;
|
||||
}
|
||||
ranges.push({ start: open.index, end });
|
||||
opener.lastIndex = end;
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
function protectedBlockEnd(source: string, name: string, from: number, lastGt: number): number {
|
||||
const closer = new RegExp(`</${name}`, "gi");
|
||||
for (let close = execFrom(closer, source, from); close; close = closer.exec(source)) {
|
||||
const after = close.index + close[0].length;
|
||||
if (after > lastGt) return -1;
|
||||
if (source[after] === ">") return after + 1;
|
||||
if (/\s/.test(source[after] ?? "")) return source.indexOf(">", after) + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** Whether `index` falls inside a start or end tag, read as one forward pass over increasing indexes. */
|
||||
export function htmlTagTracker(source: string): (index: number) => boolean {
|
||||
let i = 0;
|
||||
let inTag = false;
|
||||
let quote: string | null = null;
|
||||
return (index) => {
|
||||
for (; i < index; i++) {
|
||||
const char = source[i];
|
||||
if (!inTag) inTag = char === "<";
|
||||
else if (quote) quote = char === quote ? null : quote;
|
||||
else if (char === '"' || char === "'") quote = char;
|
||||
else if (char === ">") inTag = false;
|
||||
}
|
||||
return inTag;
|
||||
};
|
||||
}
|
||||
|
||||
/** `/<script\b[^>]*\bsrc\s*=/i.test(source)`, reading each start tag once. */
|
||||
export function hasScriptWithSrc(source: string): boolean {
|
||||
for (const [tag] of source.matchAll(/<script\b[^>]*>?/gi))
|
||||
if (/\bsrc\s*=/i.test(tag)) return true;
|
||||
return false;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { emptyStyleBlocks, hasScriptWithSrc, replaceBlockComments } from "../linearScans";
|
||||
import type { LintContext, HyperframeLintFinding, ExtractedBlock, OpenTag } from "../context";
|
||||
import selectorParser from "postcss-selector-parser";
|
||||
import { parseHTML } from "linkedom";
|
||||
@@ -96,7 +97,7 @@ function countPhysicalLines(source: string): number {
|
||||
}
|
||||
|
||||
function countStructuralLines(source: string): number {
|
||||
return countPhysicalLines(source.replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, "<style></style>"));
|
||||
return countPhysicalLines(emptyStyleBlocks(source));
|
||||
}
|
||||
|
||||
function isCaptionCue(tag: OpenTag): boolean {
|
||||
@@ -131,7 +132,7 @@ function isCompositionRootOrMount(rawTag: string): boolean {
|
||||
// rules that consume this — the helper just yields raw URL values.
|
||||
function extractCssUrlReferences(css: string): string[] {
|
||||
const out: string[] = [];
|
||||
const noComments = css.replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
const noComments = replaceBlockComments(css, "");
|
||||
const urlPattern = /\burl\(\s*(["']?)([^)"']+)\1\s*\)/g;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = urlPattern.exec(noComments)) !== null) {
|
||||
@@ -166,7 +167,7 @@ function climbsAboveRoot(fileDepth: number, path: string): boolean {
|
||||
// full CSS parser, and rules elsewhere in this file scan CSS the same way.
|
||||
function extractCssSelectors(css: string): string[] {
|
||||
const out: string[] = [];
|
||||
const noComments = css.replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
const noComments = replaceBlockComments(css, "");
|
||||
const ruleHeader = /(?<![^{}])([^{}]+)\{/g;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = ruleHeader.exec(noComments)) !== null) {
|
||||
@@ -208,7 +209,7 @@ function collectHeavyOverlayHooks(styles: ExtractedBlock[]): {
|
||||
const classes = new Set<string>();
|
||||
const ids = new Set<string>();
|
||||
for (const style of styles) {
|
||||
const noComments = style.content.replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
const noComments = replaceBlockComments(style.content, "");
|
||||
const ruleWithBody = /(?<![^{}])([^{}]+)\{([^{}]*)\}/g;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = ruleWithBody.exec(noComments)) !== null) {
|
||||
@@ -1259,7 +1260,7 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding
|
||||
if (!rootCompositionId || !rootTag) return [];
|
||||
// Can't scan external script files for timeline registration; skip to avoid
|
||||
// false positives on compositions that register via a bundled JS file.
|
||||
if (/<script\b[^>]*\bsrc\s*=/i.test(rawSource)) return [];
|
||||
if (hasScriptWithSrc(rawSource)) return [];
|
||||
// A computed key may map to any authored id. When static analysis cannot
|
||||
// resolve that key, stay silent rather than claim a registration is absent.
|
||||
if (hasComputedTimelineRegistration(scripts)) return [];
|
||||
@@ -1625,7 +1626,7 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding
|
||||
|
||||
const allCss = styles.map((s) => s.content).join("\n");
|
||||
const allInlineStyles = tags.map((t) => readAttr(t.raw, "style") || "").join("\n");
|
||||
const combinedCss = `${allCss}\n${allInlineStyles}`.replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
const combinedCss = replaceBlockComments(`${allCss}\n${allInlineStyles}`, "");
|
||||
|
||||
const usesLottie =
|
||||
tags.some((t) => readAttr(t.raw, "data-lottie-src") !== null) ||
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
import {
|
||||
blockComments,
|
||||
hasInvalidScriptClose,
|
||||
htmlTagTracker,
|
||||
protectedBlockRanges,
|
||||
styleTagTokens,
|
||||
} from "../linearScans";
|
||||
import type { LintContext, HyperframeLintFinding } from "../context";
|
||||
import postcss from "postcss";
|
||||
import { styleRoot } from "../styleRoots";
|
||||
@@ -22,7 +29,6 @@ import {
|
||||
TIMELINE_REGISTRY_INIT_PATTERN,
|
||||
TIMELINE_REGISTRY_ASSIGN_PATTERN,
|
||||
TIMELINE_REGISTRY_OBJECT_LITERAL_PATTERN,
|
||||
INVALID_SCRIPT_CLOSE_PATTERN,
|
||||
} from "../utils";
|
||||
|
||||
const SCRIPT_SYNTAX_EXEMPT_TYPES = new Set([
|
||||
@@ -354,55 +360,15 @@ function describeStudioElement(tag: { raw: string; name: string }): string {
|
||||
return parts.join("");
|
||||
}
|
||||
|
||||
const VISIBLE_MARKUP_COMMENT_PATTERN = /\/\*[\s\S]*?\*\//g;
|
||||
const VISIBLE_MARKUP_COMMENT_PROTECTED_BLOCK_PATTERN =
|
||||
/<(style|script|template|title|noscript|pre|code|textarea|text)\b[^>]*>[\s\S]*?<\/\1(?:\s[^>]*)?>/gi;
|
||||
|
||||
interface SourceRange {
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
function findProtectedVisibleMarkupRanges(source: string): SourceRange[] {
|
||||
const ranges: SourceRange[] = [];
|
||||
for (const match of source.matchAll(VISIBLE_MARKUP_COMMENT_PROTECTED_BLOCK_PATTERN)) {
|
||||
ranges.push({ start: match.index, end: match.index + match[0].length });
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
function isInsideSourceRange(index: number, ranges: SourceRange[]): boolean {
|
||||
return ranges.some((range) => range.start <= index && index < range.end);
|
||||
}
|
||||
|
||||
function isInsideHtmlTag(source: string, index: number): boolean {
|
||||
let inTag = false;
|
||||
let quote: '"' | "'" | null = null;
|
||||
for (let i = 0; i < index; i++) {
|
||||
const char = source[i];
|
||||
if (!inTag) {
|
||||
if (char === "<") inTag = true;
|
||||
continue;
|
||||
}
|
||||
if (quote) {
|
||||
if (char === quote) quote = null;
|
||||
continue;
|
||||
}
|
||||
if (char === '"' || char === "'") {
|
||||
quote = char;
|
||||
} else if (char === ">") {
|
||||
inTag = false;
|
||||
}
|
||||
}
|
||||
return inTag;
|
||||
}
|
||||
|
||||
function findVisibleMarkupCommentLeak(source: string): string | null {
|
||||
const protectedRanges = findProtectedVisibleMarkupRanges(source);
|
||||
for (const match of source.matchAll(VISIBLE_MARKUP_COMMENT_PATTERN)) {
|
||||
if (isInsideHtmlTag(source, match.index)) continue;
|
||||
if (isInsideSourceRange(match.index, protectedRanges)) continue;
|
||||
return match[0];
|
||||
const protectedRanges = protectedBlockRanges(source);
|
||||
const insideHtmlTag = htmlTagTracker(source);
|
||||
let range = 0;
|
||||
for (const comment of blockComments(source)) {
|
||||
if (insideHtmlTag(comment.index)) continue;
|
||||
while (protectedRanges[range] && protectedRanges[range]!.end <= comment.index) range++;
|
||||
if ((protectedRanges[range]?.start ?? Infinity) <= comment.index) continue;
|
||||
return comment.text;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -577,14 +543,10 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
|
||||
let opens = 0;
|
||||
let closes = 0;
|
||||
let firstTag = "";
|
||||
for (const match of source.matchAll(
|
||||
/<script\b[\s\S]*?<\/script[^>]*>|<style\b|<\/style\s*>/gi,
|
||||
)) {
|
||||
const token = match[0].toLowerCase();
|
||||
if (token.startsWith("<script")) continue;
|
||||
if (token.startsWith("</style")) closes += 1;
|
||||
for (const tag of styleTagTokens(source)) {
|
||||
if (tag.toLowerCase().startsWith("</style")) closes += 1;
|
||||
else opens += 1;
|
||||
if (!firstTag) firstTag = match[0];
|
||||
if (!firstTag) firstTag = tag;
|
||||
}
|
||||
if (opens === closes) return [];
|
||||
return [
|
||||
@@ -830,7 +792,7 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
|
||||
|
||||
// invalid_inline_script_syntax (malformed close tag)
|
||||
({ source }) => {
|
||||
if (!INVALID_SCRIPT_CLOSE_PATTERN.test(source)) return [];
|
||||
if (!hasInvalidScriptClose(source)) return [];
|
||||
return [
|
||||
{
|
||||
code: "invalid_inline_script_syntax",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { replaceBlockComments } from "../linearScans";
|
||||
import postcss from "postcss";
|
||||
import { styleRoot } from "../styleRoots";
|
||||
import { gsapCallOffset, containingCallOffset, gsapRunsWhenScriptRuns } from "../scriptPositions";
|
||||
@@ -239,7 +240,7 @@ function collectStaticCssVariableDefinitions(
|
||||
];
|
||||
const definitionPattern = /(?:^|[;{])\s*(--[A-Za-z0-9_-]+)\s*:/gm;
|
||||
for (const source of sources) {
|
||||
const withoutComments = source.replace(/\/\*[\s\S]*?\*\//g, " ");
|
||||
const withoutComments = replaceBlockComments(source, " ");
|
||||
for (const match of withoutComments.matchAll(definitionPattern)) {
|
||||
if (match[1]) definitions.add(match[1]);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,6 @@ export const TIMELINE_REGISTRY_ASSIGN_PATTERN =
|
||||
// callers only `.test()` this pattern.
|
||||
export const WINDOW_TIMELINE_ASSIGN_PATTERN =
|
||||
/window\.__timelines(?:\[\s*(?:["']([^"']+)["']|[A-Za-z_$][\w$.]*)\s*\]|\.\s*([A-Za-z_$][\w$]*))\s*=\s*([A-Za-z_$][\w$]*)/i;
|
||||
export const INVALID_SCRIPT_CLOSE_PATTERN = /<script[^>]*>[\s\S]*?<\s*\/\s*script(?!>)/i;
|
||||
|
||||
const TIMELINE_REGISTRY_KEY_PATTERN =
|
||||
/window\.__timelines(?:\[\s*["']([^"']+)["']\s*\]|\.\s*([A-Za-z_$][\w$]*))\s*(?:\?\?|\|\||&&)?=/g;
|
||||
|
||||
@@ -45,6 +45,59 @@ describe("parseSlideshowManifest", () => {
|
||||
expect((island + island).replace(slideshowIslandRegex("gi"), "")).toBe("");
|
||||
});
|
||||
|
||||
it("finds the island in every page exactly where the island regex does", () => {
|
||||
const viaRegex = (html: string) => {
|
||||
const raw = slideshowIslandRegex("i").exec(html)?.[1]?.trim();
|
||||
return raw
|
||||
? parseSlideshowManifest(
|
||||
`<script type="application/hyperframes-slideshow+json">${raw}</script>`,
|
||||
)
|
||||
: null;
|
||||
};
|
||||
const outcome = (read: () => unknown) => {
|
||||
try {
|
||||
return { value: read() };
|
||||
} catch (error) {
|
||||
return { error: String(error) };
|
||||
}
|
||||
};
|
||||
const tokens = [
|
||||
"<script",
|
||||
"<SCRIPT ",
|
||||
">",
|
||||
"</script>",
|
||||
"</SCRIPT>",
|
||||
"</script >",
|
||||
' type="application/hyperframes-slideshow+json"',
|
||||
" TYPE = 'APPLICATION/HYPERFRAMES-SLIDESHOW+JSON'",
|
||||
' type="text/javascript"',
|
||||
'{"slides":[{"sceneId":"a"}]}',
|
||||
"{",
|
||||
" ",
|
||||
"\n",
|
||||
"x",
|
||||
];
|
||||
let seed = 3;
|
||||
const next = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648;
|
||||
for (let i = 0; i < 20_000; i++) {
|
||||
const html = Array.from(
|
||||
{ length: 1 + Math.floor(next() * 16) },
|
||||
() => tokens[Math.floor(next() * tokens.length)],
|
||||
).join("");
|
||||
expect(
|
||||
outcome(() => parseSlideshowManifest(html)),
|
||||
html,
|
||||
).toEqual(outcome(() => viaRegex(html)));
|
||||
}
|
||||
});
|
||||
|
||||
it("reads a 4 MB page of unclosed script tags in well under a second", () => {
|
||||
const html = "<script ".repeat(500_000);
|
||||
const started = performance.now();
|
||||
expect(parseSlideshowManifest(html)).toBeNull();
|
||||
expect(performance.now() - started).toBeLessThan(1000);
|
||||
});
|
||||
|
||||
it("throws when slideSequences is present but not an array", () => {
|
||||
const html = `<script type="application/hyperframes-slideshow+json">
|
||||
{ "slides": [{ "sceneId": "a" }], "slideSequences": {} }
|
||||
|
||||
@@ -35,12 +35,30 @@ interface SceneRange {
|
||||
duration: number;
|
||||
}
|
||||
|
||||
const ISLAND_TYPE_ATTR = new RegExp(
|
||||
`type[\\t\\n\\f\\r ]*=[\\t\\n\\f\\r ]*["']${SLIDESHOW_ISLAND_TYPE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}["']`,
|
||||
"i",
|
||||
);
|
||||
|
||||
/** Group 1 of `slideshowIslandRegex("i").exec(html)`, reading each `<script` start tag once instead of backtracking per opener. */
|
||||
function slideshowIslandJson(html: string): string | null {
|
||||
for (const tag of html.matchAll(/<script[^>]*>?/gi)) {
|
||||
if (!tag[0].endsWith(">")) return null;
|
||||
if (!ISLAND_TYPE_ATTR.test(tag[0])) continue;
|
||||
const bodyStart = tag.index + tag[0].length;
|
||||
const close = /<\/script>/gi;
|
||||
close.lastIndex = bodyStart;
|
||||
const end = close.exec(html);
|
||||
return end ? html.slice(bodyStart, end.index) : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Extract the JSON island from composition HTML. Returns null if absent. */
|
||||
export function parseSlideshowManifest(html: string): SlideshowManifest | null {
|
||||
const re = slideshowIslandRegex("i");
|
||||
const match = re.exec(html);
|
||||
if (!match || match[1] === undefined) return null;
|
||||
const raw = match[1].trim();
|
||||
const island = slideshowIslandJson(html);
|
||||
if (island === null) return null;
|
||||
const raw = island.trim();
|
||||
if (raw.length === 0) return null;
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (!isManifest(parsed)) {
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
"unpacked": 885314
|
||||
},
|
||||
"@hyperframes/player": {
|
||||
"packed": 157145,
|
||||
"packed": 159000,
|
||||
"unpacked": 541601
|
||||
},
|
||||
"@hyperframes/producer": {
|
||||
|
||||
Reference in New Issue
Block a user