From 3e01f691dbf44e4b1ce7b7d20cceede5df111636 Mon Sep 17 00:00:00 2001 From: Ular Kimsanov Date: Tue, 17 Nov 2026 08:44:40 +0000 Subject: [PATCH] fix(lint): match a timed wrapper's close tag literally (#8028) * fix(lint): match a timed wrapper's close tag literally The parser keeps any tag name up to whitespace, / or >, so a wrapper's name can hold regex syntax. closeTag built its pattern from the raw name: an unbalanced name threw a SyntaxError that rejected the whole lint, and a nested-quantifier name backtracked exponentially over the text before a nested video. Escape the name with the file's own escapeRegExp. * refactor(lint): name the escaped close-tag pattern instead of commenting it The comment ratchet keeps a file's comment share from rising. --- packages/lint/src/rules/media.test.ts | 29 +++++++++++++++++++++++++++ packages/lint/src/rules/media.ts | 8 ++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/lint/src/rules/media.test.ts b/packages/lint/src/rules/media.test.ts index aac27a0e67..f36516a18d 100644 --- a/packages/lint/src/rules/media.test.ts +++ b/packages/lint/src/rules/media.test.ts @@ -419,6 +419,35 @@ describe("media rules", () => { expect(audio).toContain('data-start="0"'); }); + // The parser keeps any tag name up to whitespace, `/` or `>`, so a wrapper's name can hold + // regex syntax. Its close tag must be matched literally: unescaped, `x(` throws and a + // nested-quantifier name backtracks exponentially over the text before the video. + it("matches a timed wrapper's close tag literally whatever its name holds", async () => { + const nestedCodes = async (body: string) => + ( + await lintHyperframeHtml(` + +
${body}
+ +`) + ).findings.map((f) => f.code); + const video = ``; + + expect(await nestedCodes(`${video}`)).toContain( + "video_nested_in_timed_element", + ); + expect(await nestedCodes(`${video}`)).not.toContain( + "video_nested_in_timed_element", + ); + + const started = performance.now(); + const backtracking = await nestedCodes( + `${video}`, + ); + expect(performance.now() - started).toBeLessThan(2000); + expect(backtracking).toContain("video_nested_in_timed_element"); + }); + it("allows audible video clips to omit muted when data-has-audio is true", async () => { const html = ` diff --git a/packages/lint/src/rules/media.ts b/packages/lint/src/rules/media.ts index d9640478df..080b4f60f3 100644 --- a/packages/lint/src/rules/media.ts +++ b/packages/lint/src/rules/media.ts @@ -445,7 +445,7 @@ const VOID_ELEMENTS = new Set([ "wbr", ]); -const closeTag = (name: string) => new RegExp(``, "gi"); +const literalCloseTag = (name: string) => new RegExp(``, "gi"); function innermostTimedWrapper( source: string, @@ -465,9 +465,9 @@ function innermostTimedWrapper( ...tags .filter((t) => t.name === parent.name && t.index > parent.index && t.index < media.index) .map((t): [number, 1] => [t.index, 1]), - ...[...source.substring(parent.index, media.index).matchAll(closeTag(parent.name))].map( - (m): [number, -1] => [parent.index + m.index, -1], - ), + ...[ + ...source.substring(parent.index, media.index).matchAll(literalCloseTag(parent.name)), + ].map((m): [number, -1] => [parent.index + m.index, -1]), ]; let depth = 1; for (const [, delta] of events.sort((x, y) => x[0] - y[0])) {