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

Commit

Permalink
Implement Document.prototype.getElementsByName
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Jun 12, 2014
1 parent 7004965 commit d62f09d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/wrappers/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
getSelection: function() {
renderAllPending();
return new Selection(originalGetSelection.call(unwrap(this)));
},
getElementsByName: function(name) {
return SelectorsInterface.querySelectorAll.call(this,
'[name=' + JSON.stringify(String(name)) + ']');
}
});

Expand Down Expand Up @@ -248,6 +252,7 @@
'createTextNode',
'elementFromPoint',
'getElementById',
'getElementsByName',
'getSelection',
]);

Expand Down
49 changes: 49 additions & 0 deletions test/js/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

suite('Element', function() {

var wrap = ShadowDOMPolyfill.wrap;
var div;

teardown(function() {
if (div && div.parentNode)
div.parentNode.removeChild(div);
div = null;
});

test('querySelector', function() {
var div = document.createElement('div');
div.innerHTML = '<a><b></b></a>';
Expand Down Expand Up @@ -202,4 +211,44 @@ suite('Element', function() {
assertArrayEqual([content], a.getDestinationInsertionPoints());
assertArrayEqual([content, contentB], b.getDestinationInsertionPoints());
});

test('getElementsByName', function() {
div = document.createElement('div');
document.body.appendChild(div);
div.innerHTML = '<span name=a>0</span><span name=a>1</span>';
var a0 = div.firstChild;
var a1 = div.lastChild;

var as = document.getElementsByName('a');
assert.instanceOf(as, NodeList);
assert.equal(as.length, 2);
assert.equal(as[0], a0);
assert.equal(as.item(0), a0);
assert.equal(as[1], a1);
assert.equal(as.item(1), a1);

var doc = wrap(document);
as = doc.getElementsByName('a');
assert.instanceOf(as, NodeList);
assert.equal(as.length, 2);
assert.equal(as[0], a0);
assert.equal(as.item(0), a0);
assert.equal(as[1], a1);
assert.equal(as.item(1), a1);

a0.setAttribute('name', '"odd"');
as = document.getElementsByName('"odd"');
assert.instanceOf(as, NodeList);
assert.equal(as.length, 1);
assert.equal(as[0], a0);
assert.equal(as.item(0), a0);

var sr = div.createShadowRoot();
sr.innerHTML = '<span name=a></span>';
as = document.getElementsByName('a');
assert.instanceOf(as, NodeList);
assert.equal(as.length, 1);
assert.equal(as[0], a1);
assert.equal(as.item(0), a1);
});
});

0 comments on commit d62f09d

Please sign in to comment.