@@ -383,6 +383,16 @@ function setElementDuration(
383383 } ) ;
384384}
385385
386+ /** True when the media has an authored window and the row's range (if given) is that window. */
387+ function isWindowOf (
388+ media : { start : number ; duration : number } ,
389+ row ?: { start : number ; duration : number } ,
390+ ) : boolean {
391+ if ( media . duration <= 0 ) return false ;
392+ const near = ( a : number , b : number ) => Math . abs ( a - b ) < 0.001 ;
393+ return ! row || ( near ( media . start , row . start ) && near ( media . duration , row . duration ) ) ;
394+ }
395+
386396// fallow-ignore-next-line complexity
387397export function splitElementInHtml (
388398 source : string ,
@@ -405,7 +415,20 @@ export function splitElementInHtml(
405415 const el = findTargetElement ( document , target ) ;
406416 if ( ! el || ! isHTMLElement ( el ) ) return { html : source , matched : false , newId : null } ;
407417
408- const timing = resolveElementTiming ( el ) ;
418+ // An untimed wrapper whose row is its timed media's own window is a view of that
419+ // media: cut the media and leave the wrappers untimed. A GSAP scene keeps its own window.
420+ const timedMediaSelector = "video[data-start], audio[data-start]" ;
421+ const innerMedia =
422+ el . hasAttribute ( "data-start" ) ||
423+ resolveElementTiming ( el ) . duration > 0 ||
424+ el . matches ( "[data-composition-src], [data-composition-id]" )
425+ ? null
426+ : el . querySelector ( timedMediaSelector ) ;
427+ const timedMedia =
428+ innerMedia && isWindowOf ( resolveElementTiming ( innerMedia ) , fallbackTiming ) ? innerMedia : null ;
429+ const timed = timedMedia ?? el ;
430+
431+ const timing = resolveElementTiming ( timed ) ;
409432 let { start, duration } = timing ;
410433 // GSAP-animated elements carry their timing in the script, not in data-* attrs,
411434 // so the source has no authored duration. Fall back to the store's (GSAP-derived)
@@ -432,34 +455,40 @@ export function splitElementInHtml(
432455 // Descendants carry their own data-hf-id; leaving them duplicates the id of
433456 // every nested node (e.g. an inner <span>), so strip them on the clone too.
434457 for ( const node of clone . querySelectorAll ( "[data-hf-id]" ) ) node . removeAttribute ( "data-hf-id" ) ;
435- setElementDuration ( clone , splitTime , secondDuration , fallbackTiming ?. track ) ;
458+ const clonedTimed = timedMedia ? clone . querySelector ( timedMediaSelector ) ! : clone ;
459+ if ( timedMedia ?. id ) {
460+ const mediaId = nextUniqueId ( document , `${ timedMedia . id } -split` , new Set ( [ newId ] ) ) ;
461+ duplicateCssRulesForId ( document , timedMedia . id , mediaId ) ;
462+ clonedTimed . setAttribute ( "id" , mediaId ) ;
463+ }
464+ setElementDuration ( clonedTimed , splitTime , secondDuration , fallbackTiming ?. track ) ;
436465
437466 // Keep the "clip" class — the runtime uses it to control visibility
438467 // based on data-start/data-duration timing.
439468
440469 // A split creates two views over the same media source or sub-composition.
441470 // Even an untrimmed one needs an explicit zero in-point stamped on the first
442471 // half so the second half can advance from it instead of restarting at zero.
443- const playbackStartAttr = el . hasAttribute ( "data-playback-start" )
472+ const playbackStartAttr = timed . hasAttribute ( "data-playback-start" )
444473 ? "data-playback-start"
445- : el . hasAttribute ( "data-media-start" )
474+ : timed . hasAttribute ( "data-media-start" )
446475 ? "data-media-start"
447- : el . matches ( "[data-composition-src], [data-composition-id]" )
476+ : timed . matches ( "[data-composition-src], [data-composition-id]" )
448477 ? "data-playback-start"
449- : el . matches ( "audio, video" )
478+ : timed . matches ( "audio, video" )
450479 ? "data-media-start"
451480 : null ;
452481 if ( playbackStartAttr ) {
453- const readAttr = ( name : string ) => el . getAttribute ( name ) ;
454- const authoredTrim = el . getAttribute ( playbackStartAttr ) ;
482+ const readAttr = ( name : string ) => timed . getAttribute ( name ) ;
483+ const authoredTrim = timed . getAttribute ( playbackStartAttr ) ;
455484 const currentTrim =
456485 authoredTrim !== null
457486 ? readMediaOffsetSeconds ( readAttr )
458487 : ( fallbackTiming ?. playbackStart ?? 0 ) ;
459488 const rate = readPlaybackRate ( readAttr , fallbackTiming ?. playbackRate ) ;
460489 if ( authoredTrim === null || Number ( authoredTrim ) !== currentTrim )
461- el . setAttribute ( playbackStartAttr , String ( Math . round ( currentTrim * 1000 ) / 1000 ) ) ;
462- clone . setAttribute (
490+ timed . setAttribute ( playbackStartAttr , String ( Math . round ( currentTrim * 1000 ) / 1000 ) ) ;
491+ clonedTimed . setAttribute (
463492 playbackStartAttr ,
464493 String ( Math . round ( ( currentTrim + firstDuration * rate ) * 1000 ) / 1000 ) ,
465494 ) ;
@@ -473,7 +502,7 @@ export function splitElementInHtml(
473502
474503 // Trim the original element's duration. A GSAP element had no data-start; stamp
475504 // it so the runtime windows the first half (visibility selects on [data-start]).
476- setElementDuration ( el , start , firstDuration , fallbackTiming ?. track ) ;
505+ setElementDuration ( timed , start , firstDuration , fallbackTiming ?. track ) ;
477506
478507 // Insert clone after original
479508 if ( el . nextSibling ) {
0 commit comments