1+ function trimIdUnderscores ( value : string ) : string {
2+ let start = 0 ;
3+ let end = value . length ;
4+ while ( value [ start ] === "_" ) start += 1 ;
5+ while ( end > start && value [ end - 1 ] === "_" ) end -= 1 ;
6+ return value . slice ( start , end ) ;
7+ }
8+
19export function buildTimelineAssetId ( assetPath : string , existingIds : Iterable < string > ) : string {
2- const baseName = assetPath . split ( "/" ) . pop ( ) ?? "asset" ;
10+ const baseName = assetPath . slice ( assetPath . lastIndexOf ( "/" ) + 1 ) ;
311 const normalized = baseName
412 . replace ( / \. [ ^ . ] + $ / , "" )
513 . replace ( / [ ^ a - z A - Z 0 - 9 _ - ] + / g, "_" )
6- . replace ( / ^ _ + | _ + $ / g, "" )
714 . toLowerCase ( ) ;
8- const baseId = normalized || "asset" ;
15+ const baseId = trimIdUnderscores ( normalized ) || "asset" ;
916 const ids = new Set ( existingIds ) ;
1017 if ( ! ids . has ( baseId ) ) return baseId ;
1118 let suffix = 2 ;
1219 while ( ids . has ( `${ baseId } _${ suffix } ` ) ) suffix += 1 ;
1320 return `${ baseId } _${ suffix } ` ;
1421}
1522
23+ function referenceChildren ( element : Element ) : Iterable < Element > {
24+ if ( element . children . length > 0 ) return element . children ;
25+ if ( element . tagName . toLowerCase ( ) !== "template" ) return [ ] ;
26+ const content = ( element as HTMLTemplateElement ) . content ;
27+ return content ? content . children : [ ] ;
28+ }
29+
1630function documentElements ( document : Document ) : Element [ ] {
1731 const elements : Element [ ] = [ ] ;
1832 const visit = ( element : Element ) : void => {
1933 elements . push ( element ) ;
20- for ( const child of Array . from ( element . children ) ) visit ( child ) ;
21- if ( element . tagName . toLowerCase ( ) === "template" && element . children . length === 0 ) {
22- const content = ( element as HTMLTemplateElement ) . content ;
23- if ( content ) for ( const child of Array . from ( content . children ) ) visit ( child ) ;
24- }
34+ for ( const child of referenceChildren ( element ) ) visit ( child ) ;
2535 } ;
2636 if ( document . documentElement ) visit ( document . documentElement ) ;
2737 return elements ;
@@ -43,47 +53,59 @@ function referencesId(value: string, id: string): boolean {
4353 return decoded . includes ( id ) ;
4454}
4555
56+ function decodeFragment ( value : string ) : string {
57+ const fragment = value . split ( "#" ) . slice ( 1 ) . join ( "#" ) ;
58+ try {
59+ return decodeURIComponent ( fragment ) ;
60+ } catch {
61+ return fragment ;
62+ }
63+ }
64+
65+ function attributeReferencesId ( attribute : Attr , id : string ) : boolean {
66+ if ( [ "id" , "data-hf-id" ] . includes ( attribute . name ) ) return false ;
67+ if ( attribute . name === "src" ) return referencesId ( decodeFragment ( attribute . value ) , id ) ;
68+ return referencesId ( attribute . value , id ) ;
69+ }
70+
71+ function elementReferencesId ( element : Element , id : string ) : boolean {
72+ const code = [ "script" , "style" ] . includes ( element . tagName . toLowerCase ( ) ) ;
73+ if ( code && referencesId ( element . textContent ?? "" , id ) ) return true ;
74+ return Array . from ( element . attributes ) . some ( ( attribute ) => attributeReferencesId ( attribute , id ) ) ;
75+ }
76+
77+ type MediaIdentity = { id : string ; src : string } ;
78+
79+ function readMediaIdentity ( element : Element ) : MediaIdentity | null {
80+ const media = [ "video" , "audio" , "img" ] . includes ( element . tagName . toLowerCase ( ) ) ;
81+ const src = element . getAttribute ( "src" ) ;
82+ const id = element . getAttribute ( "id" ) ;
83+ if ( ! media || ! src || ! id ) return null ;
84+ return { id, src } ;
85+ }
86+
87+ function matchesGeneratedId ( { id, src } : MediaIdentity ) : boolean {
88+ const base = buildTimelineAssetId ( src , [ ] ) ;
89+ const suffix = id . slice ( base . length ) ;
90+ return id === base || ( id . startsWith ( base ) && / ^ _ (?: [ 2 - 9 ] \d * | 1 \d + ) $ / . test ( suffix ) ) ;
91+ }
92+
93+ function canRenameMedia ( element : Element , identity : MediaIdentity , newSrc : string ) : boolean {
94+ const authored = [ "data-timeline-label" , "data-label" , "aria-label" ] . some ( ( name ) =>
95+ element . getAttribute ( name ) ?. trim ( ) ,
96+ ) ;
97+ return ! authored && identity . src !== newSrc && matchesGeneratedId ( identity ) ;
98+ }
99+
46100export function replacementTimelineAssetId (
47101 document : Document ,
48102 element : Element ,
49103 newSrc : string ,
50104) : string | null {
51- if ( ! [ "video" , "audio" , "img" ] . includes ( element . tagName . toLowerCase ( ) ) ) return null ;
52- const oldSrc = element . getAttribute ( "src" ) ;
53- const id = element . getAttribute ( "id" ) ;
54- if ( ! oldSrc || ! id || oldSrc === newSrc ) return null ;
55- if (
56- [ "data-timeline-label" , "data-label" , "aria-label" ] . some ( ( name ) =>
57- element . getAttribute ( name ) ?. trim ( ) ,
58- )
59- )
60- return null ;
61- const base = buildTimelineAssetId ( oldSrc , [ ] ) ;
62- const suffix = id . slice ( base . length ) ;
63- if ( id !== base && ( ! id . startsWith ( base ) || ! / ^ _ [ 2 - 9 ] \d * $ | ^ _ 1 \d + $ / . test ( suffix ) ) ) return null ;
105+ const identity = readMediaIdentity ( element ) ;
106+ if ( ! identity || ! canRenameMedia ( element , identity , newSrc ) ) return null ;
64107 const elements = documentElements ( document ) ;
65- for ( const other of elements ) {
66- if (
67- [ "script" , "style" ] . includes ( other . tagName . toLowerCase ( ) ) &&
68- referencesId ( other . textContent ?? "" , id )
69- )
70- return null ;
71- for ( const attribute of Array . from ( other . attributes ) ) {
72- if ( attribute . name === "id" || attribute . name === "data-hf-id" ) continue ;
73- if ( attribute . name === "src" ) {
74- const fragment = attribute . value . split ( "#" ) . slice ( 1 ) . join ( "#" ) ;
75- let decodedFragment = fragment ;
76- try {
77- decodedFragment = decodeURIComponent ( fragment ) ;
78- } catch {
79- decodedFragment = fragment ;
80- }
81- if ( referencesId ( decodedFragment , id ) ) return null ;
82- continue ;
83- }
84- if ( referencesId ( attribute . value , id ) ) return null ;
85- }
86- }
108+ if ( elements . some ( ( other ) => elementReferencesId ( other , identity . id ) ) ) return null ;
87109 const ids = elements
88110 . filter ( ( other ) => other !== element )
89111 . map ( ( other ) => other . getAttribute ( "id" ) )
0 commit comments