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

Commit

Permalink
Merge pull request #341 from arv/get-element-by-id
Browse files Browse the repository at this point in the history
Fix issue with getElementById
  • Loading branch information
arv committed Jan 7, 2014
2 parents 20d9c5b + beda36f commit de3c061
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/wrappers/ShadowRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
var shadowHostTable = new WeakMap();
var nextOlderShadowTreeTable = new WeakMap();

var spaceCharRe = /[ \t\n\r\f]/;

function ShadowRoot(hostWrapper) {
var node = unwrap(hostWrapper.impl.ownerDocument.createDocumentFragment());
DocumentFragment.call(this, node);
Expand Down Expand Up @@ -56,7 +58,9 @@
},

getElementById: function(id) {
return this.querySelector('#' + id);
if (spaceCharRe.test(id))
return null;
return this.querySelector('[id="' + id + '"]');
}
});

Expand Down
20 changes: 20 additions & 0 deletions test/js/ShadowRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ suite('ShadowRoot', function() {
assert.equal(sr.getElementById('b'), b);
});

test('getElementById with a non CSS ID', function() {
var div = document.createElement('div');
var sr = div.createShadowRoot();
sr.innerHTML = '<a id=1 name=2></a><b id=2></b>';
var a = sr.firstChild;
var b = sr.lastChild;

assert.equal(sr.getElementById(1), a);
assert.equal(sr.getElementById(2), b);
});

test('getElementById with a non ID', function() {
var div = document.createElement('div');
var sr = div.createShadowRoot();
sr.innerHTML = '<a id="a b"></a>';
var a = sr.firstChild;

assert.isNull(sr.getElementById('a b'));
});

test('olderShadowRoot', function() {
var host = document.createElement('div');
host.innerHTML = '<a>a</a><b>b</b>';
Expand Down

0 comments on commit de3c061

Please sign in to comment.