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: Do all matches with RegExp. Created Jan. 9, 2018, 10:25 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 | « no previous file | 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
@@ -117,16 +117,37 @@
}
styles.sort();
return {
style: styles.join(" "),
subSelectors: splitSelector(rule.selectorText)
};
}
+/**
+ * Make a regular expresson from a text argument.
+ * @param {string} text the text argument.
+ * @param {?string} flag the regexp flag passed to the RegExp constructor.
+ * @return {RegExp} a RegExp object.
+ */
+function makeRegExpParameter(text, flag)
+{
+ let regexpString = null;
+ if (text.length >= 2 && text[0] == "/" &&
+ text[text.length - 1] == "/")
+ {
+ regexpString = text.slice(1, -1)
+ .replace("\\7B ", "{").replace("\\7D ", "}");
+ }
+ else
+ regexpString = filterToRegExp(text);
+
+ return new RegExp(regexpString, flag);
+}
+
function* evaluate(chain, index, prefix, subtree, styles)
{
if (index >= chain.length)
{
yield prefix;
return;
}
for (let [selector, element] of
@@ -219,17 +240,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))
@@ -239,37 +260,27 @@
*getElements(prefix, subtree, stylesheet)
{
let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
prefix + "*" : prefix;
let elements = subtree.querySelectorAll(actualPrefix);
for (let element of elements)
{
- if (element.textContent.includes(this._text))
+ if (this._regexp && this._regexp.test(element.textContent))
yield element;
else
yield null;
}
}
};
function PropsSelector(propertyExpression)
{
- let regexpString;
- if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
- propertyExpression[propertyExpression.length - 1] == "/")
- {
- regexpString = propertyExpression.slice(1, -1)
- .replace("\\7B ", "{").replace("\\7D ", "}");
- }
- else
- regexpString = filterToRegExp(propertyExpression);
-
- this._regexp = new RegExp(regexpString, "i");
+ this._regexp = makeRegExpParameter(propertyExpression, "i");
}
PropsSelector.prototype = {
preferHideWithSelector: true,
dependsOnStyles: true,
*findPropsSelectors(styles, prefix, regexp)
{
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld