Skip to content

Commit

Permalink
Handle errors in elementMatchesSelector, fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jun 24, 2016
1 parent 3e84352 commit 05f16ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 14 additions & 6 deletions cq-prolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,18 +1215,26 @@ function filterRulesByElementAndProp(rules, element, prop) {
});
}

var elementMatchesSelectorMethod = (function(element) {
return element.matches
|| element.mozMatchesSelector
|| element.msMatchesSelector
|| element.oMatchesSelector
|| element.webkitMatchesSelector;
})(document.createElement('div'));

/**
* @param {Element} element
* @param {string} selector
* @return {boolean}
*/
function elementMatchesSelector(element, selector) {
var func = element.matches
|| element.mozMatchesSelector
|| element.msMatchesSelector
|| element.oMatchesSelector
|| element.webkitMatchesSelector;
return func.call(element, selector);
try {
return !!elementMatchesSelectorMethod.call(element, selector);
}
catch(e) {
return false;
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@ QUnit.test('elementMatchesSelector', function(assert) {

assert.ok(elementMatchesSelector(element, 'div'), 'Simple selector');
assert.ok(elementMatchesSelector(element, '.\\:container\\(width\\>\\=100px\\)'), 'Escaped query');
assert.notOk(elementMatchesSelector(element, ''), 'Empty selector');
assert.notOk(elementMatchesSelector(element, '#1'), 'Invalid selector');
assert.notOk(elementMatchesSelector(element, '::-webkit- *'), 'Safari bug with special semivalid selector'); // Issue #26

});

Expand Down

0 comments on commit 05f16ee

Please # to comment.