Merge pull request #421 from nazar-pc/shadowRoot.getSelection-fix

`shadowRoot.getSelection()` method added since it is present in Chromium
This commit is contained in:
Daniel Freedman
2015-10-27 15:51:06 -07:00
3 changed files with 31 additions and 1 deletions

View File

@@ -46,7 +46,9 @@
unsafeUnwrap(this).removeRange(unwrap(range));
},
selectAllChildren: function(node) {
unsafeUnwrap(this).selectAllChildren(unwrapIfNeeded(node));
unsafeUnwrap(this).selectAllChildren(
node instanceof ShadowRoot ? unsafeUnwrap(node.host) : unwrapIfNeeded(node)
);
},
toString: function() {
return unsafeUnwrap(this).toString();

View File

@@ -68,6 +68,10 @@
elementFromPoint: function(x, y) {
return elementFromPoint(this, this.ownerDocument, x, y);
},
getSelection: function() {
return document.getSelection();
}
});
scope.wrappers.ShadowRoot = ShadowRoot;

View File

@@ -36,6 +36,30 @@ suite('ShadowRoot', function() {
assert.equal(sr2.elementFromPoint(5, 5), null);
});
test('getSelection', function() {
div = document.body.appendChild(document.createElement('div'));
var sr = div.createShadowRoot();
sr.innerHTML = '<a>a</a><b>b</b><c>c</c>';
var selection = sr.getSelection();
selection.selectAllChildren(sr);
assert.equal(selection.toString(), 'abc');
assert.isFalse(selection.isCollapsed);
assert.equal(selection.rangeCount, 1);
// https://code.google.com/p/chromium/issues/detail?id=336821
if (/WebKit/.test(navigator.userAgent))
return;
assert.equal(selection.anchorNode, div);
assert.equal(selection.anchorOffset, 0);
assert.equal(selection.focusNode, div);
assert.equal(selection.focusOffset, 3);
});
test('olderShadowRoot', function() {
var host = document.createElement('div');
host.innerHTML = '<a>a</a><b>b</b>';