Skip to content

Commit

Permalink
fix(matches-selector): don't call matches function if none exist on t…
Browse files Browse the repository at this point in the history
…he element (#1613)

* fix(find-up): fix IE11 to not pass document element to matchesSelector

* fix test
  • Loading branch information
straker authored Jun 6, 2019
1 parent 69a9c3b commit 7581592
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/commons/dom/find-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ dom.findUpVirtual = function(element, target) {
parent !== document.documentElement
);

if (!parent) {
return null;
}

if (!axe.utils.matchesSelector(parent, target)) {
return null;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/core/utils/element-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ axe.utils.matchesSelector = (function() {
method = getMethod(node);
}

return node[method](selector);
if (node[method]) {
return node[method](selector);
}

return false;
};
})();
18 changes: 18 additions & 0 deletions test/core/utils/element-matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ describe('utils.matchesSelector', function() {

fixture.innerHTML = '';
});

it('should return false if the element does not have a matching method', function() {
var target,
fixture = document.getElementById('fixture');

fixture.innerHTML = '<div id="test">Hi</div>';
target = document.getElementById('test');

target.matches = null;
target.matchesSelector = null;
target.mozMatchesSelector = null;
target.webkitMatchesSelector = null;
target.msMatchesSelector = null;

assert.isFalse(matchesSelector(target, '#test'));

fixture.innerHTML = '';
});
});

0 comments on commit 7581592

Please sign in to comment.