tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 0d69cda9f254c936ec5b8e915ae23fc88d9ac7c0
parent 1733049bed20fd96ff84a9a9f252f3b1e319b681
Author: Erik Nordin <enordin@mozilla.com>
Date:   Wed, 22 Oct 2025 16:50:27 +0000

Bug 1995314 - Handle zero-sized bounding client rect during extraction r=gregtatum,translations-reviewers

This improves the page extraction algorithm to consider nodes
with a zero-sized bounding client rect to be hidden on the page.

Differential Revision: https://phabricator.services.mozilla.com/D269235

Diffstat:
Mtoolkit/components/pageextractor/DOMExtractor.sys.mjs | 9+++++++++
Mtoolkit/components/pageextractor/tests/browser/browser_dom_extractor.js | 15+++++++++++++++
Mtoolkit/components/translations/content/translations-document.sys.mjs | 23+++++++++++++++++++----
3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/toolkit/components/pageextractor/DOMExtractor.sys.mjs b/toolkit/components/pageextractor/DOMExtractor.sys.mjs @@ -326,6 +326,15 @@ function isNodeHidden(node) { return true; } + // The element may still have a zero-sized bounding client rectangle. + const boundingClientRect = element.getBoundingClientRect(); + if ( + boundingClientRect && + (boundingClientRect.width === 0 || boundingClientRect.height === 0) + ) { + return true; + } + const { ownerGlobal } = element; if (!ownerGlobal) { // We cannot compute the style without ownerGlobal, so we will assume it is not visible. diff --git a/toolkit/components/pageextractor/tests/browser/browser_dom_extractor.js b/toolkit/components/pageextractor/tests/browser/browser_dom_extractor.js @@ -106,6 +106,21 @@ add_task( <!-- Hidden via opacity:0 --> <p style="opacity:0">Hidden via opacity:0</p> + <!-- Hidden via zero-sized block container --> + <div style="width:0; height:0; overflow:hidden"> + <span>Inline text within zero-sized block container</span> + </div> + + <!-- Hidden via zero-width block container (non-zero height only) --> + <div style="width:0; height:16px; overflow:hidden"> + <span>Inline text within zero-width (height>0) block container</span> + </div> + + <!-- Hidden via zero-height block container (non-zero width only) --> + <div style="width:16px; height:0; overflow:hidden"> + <span>Inline text within zero-height (width>0) block container</span> + </div> + <!-- Visible block within hidden inline container --> <span style="width:0; height:0; overflow:hidden"> <div>Block text within zero-sized inline container</div> diff --git a/toolkit/components/translations/content/translations-document.sys.mjs b/toolkit/components/translations/content/translations-document.sys.mjs @@ -5827,13 +5827,19 @@ class TranslationScheduler { } /** - * Returns true if an HTML element is hidden based on factors such as collapsed state and + * Returns true if a node is hidden based on factors such as collapsed state and * computed style, otherwise false. * - * @param {HTMLElement} element + * @param {Node} node * @returns {boolean} */ -function isHTMLElementHidden(element) { +function isNodeHidden(node) { + const element = getHTMLElementForStyle(node); + + if (!element) { + return true; + } + // This is a cheap and easy check that will not compute style or force reflow. if (element.hidden) { // The element is explicitly hidden. @@ -5864,6 +5870,15 @@ function isHTMLElementHidden(element) { return true; } + // The element may still have a zero-sized bounding client rectangle. + const boundingClientRect = element.getBoundingClientRect(); + if ( + boundingClientRect && + (boundingClientRect.width === 0 || boundingClientRect.height === 0) + ) { + return true; + } + const { ownerGlobal } = element; if (!ownerGlobal) { // We cannot compute the style without ownerGlobal, so we will assume it is not visible. @@ -5947,7 +5962,7 @@ function getNodeSpatialContext(node) { return {}; } - if (isHTMLElementHidden(element)) { + if (isNodeHidden(element)) { // If the element is hidden, then the spatial context is not important. return {}; }