Skip to content

Commit 6de37d2

Browse files
committed
fix(autoHeightBridge): prune trailing nodes and stabilize measurements for media/iframes
Add pruneTrailingNodes helper to remove trailing whitespace text, BR and empty P elements and invoke it from ensureWrapper, measureHeight, mutation observer and bootstrap. Also trigger additional scheduleMeasure/requestFrame calls around IMG, IFRAME and VIDEO readiness/load/error to improve height measurement stability.
1 parent c50da52 commit 6de37d2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/constants/autoHeightBridge.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,50 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
156156
return id;
157157
};
158158
159+
var pruneTrailingNodes = function (container) {
160+
if (!container) {
161+
return;
162+
}
163+
164+
var isWhitespaceText = function (node) {
165+
return node && node.nodeType === 3 && (!node.textContent || !node.textContent.trim());
166+
};
167+
168+
var isTrimmableElement = function (node) {
169+
if (!node || node.nodeType !== 1) {
170+
return false;
171+
}
172+
173+
var tag = (node.tagName || '').toUpperCase();
174+
if (tag === 'BR') {
175+
return true;
176+
}
177+
178+
if (tag === 'P') {
179+
return !node.textContent || !node.textContent.trim();
180+
}
181+
182+
return false;
183+
};
184+
185+
var removed = false;
186+
var current = container.lastChild;
187+
while (current) {
188+
if (isWhitespaceText(current) || isTrimmableElement(current)) {
189+
var previous = current.previousSibling;
190+
container.removeChild(current);
191+
current = previous;
192+
removed = true;
193+
continue;
194+
}
195+
break;
196+
}
197+
198+
if (removed) {
199+
scheduleMeasure(true);
200+
}
201+
};
202+
159203
var trackedMedia =
160204
typeof WeakSet === 'function' ? new WeakSet() : undefined;
161205
@@ -197,6 +241,8 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
197241
var body = document.body;
198242
var wrapper = ensureWrapper();
199243
244+
pruneTrailingNodes(wrapper);
245+
200246
var values = [];
201247
202248
var collect = function (element) {
@@ -373,6 +419,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
373419
374420
if (tag === 'IMG') {
375421
if (element.complete && element.naturalHeight) {
422+
scheduleMeasure(true);
376423
requestFrame(function () {
377424
scheduleMeasure(true);
378425
});
@@ -389,6 +436,9 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
389436
cleanupError();
390437
clearLoading();
391438
scheduleMeasure(true);
439+
requestFrame(function () {
440+
scheduleMeasure(true);
441+
});
392442
};
393443
394444
cleanupLoad = addEvent(element, 'load', onSettled, { once: true });
@@ -408,6 +458,9 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
408458
cleanupErrorIframe();
409459
clearLoading();
410460
scheduleMeasure(true);
461+
requestFrame(function () {
462+
scheduleMeasure(true);
463+
});
411464
};
412465
413466
cleanupLoadIframe = addEvent(element, 'load', onIframe, { once: true });
@@ -416,6 +469,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
416469
try {
417470
var iframeDoc = element.contentDocument;
418471
if (iframeDoc && iframeDoc.readyState === 'complete') {
472+
scheduleMeasure(true);
419473
requestFrame(onIframe);
420474
}
421475
} catch (error) {
@@ -430,6 +484,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
430484
typeof element.readyState === 'number' &&
431485
element.readyState >= 2
432486
) {
487+
scheduleMeasure(true);
433488
requestFrame(function () {
434489
scheduleMeasure(true);
435490
});
@@ -448,6 +503,9 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
448503
cleanupEnded();
449504
clearLoading();
450505
scheduleMeasure(true);
506+
requestFrame(function () {
507+
scheduleMeasure(true);
508+
});
451509
};
452510
453511
cleanupData = addEvent(element, 'loadeddata', onVideo, { once: true });
@@ -548,6 +606,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
548606
}
549607
550608
state.wrapper = wrapper;
609+
pruneTrailingNodes(wrapper);
551610
return wrapper;
552611
};
553612
@@ -600,6 +659,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
600659
}
601660
}
602661
}
662+
pruneTrailingNodes(state.wrapper);
603663
});
604664
605665
var target = document.documentElement || document.body;
@@ -735,6 +795,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
735795
var bootstrap = function () {
736796
applyBaseStyles();
737797
var wrapper = ensureWrapper();
798+
pruneTrailingNodes(wrapper);
738799
scanForMedia(wrapper || document);
739800
observeMutations();
740801
observeResize();

0 commit comments

Comments
 (0)