@@ -120,6 +120,28 @@ const getInstanceSize = (instanceId: string, tagName: HtmlTags | undefined) => {
120120
121121const MAX_SIZE_TO_USE_OPTIMIZATION = 50 ;
122122
123+ const findFirstNonContentsParent = ( element : Element ) => {
124+ // Start with the element's parent
125+ let parent = element . parentElement ;
126+
127+ // Continue traversing up until we find a non-contents parent or reach the top
128+ while ( parent ) {
129+ // Get the computed style of the parent
130+ const computedStyle = window . getComputedStyle ( parent ) ;
131+
132+ // Check if the display is not 'contents'
133+ if ( computedStyle . display !== "contents" ) {
134+ return parent ;
135+ }
136+
137+ // Move up to the next parent
138+ parent = parent . parentElement ;
139+ }
140+
141+ // Return null if no non-contents parent is found
142+ return null ;
143+ } ;
144+
123145const recalculate = ( ) => {
124146 const rootInstanceId = $selectedPage . get ( ) ?. rootInstanceId ;
125147
@@ -180,20 +202,23 @@ const recalculate = () => {
180202 elementsToRecalculate . push ( element ) ;
181203 }
182204
183- const elementPosition = window . getComputedStyle ( element ) . position ;
205+ const elementStyle = window . getComputedStyle ( element ) ;
206+ // const elementPosition = window.getComputedStyle(element).position;
207+
208+ const parentElement = findFirstNonContentsParent ( element ) ;
184209
185- if ( element . parentElement ) {
210+ if ( parentElement ) {
186211 if (
187- elementPosition === "absolute" ||
188- elementPosition === "fixed" ||
189- element . offsetParent == null
212+ elementStyle . position === "absolute" ||
213+ elementStyle . position === "fixed" ||
214+ element . offsetParent == null // collapsed or none
190215 ) {
191216 parentsWithAbsoluteChildren . set (
192- element . parentElement ,
193- parentsWithAbsoluteChildren . get ( element . parentElement ) ?? 0
217+ parentElement ,
218+ parentsWithAbsoluteChildren . get ( parentElement ) ?? 0
194219 ) ;
195220 } else {
196- parentsWithAbsoluteChildren . set ( element . parentElement , 1 ) ;
221+ parentsWithAbsoluteChildren . set ( parentElement , 1 ) ;
197222 }
198223 }
199224 }
0 commit comments