Skip to content

Commit 6e4231b

Browse files
authored
Merge pull request #1 from mCodex/fix/emptyHTMLTagsCleanup
Fix/empty html tags cleanup
2 parents 89b046b + fb37598 commit 6e4231b

File tree

4 files changed

+191
-103
lines changed

4 files changed

+191
-103
lines changed

example/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
},
1111
"dependencies": {
1212
"@expo/metro-runtime": "~6.1.2",
13-
"expo": "~54.0.21",
13+
"expo": "~54.0.23",
1414
"expo-status-bar": "~3.0.8",
15-
"react": "19.1.0",
16-
"react-dom": "19.1.0",
15+
"react": "19.1.1",
16+
"react-dom": "19.1.1",
1717
"react-native": "0.81.5",
1818
"react-native-safe-area-context": "^5.6.2",
1919
"react-native-web": "~0.21.2",
2020
"react-native-webview": "^13.16.0"
2121
},
2222
"private": true,
2323
"devDependencies": {
24-
"react-native-builder-bob": "^0.40.14",
24+
"react-native-builder-bob": "^0.40.15",
2525
"react-native-monorepo-config": "^0.3.0"
2626
}
2727
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sized-webview",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "React Native WebView that auto-sizes to match its HTML content—whether you load local HTML or full external websites—without manual measurements, timers, or layout flicker.",
55
"main": "./lib/module/index.js",
66
"module": "./lib/module/index.js",

src/constants/autoHeightBridge.ts

Lines changed: 93 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
5757
microtask: false,
5858
pendingLoads: 0,
5959
lastHeight: 0,
60-
lastCssHeight: 0,
6160
anomalyCount: 0,
6261
fallbackTimer: null,
6362
fallbackDelay: INITIAL_FALLBACK_MS,
@@ -173,13 +172,62 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
173172
return id;
174173
};
175174
175+
var RENDERABLE_MEDIA_TAGS = {
176+
IMG: true,
177+
IFRAME: true,
178+
VIDEO: true,
179+
SVG: true,
180+
CANVAS: true,
181+
PICTURE: true,
182+
OBJECT: true,
183+
EMBED: true,
184+
AUDIO: true,
185+
};
186+
187+
var hasRenderableContent = function (node) {
188+
if (!node || !node.childNodes || !node.childNodes.length) {
189+
return false;
190+
}
191+
192+
var child = node.firstChild;
193+
while (child) {
194+
if (child.nodeType === 3) {
195+
if (child.textContent && child.textContent.trim()) {
196+
return true;
197+
}
198+
} else if (child.nodeType === 1) {
199+
var tag = (child.tagName || '').toUpperCase();
200+
if (tag === 'BR') {
201+
child = child.nextSibling;
202+
continue;
203+
}
204+
205+
if (RENDERABLE_MEDIA_TAGS[tag]) {
206+
return true;
207+
}
208+
209+
if (hasRenderableContent(child)) {
210+
return true;
211+
}
212+
}
213+
214+
child = child.nextSibling;
215+
}
216+
217+
return false;
218+
};
219+
176220
var pruneTrailingNodes = function (container) {
177221
if (!container) {
178222
return;
179223
}
180224
181225
var isWhitespaceText = function (node) {
182-
return node && node.nodeType === 3 && (!node.textContent || !node.textContent.trim());
226+
return (
227+
node &&
228+
node.nodeType === 3 &&
229+
(!node.textContent || !node.textContent.trim())
230+
);
183231
};
184232
185233
var isTrimmableElement = function (node) {
@@ -193,7 +241,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
193241
}
194242
195243
if (tag === 'P') {
196-
return !node.textContent || !node.textContent.trim();
244+
return !hasRenderableContent(node);
197245
}
198246
199247
return false;
@@ -231,75 +279,77 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
231279
}
232280
};
233281
234-
var readRectHeight = function (element) {
235-
if (!element || typeof element.getBoundingClientRect !== 'function') {
282+
var readElementHeight = function (element) {
283+
if (!element) {
236284
return 0;
237285
}
238286
239-
var rect = element.getBoundingClientRect();
240-
return typeof rect.height === 'number' ? rect.height : 0;
241-
};
242-
243-
var readMaxValue = function (values) {
244-
var max = 0;
245-
246-
for (var index = 0; index < values.length; index += 1) {
247-
var value = values[index];
248-
if (typeof value === 'number' && value > max) {
249-
max = value;
250-
}
287+
var rectHeight = 0;
288+
if (typeof element.getBoundingClientRect === 'function') {
289+
var rect = element.getBoundingClientRect();
290+
rectHeight = rect && typeof rect.height === 'number' ? rect.height : 0;
251291
}
252292
253-
return max;
293+
return Math.max(
294+
0,
295+
rectHeight,
296+
element.scrollHeight || 0,
297+
element.offsetHeight || 0,
298+
element.clientHeight || 0
299+
);
254300
};
255301
256302
var measureHeight = function () {
257303
var html = document.documentElement;
258304
var body = document.body;
259305
var wrapper = ensureWrapper();
306+
var scrollingElement = document.scrollingElement;
260307
261308
pruneTrailingNodes(wrapper);
262309
263-
var values = [];
310+
var targets = [];
264311
265-
var collect = function (element) {
266-
if (!element) {
267-
return;
268-
}
312+
if (wrapper) {
313+
targets.push(wrapper);
314+
}
269315
270-
values.push(
271-
readRectHeight(element),
272-
element.scrollHeight || 0,
273-
element.offsetHeight || 0,
274-
element.clientHeight || 0
275-
);
276-
};
316+
if (body && targets.indexOf(body) === -1) {
317+
targets.push(body);
318+
}
277319
278-
if (wrapper) {
279-
collect(wrapper);
280-
} else {
281-
if (body) {
282-
collect(body);
283-
}
284-
if (html && html !== body) {
285-
collect(html);
286-
}
320+
if (html && targets.indexOf(html) === -1) {
321+
targets.push(html);
322+
}
323+
324+
if (
325+
scrollingElement &&
326+
scrollingElement !== body &&
327+
scrollingElement !== html &&
328+
targets.indexOf(scrollingElement) === -1
329+
) {
330+
targets.push(scrollingElement);
287331
}
288332
289-
if (!values.length) {
333+
if (!targets.length) {
290334
return 0;
291335
}
292336
293-
return Math.max(0, Math.ceil(readMaxValue(values)));
337+
var maxHeight = 0;
338+
for (var index = 0; index < targets.length; index += 1) {
339+
var value = readElementHeight(targets[index]);
340+
if (value > maxHeight) {
341+
maxHeight = value;
342+
}
343+
}
344+
345+
return Math.max(0, Math.ceil(maxHeight));
294346
};
295347
296348
var postHeight = function (height) {
297349
if (!height || height <= 0) {
298350
return;
299351
}
300352
301-
state.lastCssHeight = height;
302-
303353
var sanitized = Math.ceil(height);
304354
305355
if (!isFinite(sanitized) || sanitized <= 0) {

0 commit comments

Comments
 (0)