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])) {