Skip to content

Commit dd2e905

Browse files
committed
fix(autoHeightBridge): manage wrapper element, guard DOM/style ops and stabilize measurements
Add ensureWrapper and track state.wrapper; use wrapper when measuring and scanning media, observe wrapper in Resize/Mutation observers, and ensure wrapper on refresh/bootstrap. Avoid force-applying overflow/height/minHeight/padding/width when styles already set. Reset wrapper on cleanup and improve message handling to prevent unnecessary overrides.
1 parent 0a6d09f commit dd2e905

1 file changed

Lines changed: 101 additions & 33 deletions

File tree

src/constants/autoHeightBridge.ts

Lines changed: 101 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
export const AUTO_HEIGHT_BRIDGE = `(() => {
66
var GLOBAL_KEY = '__RN_SIZED_WEBVIEW__';
7+
var WRAPPER_ID = '__RN_SIZED_WEBVIEW_WRAPPER__';
78
var MESSAGE_KEY = '__AUTO_HEIGHT__';
89
var ACTIVE_DEBOUNCE_MS = 48;
910
var IDLE_DEBOUNCE_MS = 160;
@@ -46,6 +47,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
4647
fallbackTimer: null,
4748
fallbackDelay: INITIAL_FALLBACK_MS,
4849
cleanup: [],
50+
wrapper: null,
4951
};
5052
5153
window[GLOBAL_KEY] = state;
@@ -101,9 +103,12 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
101103
}
102104
103105
state.cleanup.length = 0;
106+
state.wrapper = null;
107+
window[GLOBAL_KEY] = undefined;
104108
};
105109
106110
state.refresh = function () {
111+
ensureWrapper();
107112
scheduleMeasure(true);
108113
};
109114
@@ -190,36 +195,35 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
190195
var measureHeight = function () {
191196
var html = document.documentElement;
192197
var body = document.body;
198+
var wrapper = ensureWrapper();
193199
194-
if (!html) {
195-
return 0;
196-
}
200+
var values = [];
197201
198-
var values = [
199-
readRectHeight(html),
200-
html.scrollHeight,
201-
html.offsetHeight,
202-
html.clientHeight,
203-
window.innerHeight || 0,
204-
];
202+
var collect = function (element) {
203+
if (!element) {
204+
return;
205+
}
205206
206-
var scroller = document.scrollingElement;
207-
if (scroller && scroller !== html && scroller !== body) {
208207
values.push(
209-
readRectHeight(scroller),
210-
scroller.scrollHeight,
211-
scroller.offsetHeight,
212-
scroller.clientHeight
208+
readRectHeight(element),
209+
element.scrollHeight || 0,
210+
element.offsetHeight || 0,
211+
element.clientHeight || 0
213212
);
213+
};
214+
215+
collect(wrapper);
216+
if (body && body !== wrapper) {
217+
collect(body);
218+
}
219+
if (html && html !== wrapper) {
220+
collect(html);
214221
}
215222
216-
if (body && body !== html) {
217-
values.push(
218-
readRectHeight(body),
219-
body.scrollHeight,
220-
body.offsetHeight,
221-
body.clientHeight
222-
);
223+
values.push(window.innerHeight || 0);
224+
225+
if (!values.length) {
226+
return 0;
223227
}
224228
225229
return Math.max(0, Math.ceil(readMaxValue(values)));
@@ -480,10 +484,11 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
480484
var body = document.body;
481485
482486
if (html) {
483-
html.style.overflow = 'hidden';
484-
html.style.height = 'auto';
485-
if (!html.style.minHeight || html.style.minHeight === '0') {
486-
html.style.minHeight = '100%';
487+
if (!html.style.overflow) {
488+
html.style.overflow = 'hidden';
489+
}
490+
if (!html.style.height) {
491+
html.style.height = 'auto';
487492
}
488493
if (!html.style.backgroundColor) {
489494
html.style.backgroundColor = 'transparent';
@@ -494,17 +499,58 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
494499
if (!body.style.margin) {
495500
body.style.margin = '0';
496501
}
497-
body.style.width = '100%';
498-
body.style.height = 'auto';
499-
if (!body.style.minHeight || body.style.minHeight === '0') {
500-
body.style.minHeight = '100%';
502+
if (!body.style.padding) {
503+
body.style.padding = '0';
504+
}
505+
if (!body.style.width) {
506+
body.style.width = '100%';
507+
}
508+
if (!body.style.height) {
509+
body.style.height = 'auto';
501510
}
502511
if (!body.style.backgroundColor) {
503512
body.style.backgroundColor = 'transparent';
504513
}
505514
}
506515
};
507516
517+
var ensureWrapper = function () {
518+
if (state.wrapper && document.contains(state.wrapper)) {
519+
return state.wrapper;
520+
}
521+
522+
var body = document.body;
523+
if (!body) {
524+
return null;
525+
}
526+
527+
var existing = document.getElementById(WRAPPER_ID);
528+
if (existing) {
529+
state.wrapper = existing;
530+
return existing;
531+
}
532+
533+
var wrapper = document.createElement('div');
534+
wrapper.id = WRAPPER_ID;
535+
wrapper.style.width = '100%';
536+
wrapper.style.boxSizing = 'border-box';
537+
538+
var nodes = [];
539+
while (body.firstChild) {
540+
nodes.push(body.firstChild);
541+
body.removeChild(body.firstChild);
542+
}
543+
544+
body.appendChild(wrapper);
545+
546+
for (var index = 0; index < nodes.length; index += 1) {
547+
wrapper.appendChild(nodes[index]);
548+
}
549+
550+
state.wrapper = wrapper;
551+
return wrapper;
552+
};
553+
508554
var ensureDomReady = function (callback) {
509555
if (
510556
document.readyState === 'interactive' ||
@@ -537,6 +583,10 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
537583
var mutationObserver = new MutationObserver(function (mutations) {
538584
requestDebouncedMeasure();
539585
586+
if (!state.wrapper || !document.contains(state.wrapper)) {
587+
ensureWrapper();
588+
}
589+
540590
for (var index = 0; index < mutations.length; index += 1) {
541591
var mutation = mutations[index];
542592
if (!mutation || !mutation.addedNodes) {
@@ -578,14 +628,19 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
578628
requestDebouncedMeasure();
579629
});
580630
631+
var wrapper = ensureWrapper();
581632
var html = document.documentElement;
582633
var body = document.body;
583634
635+
if (wrapper) {
636+
resizeObserver.observe(wrapper);
637+
}
638+
584639
if (html) {
585640
resizeObserver.observe(html);
586641
}
587642
588-
if (body && body !== html) {
643+
if (body && body !== html && body !== wrapper) {
589644
resizeObserver.observe(body);
590645
}
591646
@@ -650,6 +705,18 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
650705
651706
if (event.data === MESSAGE_KEY) {
652707
scheduleMeasure(true);
708+
return;
709+
}
710+
711+
if (typeof event.data === 'string') {
712+
try {
713+
var parsed = JSON.parse(event.data);
714+
if (parsed && parsed.topic === MESSAGE_KEY) {
715+
scheduleMeasure(true);
716+
}
717+
} catch (error) {
718+
// Ignore non-JSON payloads.
719+
}
653720
}
654721
});
655722
};
@@ -667,7 +734,8 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
667734
668735
var bootstrap = function () {
669736
applyBaseStyles();
670-
scanForMedia(document);
737+
var wrapper = ensureWrapper();
738+
scanForMedia(wrapper || document);
671739
observeMutations();
672740
observeResize();
673741
observeViewport();

0 commit comments

Comments
 (0)