use Element.matches instead of ugly hack

This commit is contained in:
Raymond Hill 2018-04-09 08:28:36 -04:00
parent 8071321e91
commit fb2b402940
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uBlock Origin - a browser extension to block requests. uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2016 Raymond Hill Copyright (C) 2014-2018 Raymond Hill
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -144,34 +144,6 @@ var nodeInNodeList = function(node, nodeList) {
/******************************************************************************/ /******************************************************************************/
var doesMatchSelector = function(node, selector) {
if ( !node ) {
return false;
}
if ( node.nodeType !== 1 ) {
return false;
}
if ( selector === undefined ) {
return true;
}
var parentNode = node.parentNode;
if ( !parentNode || !parentNode.setAttribute ) {
return false;
}
var doesMatch = false;
parentNode.setAttribute('uDom-32kXc6xEZA7o73AMB8vLbLct1RZOkeoO', '');
var grandpaNode = parentNode.parentNode || document;
var nl = grandpaNode.querySelectorAll('[uDom-32kXc6xEZA7o73AMB8vLbLct1RZOkeoO] > ' + selector);
var i = nl.length;
while ( doesMatch === false && i-- ) {
doesMatch = nl[i] === node;
}
parentNode.removeAttribute('uDom-32kXc6xEZA7o73AMB8vLbLct1RZOkeoO');
return doesMatch;
};
/******************************************************************************/
DOMList.prototype.nodeAt = function(i) { DOMList.prototype.nodeAt = function(i) {
return this.nodes[i] || null; return this.nodes[i] || null;
}; };
@ -230,12 +202,8 @@ DOMList.prototype.next = function(selector) {
node = this.nodes[i]; node = this.nodes[i];
while ( node.nextSibling !== null ) { while ( node.nextSibling !== null ) {
node = node.nextSibling; node = node.nextSibling;
if ( node.nodeType !== 1 ) { if ( node.nodeType !== 1 ) { continue; }
continue; if ( node.matches(selector) === false ) { continue; }
}
if ( doesMatchSelector(node, selector) === false ) {
continue;
}
addNodeToList(r, node); addNodeToList(r, node);
break; break;
} }
@ -260,7 +228,7 @@ DOMList.prototype.filter = function(filter) {
var filterFunc; var filterFunc;
if ( typeof filter === 'string' ) { if ( typeof filter === 'string' ) {
filterFunc = function() { filterFunc = function() {
return doesMatchSelector(this, filter); return this.matches(filter);
}; };
} else if ( typeof filter === 'function' ) { } else if ( typeof filter === 'function' ) {
filterFunc = filter; filterFunc = filter;
@ -286,12 +254,13 @@ DOMList.prototype.filter = function(filter) {
DOMList.prototype.ancestors = function(selector) { DOMList.prototype.ancestors = function(selector) {
var r = new DOMList(); var r = new DOMList();
var n = this.nodes.length; for ( var i = 0, n = this.nodes.length; i < n; i++ ) {
var node; var node = this.nodes[i].parentNode;
for ( var i = 0; i < n; i++ ) {
node = this.nodes[i].parentNode;
while ( node ) { while ( node ) {
if ( doesMatchSelector(node, selector) ) { if (
node instanceof Element &&
node.matches(selector)
) {
addNodeToList(r, node); addNodeToList(r, node);
} }
node = node.parentNode; node = node.parentNode;