From 81f9223e7a9dc8b4d980bdb347d64d4d25626e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Wed, 17 Sep 2025 20:34:47 +0200 Subject: [PATCH] [atoms] fix text node children are always considered as displayed #16284 --- javascript/atoms/dom.js | 24 +++++++++++++++++------- javascript/atoms/test/shown_test.html | 9 +++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/javascript/atoms/dom.js b/javascript/atoms/dom.js index 87a0fd559c5f8..786b36238e0ef 100644 --- a/javascript/atoms/dom.js +++ b/javascript/atoms/dom.js @@ -452,12 +452,12 @@ bot.dom.getCascadedStyle_ = function (elem, styleName) { * @param {!Element} elem The element to consider. * @param {boolean} ignoreOpacity Whether to ignore the element's opacity * when determining whether it is shown. - * @param {function(!Element):boolean} parentsDisplayedFn a function that's used - * to tell if the chain of ancestors are all shown. + * @param {function(!Element):boolean} displayedFn a function that's used + * to tell if the chain of ancestors or descendants are all shown. * @return {boolean} Whether or not the element is visible. * @private */ -bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { +bot.dom.isShown_ = function (elem, ignoreOpacity, displayedFn) { if (!bot.dom.isElement(elem)) { throw new Error('Argument to isShown must be of type Element'); } @@ -476,7 +476,7 @@ bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { var select = /**@type {Element}*/ (goog.dom.getAncestor(elem, function (e) { return bot.dom.isElement(e, goog.dom.TagName.SELECT); })); - return !!select && bot.dom.isShown_(select, true, parentsDisplayedFn); + return !!select && bot.dom.isShown_(select, true, displayedFn); } // Image map elements are shown if image that uses it is shown, and @@ -486,7 +486,7 @@ bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { return !!imageMap.image && imageMap.rect.width > 0 && imageMap.rect.height > 0 && bot.dom.isShown_( - imageMap.image, ignoreOpacity, parentsDisplayedFn); + imageMap.image, ignoreOpacity, displayedFn); } // Any hidden input is not shown. @@ -506,7 +506,7 @@ bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { return false; } - if (!parentsDisplayedFn(elem)) { + if (!displayedFn(elem)) { return false; } @@ -527,6 +527,16 @@ bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { var strokeWidth = bot.dom.getEffectiveStyle(e, 'stroke-width'); return !!strokeWidth && (parseInt(strokeWidth, 10) > 0); } + + // Any element with hidden/collapsed visibility is not shown. + var visibility = bot.dom.getEffectiveStyle(e, 'visibility'); + if (visibility == 'collapse' || visibility == 'hidden') { + return false; + } + + if (!displayedFn(e)) { + return false; + } // Zero-sized elements should still be considered to have positive size // if they have a child element or text node with positive size, unless // the element has an 'overflow' style of 'hidden'. @@ -572,7 +582,7 @@ bot.dom.isShown_ = function (elem, ignoreOpacity, parentsDisplayedFn) { */ bot.dom.isShown = function (elem, opt_ignoreOpacity) { /** - * Determines whether an element or its parents have `display: none` set + * Determines whether an element or its parents have `display: none` or similar CSS properties set * @param {!Node} e the element * @return {!boolean} */ diff --git a/javascript/atoms/test/shown_test.html b/javascript/atoms/test/shown_test.html index e29b6657f9225..f27d1cac3917a 100644 --- a/javascript/atoms/test/shown_test.html +++ b/javascript/atoms/test/shown_test.html @@ -414,6 +414,11 @@ assertFalse(isShown(content)); assertFalse(isShown(findElement({ id: 'b' }, content))); } + + function testNestedTextNode() { + var content = findElement({ id: 'nestedTextNode' }); + assertFalse(isShown(content)); + }