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

Unified Diff: lib/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebase Created June 19, 2018, 12:59 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 | lib/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterClasses.js
===================================================================
--- a/lib/filterClasses.js
+++ b/lib/filterClasses.js
@@ -79,20 +79,20 @@
/**
* Cache for known filters, maps string representation to filter objects.
* @type {Map.<string,Filter>}
*/
Filter.knownFilters = new Map();
/**
- * Regular expression that element hiding filters should match
+ * Regular expression that script filters should match
* @type {RegExp}
*/
-Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/;
+Filter.scriptRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/;
/**
* Regular expression that RegExp filters specified as RegExps should match
* @type {RegExp}
*/
Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)?$/;
/**
* Regular expression that options on a RegExp filter should match
* @type {RegExp}
@@ -112,34 +112,35 @@
* @return {Filter}
*/
Filter.fromText = function(text)
{
let filter = Filter.knownFilters.get(text);
if (filter)
return filter;
- let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null);
+ let match = text.includes("#") ? Filter.scriptRegExp.exec(text) : null;
if (match)
{
let propsMatch;
if (!match[2] &&
(propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3])))
{
// This is legacy CSS properties syntax, convert to current syntax
let prefix = match[3].substr(0, propsMatch.index);
let expression = propsMatch[2];
let suffix = match[3].substr(propsMatch.index + propsMatch[0].length);
return Filter.fromText(`${match[1]}#?#` +
`${prefix}:-abp-properties(${expression})${suffix}`);
}
- filter = ElemHideBase.fromText(
- text, match[1], match[2], match[3]
- );
+ if (match[2] == "$")
+ filter = new SnippetFilter(text, match[1], match[3]);
+ else
+ filter = ElemHideBase.fromText(text, match[1], match[2], match[3]);
}
else if (text[0] == "!")
filter = new CommentFilter(text);
else
filter = RegExpFilter.fromText(text);
Filter.knownFilters.set(filter.text, filter);
return filter;
@@ -179,22 +180,22 @@
// Remove line breaks, tabs etc
text = text.replace(/[^\S ]+/g, "");
// Don't remove spaces inside comments
if (/^ *!/.test(text))
return text.trim();
- // Special treatment for element hiding filters, right side is allowed to
- // contain spaces
- if (Filter.elemhideRegExp.test(text))
+ // Special treatment for script filters, right side is allowed to contain
+ // spaces
+ if (Filter.scriptRegExp.test(text))
{
- let [, domains, separator, selector] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text);
- return domains.replace(/ +/g, "") + separator + selector.trim();
+ let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text);
+ return domains.replace(/ +/g, "") + separator + script.trim();
}
// For most regexp filters we strip all spaces, but $csp filter options
// are allowed to contain single (non trailing) spaces.
let strippedText = text.replace(/ +/g, "");
if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
return strippedText;
@@ -982,41 +983,65 @@
}
exports.WhitelistFilter = WhitelistFilter;
WhitelistFilter.prototype = extend(RegExpFilter, {
type: "whitelist"
});
/**
- * Base class for element hiding filters
+ * Base class for script filters
* @param {string} text see {@link Filter Filter()}
* @param {string} [domains] Host names or domains the filter should be
* restricted to
- * @param {string} selector CSS selector for the HTML elements that should be
- * hidden
+ * @param {string} script Script that should be executed
* @constructor
* @augments ActiveFilter
*/
-function ElemHideBase(text, domains, selector)
+function ScriptFilter(text, domains, script)
hub 2018/06/22 20:45:39 I'm not convinced with the renaming to "ScriptFilt
Manish Jethani 2018/06/23 18:24:08 That sounds reasonable to me. Done.
{
ActiveFilter.call(this, text, domains || null);
- // Braces are being escaped to prevent CSS rule injection.
- this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D ");
+ this.script = script;
}
-exports.ElemHideBase = ElemHideBase;
+exports.ScriptFilter = ScriptFilter;
-ElemHideBase.prototype = extend(ActiveFilter, {
+ScriptFilter.prototype = extend(ActiveFilter, {
/**
* @see ActiveFilter.domainSeparator
*/
domainSeparator: ",",
/**
+ * Script that should be executed
+ * @type {string}
+ */
+ script: null
+});
+
+/*
+ * Base class for element hiding filters
+ * @param {string} text see {@link Filter Filter()}
+ * @param {string} [domains] see {@link ScriptFilter ScriptFilter()}
+ * @param {string} selector CSS selector for the HTML elements that should be
+ * hidden
+ * @constructor
+ * @augments ScriptFilter
+ */
+function ElemHideBase(text, domains, selector)
+{
+ ScriptFilter.call(this, text, domains, selector);
+
+ // Braces are being escaped to prevent CSS rule injection.
+ this.selector = this.script.replace("{", "\\7B ").replace("}", "\\7D ");
+}
+exports.ElemHideBase = ElemHideBase;
+
+ElemHideBase.prototype = extend(ScriptFilter, {
+ /**
* CSS selector for the HTML elements that should be hidden
* @type {string}
*/
selector: null
});
/**
* Creates an element hiding filter from a pre-parsed text representation
@@ -1102,8 +1127,26 @@
{
ElemHideBase.call(this, text, domains, selector);
}
exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
type: "elemhideemulation"
});
+
+/**
+ * Class for snippet filters
+ * @param {string} text see Filter()
+ * @param {string} [domains] see ScriptFilter()
+ * @param {string} script Script that should be executed
+ * @constructor
+ * @augments ScriptFilter
+ */
+function SnippetFilter(text, domains, script)
+{
+ ScriptFilter.call(this, text, domains, script);
+}
+exports.SnippetFilter = SnippetFilter;
+
+SnippetFilter.prototype = extend(ScriptFilter, {
+ type: "snippet"
+});
« no previous file with comments | « no previous file | lib/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld