Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit beda36f

Browse files
committed
Fix issue with getElementById
1 parent 20d9c5b commit beda36f

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/wrappers/ShadowRoot.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
var shadowHostTable = new WeakMap();
1717
var nextOlderShadowTreeTable = new WeakMap();
1818

19+
var spaceCharRe = /[ \t\n\r\f]/;
20+
1921
function ShadowRoot(hostWrapper) {
2022
var node = unwrap(hostWrapper.impl.ownerDocument.createDocumentFragment());
2123
DocumentFragment.call(this, node);
@@ -56,7 +58,9 @@
5658
},
5759

5860
getElementById: function(id) {
59-
return this.querySelector('#' + id);
61+
if (spaceCharRe.test(id))
62+
return null;
63+
return this.querySelector('[id="' + id + '"]');
6064
}
6165
});
6266

test/js/ShadowRoot.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ suite('ShadowRoot', function() {
3737
assert.equal(sr.getElementById('b'), b);
3838
});
3939

40+
test('getElementById with a non CSS ID', function() {
41+
var div = document.createElement('div');
42+
var sr = div.createShadowRoot();
43+
sr.innerHTML = '<a id=1 name=2></a><b id=2></b>';
44+
var a = sr.firstChild;
45+
var b = sr.lastChild;
46+
47+
assert.equal(sr.getElementById(1), a);
48+
assert.equal(sr.getElementById(2), b);
49+
});
50+
51+
test('getElementById with a non ID', function() {
52+
var div = document.createElement('div');
53+
var sr = div.createShadowRoot();
54+
sr.innerHTML = '<a id="a b"></a>';
55+
var a = sr.firstChild;
56+
57+
assert.isNull(sr.getElementById('a b'));
58+
});
59+
4060
test('olderShadowRoot', function() {
4161
var host = document.createElement('div');
4262
host.innerHTML = '<a>a</a><b>b</b>';

0 commit comments

Comments
 (0)