From beda36fdb6af622bb26bb21d5e49d0b99c755c05 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Tue, 7 Jan 2014 10:36:56 -0500 Subject: [PATCH] Fix issue with getElementById --- src/wrappers/ShadowRoot.js | 6 +++++- test/js/ShadowRoot.js | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wrappers/ShadowRoot.js b/src/wrappers/ShadowRoot.js index 24416b1..31ae047 100644 --- a/src/wrappers/ShadowRoot.js +++ b/src/wrappers/ShadowRoot.js @@ -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); @@ -56,7 +58,9 @@ }, getElementById: function(id) { - return this.querySelector('#' + id); + if (spaceCharRe.test(id)) + return null; + return this.querySelector('[id="' + id + '"]'); } }); diff --git a/test/js/ShadowRoot.js b/test/js/ShadowRoot.js index acb2537..85cdbc4 100644 --- a/test/js/ShadowRoot.js +++ b/test/js/ShadowRoot.js @@ -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 = ''; + 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 = ''; + var a = sr.firstChild; + + assert.isNull(sr.getElementById('a b')); + }); + test('olderShadowRoot', function() { var host = document.createElement('div'); host.innerHTML = 'ab';