11import { readFileSync } from "node:fs" ;
22import { dirname , join } from "node:path" ;
3+ import { JSDOM } from "jsdom" ;
34import { describe , it , expect } from "vitest" ;
45import {
56 compileTimingAttrs ,
@@ -18,6 +19,82 @@ it("source contains no raw NUL bytes", () => {
1819 expect ( src . includes ( "\x00" ) ) . toBe ( false ) ;
1920} ) ;
2021
22+ describe ( "inert region scanning" , ( ) => {
23+ const media = '<video id="v" data-start="1" data-duration="2">' ;
24+
25+ it . each ( [
26+ `<!-- ${ media } <!-- nested -->` ,
27+ `<ScRiPt type="text/javascript">${ media } </sCrIpT \n>` ,
28+ `<STYLE>${ media } </STYLE\u00a0>` ,
29+ `<script-data>${ media } </script>` ,
30+ `<style:${ media } </style>` ,
31+ `<!-- <script>${ media } -->` ,
32+ `<script><!-- ${ media } </script>` ,
33+ `<style><script>${ media } </style>` ,
34+ ] ) ( "preserves the existing complete-region boundaries in %j" , ( region ) => {
35+ const result = compileTimingAttrs ( region + media ) ;
36+ expect ( result ) . toEqual ( { html : region + compileTimingAttrs ( media ) . html , unresolved : [ ] } ) ;
37+ expect ( extractResolvedMedia ( region + media ) ) . toEqual ( extractResolvedMedia ( media ) ) ;
38+ } ) ;
39+
40+ it . each ( [ "<!--" , "<script>" , "<style>" , "<scripture>" , "<stylesheet>" ] ) (
41+ "keeps media outside a complete inert region after %j visible" ,
42+ ( prefix ) => {
43+ expect ( compileTimingAttrs ( prefix + media ) . html ) . toBe ( prefix + compileTimingAttrs ( media ) . html ) ;
44+ expect ( extractResolvedMedia ( prefix + media ) ) . toEqual ( extractResolvedMedia ( media ) ) ;
45+ } ,
46+ ) ;
47+
48+ it . each ( [ "-->" , "--!>" ] ) ( "recognizes %j as the first comment end like the browser" , ( end ) => {
49+ const hidden = '<video id="hidden" data-duration="1">' ;
50+ const visible = '<video id="visible" data-start="1" data-duration="2">' ;
51+ const comment = `<!-- ${ hidden } ${ end } ` ;
52+ // The trailing delimiter must not extend the comment over visible media.
53+ const html = comment + visible + " -->" ;
54+ const dom = new JSDOM ( html ) ;
55+ expect ( [ ...dom . window . document . querySelectorAll ( "video" ) ] . map ( ( el ) => el . id ) ) . toEqual ( [
56+ "visible" ,
57+ ] ) ;
58+ dom . window . close ( ) ;
59+ expect ( compileTimingAttrs ( html ) ) . toEqual ( {
60+ html : comment + compileTimingAttrs ( visible ) . html + " -->" ,
61+ unresolved : [ ] ,
62+ } ) ;
63+ expect ( extractResolvedMedia ( html ) . map ( ( el ) => el . id ) ) . toEqual ( [ "visible" ] ) ;
64+ } ) ;
65+
66+ it ( "closes an end-bang comment without a later standard delimiter" , ( ) => {
67+ const comment = '<!-- <video id="hidden" data-duration="1"> --!>' ;
68+ expect ( compileTimingAttrs ( comment + media ) ) . toEqual ( {
69+ html : comment + compileTimingAttrs ( media ) . html ,
70+ unresolved : [ ] ,
71+ } ) ;
72+ expect ( extractResolvedMedia ( comment + media ) ) . toEqual ( extractResolvedMedia ( media ) ) ;
73+ } ) ;
74+
75+ it . each ( [ "<!--" , "<script" , "<style" ] ) (
76+ "handles many unclosed %j prefixes while masking other region kinds" ,
77+ ( prefix ) => {
78+ const unclosed = prefix . repeat ( 100_000 ) ;
79+ const hidden = prefix === "<!--" ? `<style>${ media } </style>` : `<!--${ media } -->` ;
80+ expect ( compileTimingAttrs ( unclosed + hidden + media ) ) . toEqual ( {
81+ html : unclosed + hidden + compileTimingAttrs ( media ) . html ,
82+ unresolved : [ ] ,
83+ } ) ;
84+ expect ( extractResolvedMedia ( unclosed + hidden + media ) ) . toEqual ( extractResolvedMedia ( media ) ) ;
85+ } ,
86+ ) ;
87+
88+ it ( "uses the first closing delimiter and resumes scanning after it" , ( ) => {
89+ const hidden = `<script>${ media } </script>` ;
90+ const html = hidden + media + `</script><!--${ media } --><style>${ media } </style>` ;
91+ expect ( compileTimingAttrs ( html ) . html ) . toBe (
92+ hidden + compileTimingAttrs ( media ) . html + `</script><!--${ media } --><style>${ media } </style>` ,
93+ ) ;
94+ expect ( extractResolvedMedia ( html ) ) . toEqual ( extractResolvedMedia ( media ) ) ;
95+ } ) ;
96+ } ) ;
97+
2198describe ( "compileTimingAttrs" , ( ) => {
2299 it . each ( [ "" , " " , "0s" , "0abc" , "0px" , "-1s" , "Infinity" , "NaN" ] ) (
23100 "does not partially parse invalid literal data-duration=%j" ,
0 commit comments