Skip to content

Commit

Permalink
Fix "Get Text" to retrieve the correct value for ShadowRoot using slo…
Browse files Browse the repository at this point in the history
…t. (#13218)

Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
whimboo and diemol authored Jan 20, 2024
1 parent f83765d commit 21560a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,9 @@ bot.dom.appendVisibleTextLinesFromNodeInComposedDom_ = function(
} else {
shadowChildren = contentElem.assignedNodes();
}
goog.array.forEach(shadowChildren, function(node) {
const childrenToTraverse =
shadowChildren.length > 0 ? shadowChildren : contentElem.childNodes;
goog.array.forEach(childrenToTraverse, function (node) {
bot.dom.appendVisibleTextLinesFromNodeInComposedDom_(
node, lines, shown, whitespace, textTransform);
});
Expand Down
31 changes: 28 additions & 3 deletions javascript/atoms/test/text_shadow_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,31 @@

let customEl = findElement({ tagName: 'open-shadow-element' });
let text = getText(customEl);
assertEquals(text, 'full link text');
assertEquals('full link text', text);
}

function testTextForSlotValueInShadowDOM() {
setCustomElement('<slot><span>full</span></slot>');

let customEl = findElement({ id: 'custom-text' });
let text = getText(customEl);
assertEquals('custom', text);
}

function testTextForSlotAndExtraValueInShadowDOM() {
setCustomElement('<slot><span>full</span></slot> text');

let customEl = findElement({ id: 'custom-text' });
let text = getText(customEl);
assertEquals('custom text', text);
}

function testTextForDefaultSlotValueInShadowDOM() {
setCustomElement('<slot><span>full</span></slot> text');

let customEl = findElement({ tagName: 'open-shadow-element' });
let text = getText(customEl);
assertEquals('full text', text);
}

function testHiddenTextInShadowDOM() {
Expand All @@ -81,21 +105,22 @@

let customEl = findElement({ tagName: 'open-shadow-element' });
let text = getText(customEl);
assertEquals(text, 'full text');
assertEquals('full text', text);
}

function testTextForElementWithinClosedShadowDOM() {
setCustomElement('<a href=# id=linkText>full link text</a>', 'closed');
let customEl = findElement({ tagName: 'closed-shadow-element' });
let innerEl = findElement({ css: 'a' }, customEl._shadowRoot);
let text = getText(innerEl);
assertEquals(text, 'full link text');
assertEquals('full link text', text);
}
</script>
</head>

<body>
<open-shadow-element></open-shadow-element>
<open-shadow-element id="custom-text">custom</open-shadow-element>
<closed-shadow-element></closed-shadow-element>
</body>

Expand Down

0 comments on commit 21560a4

Please sign in to comment.