@@ -65,6 +65,15 @@ const sameSource = (a: EditingElementSource, b: EditingElementSource) =>
6565 a . compositionId === b . compositionId &&
6666 a . originalCompositionId === b . originalCompositionId ;
6767
68+ /** The element's source when every field fits the port; null otherwise, since a cut path or id names another file. */
69+ function boundedSource ( element : Element ) : EditingElementSource | null {
70+ const source = readElementSource ( element ) ;
71+ const fields = [ source . file , source . compositionId , source . originalCompositionId ] ;
72+ return fields . every ( ( field ) => field === null || field . length <= MAX_ELEMENT_VALUE )
73+ ? source
74+ : null ;
75+ }
76+
6877function matchesBefore ( element : Element , selector : string | undefined ) {
6978 if ( ! selector ?. startsWith ( "." ) ) return null ;
7079 const groups : Array < { source : EditingElementSource ; count : number } > = [ ] ;
@@ -76,7 +85,8 @@ function matchesBefore(element: Element, selector: string | undefined) {
7685 }
7786 for ( const match of matches ) {
7887 if ( match === element ) return groups ;
79- const source = readElementSource ( match ) ;
88+ const source = boundedSource ( match ) ;
89+ if ( ! source ) return null ;
8090 const group = groups . find ( ( entry ) => sameSource ( entry . source , source ) ) ;
8191 if ( group ) group . count += 1 ;
8292 else if ( groups . push ( { source, count : 1 } ) > MAX_SOURCE_GROUPS ) return null ;
@@ -105,14 +115,19 @@ function inlineStyles(element: Element, state: Cut): Record<string, string> {
105115 if ( declared . length > MAX_ELEMENT_ENTRIES ) state . truncated = true ;
106116 for ( let index = 0 ; index < Math . min ( declared . length , MAX_ELEMENT_ENTRIES ) ; index += 1 ) {
107117 const name = declared . item ( index ) ;
118+ if ( name . length > MAX_ELEMENT_VALUE ) {
119+ state . truncated = true ;
120+ continue ;
121+ }
108122 styles [ name ] = cut ( declared . getPropertyValue ( name ) , MAX_ELEMENT_VALUE , state , true ) ;
109123 }
110124 return styles ;
111125}
112126
113127function attributes ( element : Element , state : Cut ) : EditingElementDescription [ "attributes" ] {
114- const kept = Array . from ( element . attributes ) . filter ( ( attribute ) => attribute . name !== "style" ) ;
115- if ( kept . length > MAX_ELEMENT_ENTRIES ) state . truncated = true ;
128+ const named = Array . from ( element . attributes ) . filter ( ( attribute ) => attribute . name !== "style" ) ;
129+ const kept = named . filter ( ( attribute ) => attribute . name . length <= MAX_ELEMENT_VALUE ) ;
130+ if ( kept . length < named . length || kept . length > MAX_ELEMENT_ENTRIES ) state . truncated = true ;
116131 return kept
117132 . slice ( 0 , MAX_ELEMENT_ENTRIES )
118133 . map ( ( attribute ) => [ attribute . name , cut ( attribute . value , MAX_ELEMENT_VALUE , state , true ) ] ) ;
@@ -137,22 +152,27 @@ export function elementHit(element: Element): EditingElementHit {
137152 const rect = element . getBoundingClientRect ( ) ;
138153 return {
139154 handle : handleFor ( element ) ,
140- tag : element . tagName . toLowerCase ( ) ,
155+ tag : element . tagName . toLowerCase ( ) . slice ( 0 , MAX_ELEMENT_VALUE ) ,
141156 rect : { x : rect . x , y : rect . y , width : rect . width , height : rect . height } ,
142157 } ;
143158}
144159
145- function describeElement ( element : Element ) : EditingElementDescription {
146- const selector = buildStableSelector ( element ) ;
160+ const whole = ( value : string | undefined ) =>
161+ value && value . length <= MAX_ELEMENT_VALUE ? value : undefined ;
162+
163+ function describeElement ( element : Element ) : EditingElementDescription | null {
164+ const source = boundedSource ( element ) ;
165+ if ( ! source ) return null ;
166+ const selector = whole ( buildStableSelector ( element ) ) ;
147167 const state : Cut = { truncated : false } ;
148168 return {
149169 ...elementHit ( element ) ,
150- label : buildElementLabel ( element ) ,
170+ label : buildElementLabel ( element ) . slice ( 0 , MAX_ELEMENT_VALUE ) ,
151171 parent : element . parentElement ? handleFor ( element . parentElement ) : null ,
152- id : element . id || null ,
172+ id : whole ( element . id ) ?? null ,
153173 selector : selector ?? null ,
154174 matchesBefore : matchesBefore ( element , selector ) ,
155- source : readElementSource ( element ) ,
175+ source,
156176 locked : element . closest ( "[data-timeline-locked]" ) !== null ,
157177 attributes : attributes ( element , state ) ,
158178 inlineStyles : inlineStyles ( element , state ) ,
@@ -195,9 +215,12 @@ export function queryElements(selector: string): {
195215 truncated : boolean ;
196216} {
197217 const matches : EditingElementMatch [ ] = [ ] ;
218+ let truncated = false ;
198219 for ( const element of document . querySelectorAll ( selector ) ) {
199220 if ( matches . length === MAX_QUERY_MATCHES ) return { matches, truncated : true } ;
200- matches . push ( { handle : handleFor ( element ) , source : readElementSource ( element ) } ) ;
221+ const source = boundedSource ( element ) ;
222+ if ( source ) matches . push ( { handle : handleFor ( element ) , source } ) ;
223+ else truncated = true ;
201224 }
202- return { matches, truncated : false } ;
225+ return { matches, truncated } ;
203226}
0 commit comments