Skip to content

Commit

Permalink
Support shadow DOM when reading element text. (#4230)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored and juangj committed Jul 19, 2017
1 parent c76b56d commit 9cf58f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
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') {
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

0 comments on commit 9cf58f6

Please sign in to comment.