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

Commit

Permalink
Handle native querySelector returning nothing
Browse files Browse the repository at this point in the history
	- Fixes #476
	- Added tests
  • Loading branch information
jcmoore committed Jul 30, 2014
1 parent 14bc904 commit 0035677
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/querySelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@
return findOne(this, selector);
}

if (root = getTreeScope(wrappedItem).root) {
if (!wrappedItem) {
// When the original query returns nothing
// we return nothing (to be consistent with the other wrapped calls)
return wrappedItem;
} else if (root = getTreeScope(wrappedItem).root) {
if (root instanceof scope.wrappers.ShadowRoot) {
// When the original query returns an element in the ShadowDOM
// we must do a manual tree traversal
Expand Down
11 changes: 11 additions & 0 deletions test/js/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ htmlSuite('Document', function() {
assert.equal(elements.length, 2);
assert.equal(elements[0], aa3);
assert.equal(elements[1], aa4);

var z = document.getElementsByTagName('z');
assert.equal(z.length, 0);
});

test('getElementsByTagNameNS', function() {
Expand Down Expand Up @@ -176,6 +179,14 @@ htmlSuite('Document', function() {
assert.equal(elements.length, 2);
assert.equal(elements[0], aa3);
assert.equal(elements[1], aa4);

var z = document.querySelectorAll('z');
assert.equal(z.length, 0);
});

skipTest('querySelector', function() {
var z = document.querySelector('z');
assert.equal(z, null);
});

test('addEventListener', function() {
Expand Down
32 changes: 32 additions & 0 deletions test/js/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ suite('Element', function() {

assert.equal(div.querySelector('b'), b);
assert.equal(sr.querySelector('b'), srb);

var z = div.querySelector('z');
assert.equal(z, null);

var z = sr.querySelector('z');
assert.equal(z, null);
});

skipTest('querySelectorAll', function() {
Expand Down Expand Up @@ -62,6 +68,12 @@ suite('Element', function() {
assert.equal(as.length, 2);
assert.equal(as[0], a3);
assert.equal(as[1], a4);

var z = div.querySelectorAll('z');
assert.equal(z.length, 0);

var z = sr.querySelectorAll('z');
assert.equal(z.length, 0);
});

skipTest('getElementsByTagName', function() {
Expand Down Expand Up @@ -93,6 +105,12 @@ suite('Element', function() {
assert.equal(as.length, 2);
assert.equal(as[0], a3);
assert.equal(as[1], a4);

var z = div.getElementsByTagName('z');
assert.equal(z.length, 0);

var z = sr.getElementsByTagName('z');
assert.equal(z.length, 0);
});

test('getElementsByTagName with colon', function() {
Expand Down Expand Up @@ -138,6 +156,20 @@ suite('Element', function() {
assert.equal(as.item(2), a3);
});

test('getElementsByTagNameNS', function() {
var div = document.createElement('div');
div.innerHTML = '<a>0</a><a>1</a>';

var sr = div.createShadowRoot();
sr.innerHTML = '<a>3</a><a>4</a>';

var z = div.getElementsByTagNameNS('NS', 'z');
assert.equal(z.length, 0);

var z = sr.getElementsByTagNameNS('NS', 'z');
assert.equal(z.length, 0);
});

skipTest('getElementsByClassName', function() {
var div = document.createElement('div');
div.innerHTML = '<span class=a>0</span><span class=a>1</span>';
Expand Down

0 comments on commit 0035677

Please sign in to comment.