Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/content/elemHideEmulation.js

Issue 29613805: Issue 6034 - :-abp-contains() accept a regular expression (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Some comments editing, Created Feb. 22, 2018, 2:05 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/common.js ('k') | test/browser/elemHideEmulation.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/elemHideEmulation.js
===================================================================
--- a/lib/content/elemHideEmulation.js
+++ b/lib/content/elemHideEmulation.js
@@ -12,17 +12,17 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
-const {filterToRegExp, splitSelector} = require("../common");
+const {textToRegExp, filterToRegExp, splitSelector} = require("../common");
let MIN_INVOCATION_INTERVAL = 3000;
const MAX_SYNCHRONOUS_PROCESSING_TIME = 50;
const abpSelectorRegexp = /:-abp-([\w-]+)\(/i;
/** Return position of node from parent.
* @param {Node} node the node to find the position of.
* @return {number} One-based index like for :nth-child(), or 0 on error.
@@ -166,16 +166,39 @@
subtree.querySelector(selector);
}
function scopedQuerySelectorAll(subtree, selector)
{
return scopedQuerySelector(subtree, selector, true);
}
+const regexpRegexp = /^\/(.*)\/([im]*)$/;
+
+/**
+ * Make a regular expression from a text argument. If it can be parsed as a
+ * regular expression, parse it and the flags.
+ * @param {string} text the text argument.
+ * @return {?RegExp} a RegExp object or null in case of error.
+ */
+function makeRegExpParameter(text)
+{
+ let [, pattern, flags] =
+ regexpRegexp.exec(text) || [undefined, textToRegExp(text)];
+
+ try
+ {
+ return new RegExp(pattern, flags);
+ }
+ catch (e)
+ {
+ }
+ return null;
+}
+
function* evaluate(chain, index, prefix, subtree, styles)
{
if (index >= chain.length)
{
yield prefix;
return;
}
for (let [selector, element] of
@@ -258,17 +281,17 @@
yield null;
}
}
}
};
function ContainsSelector(textContent)
{
- this._text = textContent;
+ this._regexp = makeRegExpParameter(textContent);
}
ContainsSelector.prototype = {
requiresHiding: true,
*getSelectors(prefix, subtree, stylesheet)
{
for (let element of this.getElements(prefix, subtree, stylesheet))
@@ -280,17 +303,17 @@
let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
prefix + "*" : prefix;
let elements = scopedQuerySelectorAll(subtree, actualPrefix);
if (elements)
{
for (let element of elements)
{
- if (element.textContent.includes(this._text))
+ if (this._regexp && this._regexp.test(element.textContent))
yield element;
else
yield null;
}
}
}
};
« no previous file with comments | « lib/common.js ('k') | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld