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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created July 6, 2018, 1:36 p.m.
Right Patch Set: Improve comment formatting Created July 11, 2018, 1:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/filterListener.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 text = text.replace(/[^\S ]+/g, ""); 179 text = text.replace(/[^\S ]+/g, "");
180 180
181 // Don't remove spaces inside comments 181 // Don't remove spaces inside comments
182 if (/^ *!/.test(text)) 182 if (/^ *!/.test(text))
183 return text.trim(); 183 return text.trim();
184 184
185 // Special treatment for content filters, right side is allowed to contain 185 // Special treatment for content filters, right side is allowed to contain
186 // spaces 186 // spaces
187 if (Filter.contentRegExp.test(text)) 187 if (Filter.contentRegExp.test(text))
188 { 188 {
189 let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text);
190 return domains.replace(/ +/g, "") + separator + script.trim(); 190 return domains.replace(/ +/g, "") + separator + body.trim();
191 } 191 }
192 192
193 // For most regexp filters we strip all spaces, but $csp filter options 193 // For most regexp filters we strip all spaces, but $csp filter options
194 // are allowed to contain single (non trailing) spaces. 194 // are allowed to contain single (non trailing) spaces.
195 let strippedText = text.replace(/ +/g, ""); 195 let strippedText = text.replace(/ +/g, "");
196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText))
197 return strippedText; 197 return strippedText;
198 198
199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText);
200 if (!optionsMatch) 200 if (!optionsMatch)
(...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after
990 990
991 WhitelistFilter.prototype = extend(RegExpFilter, { 991 WhitelistFilter.prototype = extend(RegExpFilter, {
992 type: "whitelist" 992 type: "whitelist"
993 }); 993 });
994 994
995 /** 995 /**
996 * Base class for content filters 996 * Base class for content filters
997 * @param {string} text see {@link Filter Filter()} 997 * @param {string} text see {@link Filter Filter()}
998 * @param {string} [domains] Host names or domains the filter should be 998 * @param {string} [domains] Host names or domains the filter should be
999 * restricted to 999 * restricted to
1000 * @param {string} script Script that should be executed 1000 * @param {string} body The body of the filter
1001 * @constructor 1001 * @constructor
1002 * @augments ActiveFilter 1002 * @augments ActiveFilter
1003 */ 1003 */
1004 function ContentFilter(text, domains, script) 1004 function ContentFilter(text, domains, body)
1005 { 1005 {
1006 ActiveFilter.call(this, text, domains || null); 1006 ActiveFilter.call(this, text, domains || null);
1007 1007
1008 this.script = script; 1008 this.body = body;
1009 } 1009 }
1010 exports.ContentFilter = ContentFilter; 1010 exports.ContentFilter = ContentFilter;
1011 1011
1012 ContentFilter.prototype = extend(ActiveFilter, { 1012 ContentFilter.prototype = extend(ActiveFilter, {
1013 /** 1013 /**
1014 * @see ActiveFilter.domainSeparator 1014 * @see ActiveFilter.domainSeparator
1015 */ 1015 */
1016 domainSeparator: ",", 1016 domainSeparator: ",",
1017 1017
1018 /** 1018 /**
1019 * Script that should be executed 1019 * The body of the filter
1020 * @type {string} 1020 * @type {string}
1021 */ 1021 */
1022 script: null 1022 body: null
1023 }); 1023 });
1024 1024
1025 /** 1025 /**
1026 * Creates a content filter from a pre-parsed text representation 1026 * Creates a content filter from a pre-parsed text representation
1027 * 1027 *
1028 * @param {string} text same as in Filter() 1028 * @param {string} text same as in Filter()
1029 * @param {string} [domains] 1029 * @param {string} [domains]
1030 * domains part of the text representation 1030 * domains part of the text representation
1031 * @param {string} [type] 1031 * @param {string} [type]
1032 * rule type, either empty or @ (exception) or ? (emulation rule) or 1032 * rule type, either:
1033 * $ (snippet) 1033 * <li>"" for an element hiding filter</li>
Manish Jethani 2018/07/11 13:04:26 Using just "-" doesn't work in the HTML version, b
kzar 2018/07/11 17:17:52 Acknowledged.
1034 * @param {string} script 1034 * <li>"@" for an element hiding exception filter</li>
1035 * script part of the text representation, either a CSS selector or a snippet 1035 * <li>"?" for an element hiding emulation filter</li>
1036 * <li>"$" for a snippet filter</li>
1037 * @param {string} body
1038 * body part of the text representation, either a CSS selector or a snippet
1036 * script 1039 * script
1037 * @return {ElemHideFilter|ElemHideException| 1040 * @return {ElemHideFilter|ElemHideException|
1038 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} 1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter}
1039 */ 1042 */
1040 ContentFilter.fromText = function(text, domains, type, script) 1043 ContentFilter.fromText = function(text, domains, type, body)
1041 { 1044 {
1042 // We don't allow content filters which have any empty domains. 1045 // We don't allow content filters which have any empty domains.
1043 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if 1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if
1044 // that changes this must be changed too. 1047 // that changes this must be changed too.
1045 if (domains && /(^|,)~?(,|$)/.test(domains)) 1048 if (domains && /(^|,)~?(,|$)/.test(domains))
1046 return new InvalidFilter(text, "filter_invalid_domain"); 1049 return new InvalidFilter(text, "filter_invalid_domain");
1047 1050
1048 if (type == "@") 1051 if (type == "@")
1049 return new ElemHideException(text, domains, script); 1052 return new ElemHideException(text, domains, body);
1050 1053
1051 if (type == "$") 1054 if (type == "$")
1052 return new SnippetFilter(text, domains, script); 1055 return new SnippetFilter(text, domains, body);
1053 1056
1054 if (type == "?") 1057 if (type == "?")
1055 { 1058 {
1056 // Element hiding emulation filters are inefficient so we need to make sure 1059 // Element hiding emulation filters are inefficient so we need to make sure
1057 // that they're only applied if they specify active domains 1060 // that they're only applied if they specify active domains
1058 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) 1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains))
1059 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); 1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain");
1060 1063
1061 return new ElemHideEmulationFilter(text, domains, script); 1064 return new ElemHideEmulationFilter(text, domains, body);
1062 } 1065 }
1063 1066
1064 return new ElemHideFilter(text, domains, script); 1067 return new ElemHideFilter(text, domains, body);
1065 }; 1068 };
1066 1069
1067 /* 1070 /**
1068 * Base class for element hiding filters 1071 * Base class for element hiding filters
1069 * @param {string} text see {@link Filter Filter()} 1072 * @param {string} text see {@link Filter Filter()}
1070 * @param {string} [domains] see {@link ContentFilter ContentFilter()} 1073 * @param {string} [domains] see {@link ContentFilter ContentFilter()}
1071 * @param {string} selector CSS selector for the HTML elements that should be 1074 * @param {string} selector CSS selector for the HTML elements that should be
1072 * hidden 1075 * hidden
1073 * @constructor 1076 * @constructor
1074 * @augments ContentFilter 1077 * @augments ContentFilter
1075 */ 1078 */
1076 function ElemHideBase(text, domains, selector) 1079 function ElemHideBase(text, domains, selector)
1077 { 1080 {
1078 ContentFilter.call(this, text, domains, selector); 1081 ContentFilter.call(this, text, domains, selector);
1079 } 1082 }
1080 exports.ElemHideBase = ElemHideBase; 1083 exports.ElemHideBase = ElemHideBase;
1081 1084
1082 ElemHideBase.prototype = extend(ContentFilter, { 1085 ElemHideBase.prototype = extend(ContentFilter, {
1083 /** 1086 /**
1084 * CSS selector for the HTML elements that should be hidden 1087 * CSS selector for the HTML elements that should be hidden
1085 * @type {string} 1088 * @type {string}
1086 */ 1089 */
1087 get selector() 1090 get selector()
1088 { 1091 {
1089 // Braces are being escaped to prevent CSS rule injection. 1092 // Braces are being escaped to prevent CSS rule injection.
1090 return this.script.replace("{", "\\7B ").replace("}", "\\7D "); 1093 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1091 } 1094 }
1092 }); 1095 });
1093 1096
1094 /** 1097 /**
1095 * Class for element hiding filters 1098 * Class for element hiding filters
1096 * @param {string} text see {@link Filter Filter()} 1099 * @param {string} text see {@link Filter Filter()}
1097 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1098 * @param {string} selector see {@link ElemHideBase ElemHideBase()} 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1099 * @constructor 1102 * @constructor
1100 * @augments ElemHideBase 1103 * @augments ElemHideBase
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 * @constructor 1156 * @constructor
1154 * @augments ContentFilter 1157 * @augments ContentFilter
1155 */ 1158 */
1156 function SnippetFilter(text, domains, script) 1159 function SnippetFilter(text, domains, script)
1157 { 1160 {
1158 ContentFilter.call(this, text, domains, script); 1161 ContentFilter.call(this, text, domains, script);
1159 } 1162 }
1160 exports.SnippetFilter = SnippetFilter; 1163 exports.SnippetFilter = SnippetFilter;
1161 1164
1162 SnippetFilter.prototype = extend(ContentFilter, { 1165 SnippetFilter.prototype = extend(ContentFilter, {
1163 type: "snippet" 1166 type: "snippet",
1164 }); 1167
1168 /**
1169 * Script that should be executed
1170 * @type {string}
1171 */
1172 get script()
1173 {
1174 return this.body;
1175 }
1176 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld