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: Parse the regexp flag. Created Jan. 10, 2018, 6:01 a.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,42 @@
}
styles.sort();
return {
style: styles.join(" "),
subSelectors: splitSelector(rule.selectorText)
};
}
+const regexpRegexp = /^\/(.*)\/([gimuy]*)$/;
Manish Jethani 2018/01/10 11:22:15 We don't have to support all these flags, some of
hub 2018/01/10 18:27:22 let's restrict to "im" for the flags.
+
+/**
+ * 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.
+ * @param {?string} flag the regexp flag passed to the RegExp constructor:
+ * it overrides flags passed in the text argument.
+ * @return {RegExp} a RegExp object.
+ */
+function makeRegExpParameter(text, flag)
Manish Jethani 2018/01/10 11:22:14 Shouldn't the parameter be called "flags" (plural)
hub 2018/01/10 18:27:21 Done.
+{
+ let regexpString = null;
+ let match = regexpRegexp.exec(text);
+ if (match)
+ {
+ regexpString = match[1].replace("\\7B ", "{").replace("\\7D ", "}");
+ if (!flag)
Manish Jethani 2018/01/10 11:22:15 This changes the syntax of PropsSelector so that n
hub 2018/01/10 18:27:22 It is just more permissive to allow the regexp syn
+ flag = match[2];
+ }
+ else
+ regexpString = filterToRegExp(text);
+
+ return new RegExp(regexpString, flag);
Manish Jethani 2018/01/10 11:22:14 We still need to handle the error if any.
hub 2018/01/10 18:27:21 Done.
+}
+
function* evaluate(chain, index, prefix, subtree, styles)
{
if (index >= chain.length)
{
yield prefix;
return;
}
for (let [selector, element] of
@@ -219,17 +245,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 +265,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;
lainverse 2018/01/10 09:14:02 BTW, while we are at it why do we even want to yie
lainverse 2018/01/10 19:10:22 Checked. makeSelectors returns null if null is pas
}
}
};
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