@@ -154,6 +154,60 @@ function maskInertRegions(html: string): { masked: string; restore: (s: string)
154154
155155// ── Core compilation ─────────────────────────────────────────────────────
156156
157+ function * iterateOpeningTags ( html : string , prefix : RegExp ) {
158+ let match : RegExpExecArray | null ;
159+ while ( ( match = prefix . exec ( html ) ) !== null ) {
160+ const closing = html . indexOf ( ">" , prefix . lastIndex ) ;
161+ // Without a closer, no later prefix can form a complete tag either.
162+ if ( closing < 0 ) break ;
163+ const end = closing + 1 ;
164+ yield { tag : html . slice ( match . index , end ) , index : match . index , end } ;
165+ prefix . lastIndex = end ;
166+ }
167+ }
168+
169+ function replaceOpeningTags (
170+ html : string ,
171+ prefix : RegExp ,
172+ replace : ( tag : string ) => string ,
173+ ) : string {
174+ const parts : string [ ] = [ ] ;
175+ let cursor = 0 ;
176+ for ( const { tag, index, end } of iterateOpeningTags ( html , prefix ) ) {
177+ parts . push ( html . slice ( cursor , index ) , replace ( tag ) ) ;
178+ cursor = end ;
179+ }
180+ parts . push ( html . slice ( cursor ) ) ;
181+ return parts . join ( "" ) ;
182+ }
183+
184+ function replaceIdTags ( html : string , id : string , replace : ( tag : string ) => string ) : string {
185+ const idPattern = new RegExp ( `id=["']${ escapeRegex ( id ) } ["']` , "gi" ) ;
186+ const lastClosing = html . lastIndexOf ( ">" ) ;
187+ const parts : string [ ] = [ ] ;
188+ let cursor = 0 ;
189+ let candidate = idPattern . exec ( html ) ;
190+ for ( const { index, end } of iterateOpeningTags ( html , / < / g) ) {
191+ if ( index < cursor ) continue ;
192+ let targetEnd = - 1 ;
193+ while ( candidate && candidate . index < end ) {
194+ const candidateEnd = candidate . index + candidate [ 0 ] . length ;
195+ if ( candidate . index > index && candidateEnd <= lastClosing ) targetEnd = candidateEnd ;
196+ // Preserve the old greedy prefix's last matching ID, including overlaps.
197+ idPattern . lastIndex = candidate . index + 1 ;
198+ candidate = idPattern . exec ( html ) ;
199+ }
200+ if ( targetEnd < 0 ) continue ;
201+ // An authored ID can itself contain '>', so its closer may follow the
202+ // initial span. The lastClosing check guarantees a closing delimiter.
203+ const closing = html . indexOf ( ">" , targetEnd ) + 1 ;
204+ parts . push ( html . slice ( cursor , index ) , replace ( html . slice ( index , closing ) ) ) ;
205+ cursor = closing ;
206+ }
207+ parts . push ( html . slice ( cursor ) ) ;
208+ return parts . join ( "" ) ;
209+ }
210+
157211function compileTag (
158212 tag : string ,
159213 isVideo : boolean ,
@@ -228,24 +282,24 @@ export function compileTimingAttrs(html: string): CompilationResult {
228282 html = masked ;
229283
230284 // Process <video ...> tags
231- html = html . replace ( / < v i d e o [ ^ > ] * > / gi, ( match ) => {
285+ html = replaceOpeningTags ( html , / < v i d e o / gi, ( match ) => {
232286 const { tag, unresolved : u } = compileTag ( match , true , ( ) => nextVideoId ++ ) ;
233287 if ( u ) unresolved . push ( u ) ;
234288 return tag ;
235289 } ) ;
236290
237291 // Process <audio ...> tags
238- html = html . replace ( / < a u d i o [ ^ > ] * > / gi, ( match ) => {
292+ html = replaceOpeningTags ( html , / < a u d i o / gi, ( match ) => {
239293 const { tag, unresolved : u } = compileTag ( match , false , ( ) => nextAudioId ++ ) ;
240294 if ( u ) unresolved . push ( u ) ;
241295 return tag ;
242296 } ) ;
243297
244298 // Identify unresolved timed elements (divs with data-start but no data-end/data-duration)
245299 // These are typically compositions whose duration depends on GSAP timelines
246- html . replace ( / < (?: d i v | s e c t i o n ) [ ^ > ] * > / gi, ( match ) => {
247- if ( ! hasAttr ( match , "data-start" ) ) return match ;
248- if ( hasAttr ( match , "data-end" ) || hasAttr ( match , "data-duration" ) ) return match ;
300+ for ( const { tag : match } of iterateOpeningTags ( html , / < (?: d i v | s e c t i o n ) / gi) ) {
301+ if ( ! hasAttr ( match , "data-start" ) ) continue ;
302+ if ( hasAttr ( match , "data-end" ) || hasAttr ( match , "data-duration" ) ) continue ;
249303
250304 const id = getAttr ( match , "id" ) ;
251305 const compositionSrc = getAttr ( match , "data-composition-src" ) ;
@@ -260,9 +314,7 @@ export function compileTimingAttrs(html: string): CompilationResult {
260314 compositionSrc : compositionSrc ?? undefined ,
261315 } ) ;
262316 }
263-
264- return match ;
265- } ) ;
317+ }
266318
267319 return { html : restore ( html ) , unresolved } ;
268320}
@@ -277,9 +329,7 @@ export function compileTimingAttrs(html: string): CompilationResult {
277329export function injectDurations ( html : string , resolutions : ResolvedDuration [ ] ) : string {
278330 for ( const { id, duration } of resolutions ) {
279331 // Match the element's opening tag by id
280- const idPattern = new RegExp ( `(<[^>]*id=["']${ escapeRegex ( id ) } ["'][^>]*>)` , "gi" ) ;
281-
282- html = html . replace ( idPattern , ( tag ) => {
332+ html = replaceIdTags ( html , id , ( tag ) => {
283333 let result = tag ;
284334
285335 // Add data-duration if missing
@@ -314,10 +364,7 @@ export function extractResolvedMedia(html: string): ResolvedMediaElement[] {
314364 const resolved : ResolvedMediaElement [ ] = [ ] ;
315365
316366 html = maskInertRegions ( html ) . masked ;
317- const mediaRegex = / < (?: v i d e o | a u d i o ) [ ^ > ] * > / gi;
318- let match : RegExpExecArray | null ;
319- while ( ( match = mediaRegex . exec ( html ) ) !== null ) {
320- const tag = match [ 0 ] ;
367+ for ( const { tag } of iterateOpeningTags ( html , / < (?: v i d e o | a u d i o ) / gi) ) {
321368 const id = getAttr ( tag , "id" ) ;
322369 const durationStr = getAttr ( tag , "data-duration" ) ;
323370 if ( ! id || durationStr === null ) continue ;
@@ -351,9 +398,7 @@ export function extractResolvedMedia(html: string): ResolvedMediaElement[] {
351398 */
352399export function clampDurations ( html : string , clamps : ResolvedDuration [ ] ) : string {
353400 for ( const { id, duration } of clamps ) {
354- const idPattern = new RegExp ( `(<[^>]*id=["']${ escapeRegex ( id ) } ["'][^>]*>)` , "gi" ) ;
355-
356- html = html . replace ( idPattern , ( tag ) => {
401+ html = replaceIdTags ( html , id , ( tag ) => {
357402 // Replace data-duration value
358403 tag = tag . replace ( / d a t a - d u r a t i o n = [ " ' ] [ ^ " ' ] * [ " ' ] / , `data-duration="${ duration } "` ) ;
359404
0 commit comments