From 5bc763d319c1beda32d5b9f951115d4368fe935d Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Fri, 3 Apr 2026 11:14:25 +0800 Subject: [PATCH] fix(simphtml): improve bounding box calculation for transparent containers by using children union rect --- simphtml.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/simphtml.py b/simphtml.py index c509383..b7865dc 100644 --- a/simphtml.py +++ b/simphtml.py @@ -73,8 +73,29 @@ function createEnhancedDOMCopy() { const hasValidChildren = nonTextChildren.length > 0; if (hasValidChildren) { - const maxC = nonTextChildren.map(c => nodeInfo.get(c)).filter(i => i?.isVisible).sort((a, b) => b.area - a.area)[0]; - if (maxC && maxC.area > 10000 && (!isVisible || maxC.area > info.area * 5)) info = maxC; + const childrenInfos = nonTextChildren.map(c => nodeInfo.get(c)).filter(i => i && i.rect && i.rect.width > 0 && i.rect.height > 0); + const bgAlpha = (() => { + const c = style.backgroundColor; + if (!c || c === 'transparent') return 0; + const m = c.match(/rgba?\([^)]+,\s*([\d.]+)\)/); + return m ? parseFloat(m[1]) : 1; + })(); + const hasVisualBg = bgAlpha > 0.1 || style.backgroundImage !== 'none' || (style.backdropFilter && style.backdropFilter !== 'none') || style.boxShadow !== 'none'; + + if (!hasVisualBg && childrenInfos.length > 0) { + let minL = Infinity, minT = Infinity, maxR = -Infinity, maxB = -Infinity; + for (const cInfo of childrenInfos) { + minL = Math.min(minL, cInfo.rect.left); + minT = Math.min(minT, cInfo.rect.top); + maxR = Math.max(maxR, cInfo.rect.right); + maxB = Math.max(maxB, cInfo.rect.bottom); + } + info.rect = { left: minL, top: minT, right: maxR, bottom: maxB, width: maxR - minL, height: maxB - minT }; + info.area = info.rect.width * info.rect.height; + } else { + const maxC = childrenInfos.filter(i => i.isVisible).sort((a, b) => b.area - a.area)[0]; + if (maxC && maxC.area > 10000 && (!isVisible || maxC.area > info.area * 5)) info = maxC; + } } nodeInfo.set(clone, info);