From fb2b4029405f8e58e2e04c1660e8d3ecaed23268 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 9 Apr 2018 08:28:36 -0400 Subject: [PATCH] use Element.matches instead of ugly hack --- src/js/udom.js | 51 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/src/js/udom.js b/src/js/udom.js index a53a4afe7..dc85e6b21 100644 --- a/src/js/udom.js +++ b/src/js/udom.js @@ -1,7 +1,7 @@ /******************************************************************************* 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 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) { return this.nodes[i] || null; }; @@ -230,12 +202,8 @@ DOMList.prototype.next = function(selector) { node = this.nodes[i]; while ( node.nextSibling !== null ) { node = node.nextSibling; - if ( node.nodeType !== 1 ) { - continue; - } - if ( doesMatchSelector(node, selector) === false ) { - continue; - } + if ( node.nodeType !== 1 ) { continue; } + if ( node.matches(selector) === false ) { continue; } addNodeToList(r, node); break; } @@ -260,7 +228,7 @@ DOMList.prototype.filter = function(filter) { var filterFunc; if ( typeof filter === 'string' ) { filterFunc = function() { - return doesMatchSelector(this, filter); + return this.matches(filter); }; } else if ( typeof filter === 'function' ) { filterFunc = filter; @@ -286,12 +254,13 @@ DOMList.prototype.filter = function(filter) { DOMList.prototype.ancestors = function(selector) { var r = new DOMList(); - var n = this.nodes.length; - var node; - for ( var i = 0; i < n; i++ ) { - node = this.nodes[i].parentNode; + for ( var i = 0, n = this.nodes.length; i < n; i++ ) { + var node = this.nodes[i].parentNode; while ( node ) { - if ( doesMatchSelector(node, selector) ) { + if ( + node instanceof Element && + node.matches(selector) + ) { addNodeToList(r, node); } node = node.parentNode;