Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support shadow DOM when reading element text #4230

Merged
merged 3 commits into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions javascript/selenium-atoms/test/get_text_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
assertTrue(core.text.isTextPresent("exact:This is a test of"));
}

function testShadowDom() {
if (typeof ShadowRoot !== 'function') {
// TODO: Remove when all browsers support shadow dom
return;
}

var element = document.createElement('div');
var shadow;

if (element.attachShadow) {
shadow = element.attachShadow({ mode: 'open' });
shadow.innerHTML = '<p>shadow</p><slot></slot>';
} else {
shadow = element.createShadowRoot();
shadow.innerHTML = '<p>shadow</p><content></content>';
}

element.innerHTML = '<p>light</p>';

assertEquals('shadow\nlight', core.text.getText(element));
}

function testCanSearchByXPath() {
if (!(document.documentElement['selectSingleNode'] || document['evaluate'])) {
// TODO: Remove when all browsers [IE] support xpath
Expand Down
30 changes: 27 additions & 3 deletions javascript/selenium-atoms/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ goog.require('goog.dom.NodeType');
goog.require('goog.string');
goog.require('goog.userAgent');


var SHADOW_DOM_ENABLED = typeof ShadowRoot === 'function';

/**
* Attempt to normalize the text content of an element.
Expand All @@ -56,15 +56,39 @@ core.text.getTextContent_ = function(element, preformatted) {
}
return text.replace(/&nbsp/, ' ');
}
if (element.nodeType == goog.dom.NodeType.ELEMENT &&
element.nodeName != 'SCRIPT') {
if (SHADOW_DOM_ENABLED &&
element.nodeType == goog.dom.NodeType.ELEMENT &&
element.shadowRoot !== null) {
return core.text.getTextContent_(element.shadowRoot, preformatted);
}
if ((element.nodeType == goog.dom.NodeType.ELEMENT ||
element.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT) &&
element.nodeName != 'SCRIPT' &&
element.nodeName != 'STYLE') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for adding 'STYLE' here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some web components have style tags in their shadow trees. Polymer is a good example of this.

So, from what I remember, we would end up with CSS in the text content result.

var childrenPreformatted = preformatted || (element.tagName == 'PRE');
text = '';
for (var i = 0; i < element.childNodes.length; i++) {
var child = element.childNodes.item(i);
if (!child) {
continue;
}
if (SHADOW_DOM_ENABLED &&
(child.nodeName == 'CONTENT' || child.nodeName == 'SLOT')) {
var shadowChildren;
if (child.nodeName == 'CONTENT') {
shadowChildren = child.getDistributedNodes();
} else {
shadowChildren = child.assignedNodes();
}
for (var j = 0; j < shadowChildren.length; j++) {
var shadowChild = shadowChildren[j];
if (!shadowChild) {
continue;
}
text += core.text.getTextContent_(shadowChild, preformatted);
}
continue;
}
text += core.text.getTextContent_(child, childrenPreformatted);
}
// Handle block elements that introduce newlines
Expand Down