Skip to content

Commit caaa83f

Browse files
committed
Fix "Get Text" to retrieve the correct value for ShadowRoot using slot.
1 parent 1b66415 commit caaa83f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

javascript/atoms/dom.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,9 @@ bot.dom.appendVisibleTextLinesFromNodeInComposedDom_ = function(
13251325
} else {
13261326
shadowChildren = contentElem.assignedNodes();
13271327
}
1328-
goog.array.forEach(shadowChildren, function(node) {
1328+
const childrenToTraverse =
1329+
shadowChildren.length > 0 ? shadowChildren : contentElem.childNodes;
1330+
goog.array.forEach(childrenToTraverse, function (node) {
13291331
bot.dom.appendVisibleTextLinesFromNodeInComposedDom_(
13301332
node, lines, shown, whitespace, textTransform);
13311333
});

javascript/atoms/test/text_shadow_test.html

+28-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,31 @@
7171

7272
let customEl = findElement({ tagName: 'open-shadow-element' });
7373
let text = getText(customEl);
74-
assertEquals(text, 'full link text');
74+
assertEquals('full link text', text);
75+
}
76+
77+
function testTextForSlotValueInShadowDOM() {
78+
setCustomElement('<slot><span>full</span></slot>');
79+
80+
let customEl = findElement({ id: 'custom-text' });
81+
let text = getText(customEl);
82+
assertEquals('custom', text);
83+
}
84+
85+
function testTextForSlotAndExtraValueInShadowDOM() {
86+
setCustomElement('<slot><span>full</span></slot> text');
87+
88+
let customEl = findElement({ id: 'custom-text' });
89+
let text = getText(customEl);
90+
assertEquals('custom text', text);
91+
}
92+
93+
function testTextForDefaultSlotValueInShadowDOM() {
94+
setCustomElement('<slot><span>full</span></slot> text');
95+
96+
let customEl = findElement({ tagName: 'open-shadow-element' });
97+
let text = getText(customEl);
98+
assertEquals('full text', text);
7599
}
76100

77101
function testHiddenTextInShadowDOM() {
@@ -81,21 +105,22 @@
81105

82106
let customEl = findElement({ tagName: 'open-shadow-element' });
83107
let text = getText(customEl);
84-
assertEquals(text, 'full text');
108+
assertEquals('full text', text);
85109
}
86110

87111
function testTextForElementWithinClosedShadowDOM() {
88112
setCustomElement('<a href=# id=linkText>full link text</a>', 'closed');
89113
let customEl = findElement({ tagName: 'closed-shadow-element' });
90114
let innerEl = findElement({ css: 'a' }, customEl._shadowRoot);
91115
let text = getText(innerEl);
92-
assertEquals(text, 'full link text');
116+
assertEquals('full link text', text);
93117
}
94118
</script>
95119
</head>
96120

97121
<body>
98122
<open-shadow-element></open-shadow-element>
123+
<open-shadow-element id="custom-text">custom</open-shadow-element>
99124
<closed-shadow-element></closed-shadow-element>
100125
</body>
101126

0 commit comments

Comments
 (0)