Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) |
126 { | 126 { |
127 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax |
128 let prefix = match[3].substr(0, propsMatch.index); | 128 let prefix = match[3].substr(0, propsMatch.index); |
129 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; |
130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); |
131 return Filter.fromText(`${match[1]}#?#` + | 131 return Filter.fromText(`${match[1]}#?#` + |
132 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); |
133 } | 133 } |
134 | 134 |
135 if (match[2] == "$") | 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); |
136 filter = new SnippetFilter(text, match[1], match[3]); | |
137 else | |
138 filter = ElemHideBase.fromText(text, match[1], match[2], match[3]); | |
hub
2018/07/05 08:40:14
I'm wonder if we shouldn't have this in ContentFil
Manish Jethani
2018/07/06 13:32:15
Yeah, this makes sense. Done.
| |
139 } | 136 } |
140 else if (text[0] == "!") | 137 else if (text[0] == "!") |
141 filter = new CommentFilter(text); | 138 filter = new CommentFilter(text); |
142 else | 139 else |
143 filter = RegExpFilter.fromText(text); | 140 filter = RegExpFilter.fromText(text); |
144 | 141 |
145 Filter.knownFilters.set(filter.text, filter); | 142 Filter.knownFilters.set(filter.text, filter); |
146 return filter; | 143 return filter; |
147 }; | 144 }; |
148 | 145 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 text = text.replace(/[^\S ]+/g, ""); | 179 text = text.replace(/[^\S ]+/g, ""); |
183 | 180 |
184 // Don't remove spaces inside comments | 181 // Don't remove spaces inside comments |
185 if (/^ *!/.test(text)) | 182 if (/^ *!/.test(text)) |
186 return text.trim(); | 183 return text.trim(); |
187 | 184 |
188 // Special treatment for content filters, right side is allowed to contain | 185 // Special treatment for content filters, right side is allowed to contain |
189 // spaces | 186 // spaces |
190 if (Filter.contentRegExp.test(text)) | 187 if (Filter.contentRegExp.test(text)) |
191 { | 188 { |
192 let [, domains, separator, script] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); | 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); |
193 return domains.replace(/ +/g, "") + separator + script.trim(); | 190 return domains.replace(/ +/g, "") + separator + body.trim(); |
194 } | 191 } |
195 | 192 |
196 // 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 |
197 // are allowed to contain single (non trailing) spaces. | 194 // are allowed to contain single (non trailing) spaces. |
198 let strippedText = text.replace(/ +/g, ""); | 195 let strippedText = text.replace(/ +/g, ""); |
199 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) |
200 return strippedText; | 197 return strippedText; |
201 | 198 |
202 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); |
203 if (!optionsMatch) | 200 if (!optionsMatch) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 */ | 391 */ |
395 domainSourceIsLowerCase: false, | 392 domainSourceIsLowerCase: false, |
396 | 393 |
397 /** | 394 /** |
398 * Map containing domains that this filter should match on/not match | 395 * Map containing domains that this filter should match on/not match |
399 * on or null if the filter should match on all domains | 396 * on or null if the filter should match on all domains |
400 * @type {?Map.<string,boolean>} | 397 * @type {?Map.<string,boolean>} |
401 */ | 398 */ |
402 get domains() | 399 get domains() |
403 { | 400 { |
401 let prop = Object.getOwnPropertyDescriptor(this, "_domains"); | |
402 if (prop) | |
403 { | |
404 let {value} = prop; | |
405 return typeof value == "string" ? | |
406 new Map([[value, true], ["", false]]) : value; | |
407 } | |
408 | |
404 let domains = null; | 409 let domains = null; |
405 | 410 |
406 if (this.domainSource) | 411 if (this.domainSource) |
407 { | 412 { |
408 let source = this.domainSource; | 413 let source = this.domainSource; |
409 if (!this.domainSourceIsLowerCase) | 414 if (!this.domainSourceIsLowerCase) |
410 { | 415 { |
411 // RegExpFilter already have lowercase domains | 416 // RegExpFilter already have lowercase domains |
412 source = source.toLowerCase(); | 417 source = source.toLowerCase(); |
413 } | 418 } |
414 let list = source.split(this.domainSeparator); | 419 let list = source.split(this.domainSeparator); |
415 if (list.length == 1 && list[0][0] != "~") | 420 if (list.length == 1 && list[0][0] != "~") |
416 { | 421 { |
417 // Fast track for the common one-domain scenario | 422 // Fast track for the common one-domain scenario |
418 domains = new Map([["", false], [list[0], true]]); | 423 domains = list[0]; |
419 } | 424 } |
420 else | 425 else |
421 { | 426 { |
422 let hasIncludes = false; | 427 let hasIncludes = false; |
423 for (let i = 0; i < list.length; i++) | 428 for (let i = 0; i < list.length; i++) |
424 { | 429 { |
425 let domain = list[i]; | 430 let domain = list[i]; |
426 if (domain == "") | 431 if (domain == "") |
427 continue; | 432 continue; |
428 | 433 |
(...skipping 14 matching lines...) Expand all Loading... | |
443 | 448 |
444 domains.set(domain, include); | 449 domains.set(domain, include); |
445 } | 450 } |
446 if (domains) | 451 if (domains) |
447 domains.set("", !hasIncludes); | 452 domains.set("", !hasIncludes); |
448 } | 453 } |
449 | 454 |
450 this.domainSource = null; | 455 this.domainSource = null; |
451 } | 456 } |
452 | 457 |
453 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); | 458 Object.defineProperty(this, "_domains", {value: domains}); |
454 return this.domains; | 459 return this.domains; |
455 }, | 460 }, |
456 | 461 |
457 /** | 462 /** |
458 * Array containing public keys of websites that this filter should apply to | 463 * Array containing public keys of websites that this filter should apply to |
459 * @type {?string[]} | 464 * @type {?string[]} |
460 */ | 465 */ |
461 sitekeys: null, | 466 sitekeys: null, |
462 | 467 |
463 /** | 468 /** |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
985 | 990 |
986 WhitelistFilter.prototype = extend(RegExpFilter, { | 991 WhitelistFilter.prototype = extend(RegExpFilter, { |
987 type: "whitelist" | 992 type: "whitelist" |
988 }); | 993 }); |
989 | 994 |
990 /** | 995 /** |
991 * Base class for content filters | 996 * Base class for content filters |
992 * @param {string} text see {@link Filter Filter()} | 997 * @param {string} text see {@link Filter Filter()} |
993 * @param {string} [domains] Host names or domains the filter should be | 998 * @param {string} [domains] Host names or domains the filter should be |
994 * restricted to | 999 * restricted to |
995 * @param {string} script Script that should be executed | 1000 * @param {string} body The body of the filter |
996 * @constructor | 1001 * @constructor |
997 * @augments ActiveFilter | 1002 * @augments ActiveFilter |
998 */ | 1003 */ |
999 function ContentFilter(text, domains, script) | 1004 function ContentFilter(text, domains, body) |
1000 { | 1005 { |
1001 ActiveFilter.call(this, text, domains || null); | 1006 ActiveFilter.call(this, text, domains || null); |
1002 | 1007 |
1003 this.script = script; | 1008 this.body = body; |
1004 } | 1009 } |
1005 exports.ContentFilter = ContentFilter; | 1010 exports.ContentFilter = ContentFilter; |
1006 | 1011 |
1007 ContentFilter.prototype = extend(ActiveFilter, { | 1012 ContentFilter.prototype = extend(ActiveFilter, { |
1008 /** | 1013 /** |
1009 * @see ActiveFilter.domainSeparator | 1014 * @see ActiveFilter.domainSeparator |
1010 */ | 1015 */ |
1011 domainSeparator: ",", | 1016 domainSeparator: ",", |
1012 | 1017 |
1013 /** | 1018 /** |
1014 * Script that should be executed | 1019 * The body of the filter |
1015 * @type {string} | 1020 * @type {string} |
1016 */ | 1021 */ |
1017 script: null | 1022 body: null |
1018 }); | 1023 }); |
1019 | 1024 |
1020 /* | 1025 /** |
1026 * Creates a content filter from a pre-parsed text representation | |
1027 * | |
1028 * @param {string} text same as in Filter() | |
1029 * @param {string} [domains] | |
1030 * domains part of the text representation | |
1031 * @param {string} [type] | |
1032 * rule type, either: | |
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 * <li>"@" for an element hiding exception filter</li> | |
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 | |
1039 * script | |
1040 * @return {ElemHideFilter|ElemHideException| | |
1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} | |
1042 */ | |
1043 ContentFilter.fromText = function(text, domains, type, body) | |
1044 { | |
1045 // We don't allow content filters which have any empty domains. | |
1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if | |
1047 // that changes this must be changed too. | |
1048 if (domains && /(^|,)~?(,|$)/.test(domains)) | |
1049 return new InvalidFilter(text, "filter_invalid_domain"); | |
1050 | |
1051 if (type == "@") | |
1052 return new ElemHideException(text, domains, body); | |
1053 | |
1054 if (type == "$") | |
1055 return new SnippetFilter(text, domains, body); | |
1056 | |
1057 if (type == "?") | |
1058 { | |
1059 // Element hiding emulation filters are inefficient so we need to make sure | |
1060 // that they're only applied if they specify active domains | |
1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | |
1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | |
1063 | |
1064 return new ElemHideEmulationFilter(text, domains, body); | |
1065 } | |
1066 | |
1067 return new ElemHideFilter(text, domains, body); | |
1068 }; | |
1069 | |
1070 /** | |
1021 * Base class for element hiding filters | 1071 * Base class for element hiding filters |
1022 * @param {string} text see {@link Filter Filter()} | 1072 * @param {string} text see {@link Filter Filter()} |
1023 * @param {string} [domains] see {@link ContentFilter ContentFilter()} | 1073 * @param {string} [domains] see {@link ContentFilter ContentFilter()} |
1024 * @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 |
1025 * hidden | 1075 * hidden |
1026 * @constructor | 1076 * @constructor |
1027 * @augments ContentFilter | 1077 * @augments ContentFilter |
1028 */ | 1078 */ |
1029 function ElemHideBase(text, domains, selector) | 1079 function ElemHideBase(text, domains, selector) |
1030 { | 1080 { |
1031 ContentFilter.call(this, text, domains, selector); | 1081 ContentFilter.call(this, text, domains, selector); |
1032 } | 1082 } |
1033 exports.ElemHideBase = ElemHideBase; | 1083 exports.ElemHideBase = ElemHideBase; |
1034 | 1084 |
1035 ElemHideBase.prototype = extend(ContentFilter, { | 1085 ElemHideBase.prototype = extend(ContentFilter, { |
1036 /** | 1086 /** |
1037 * CSS selector for the HTML elements that should be hidden | 1087 * CSS selector for the HTML elements that should be hidden |
1038 * @type {string} | 1088 * @type {string} |
1039 */ | 1089 */ |
1040 get selector() | 1090 get selector() |
Manish Jethani
2018/06/26 19:28:15
This can be a getter actually, so we don't create
| |
1041 { | 1091 { |
1042 // Braces are being escaped to prevent CSS rule injection. | 1092 // Braces are being escaped to prevent CSS rule injection. |
1043 return this.script.replace("{", "\\7B ").replace("}", "\\7D "); | 1093 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); |
1044 } | 1094 } |
1045 }); | 1095 }); |
1046 | |
1047 /** | |
1048 * Creates an element hiding filter from a pre-parsed text representation | |
1049 * | |
1050 * @param {string} text same as in Filter() | |
1051 * @param {string} [domains] | |
1052 * domains part of the text representation | |
1053 * @param {string} [type] | |
1054 * rule type, either empty or @ (exception) or ? (emulation rule) | |
1055 * @param {string} selector raw CSS selector | |
1056 * @return {ElemHideFilter|ElemHideException| | |
1057 * ElemHideEmulationFilter|InvalidFilter} | |
1058 */ | |
1059 ElemHideBase.fromText = function(text, domains, type, selector) | |
1060 { | |
1061 // We don't allow ElemHide filters which have any empty domains. | |
1062 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that | |
1063 // changes this must be changed too. | |
1064 if (domains && /(^|,)~?(,|$)/.test(domains)) | |
1065 return new InvalidFilter(text, "filter_invalid_domain"); | |
1066 | |
1067 if (type == "@") | |
1068 return new ElemHideException(text, domains, selector); | |
1069 | |
1070 if (type == "?") | |
1071 { | |
1072 // Element hiding emulation filters are inefficient so we need to make sure | |
1073 // that they're only applied if they specify active domains | |
1074 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | |
1075 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | |
1076 | |
1077 return new ElemHideEmulationFilter(text, domains, selector); | |
1078 } | |
1079 | |
1080 return new ElemHideFilter(text, domains, selector); | |
1081 }; | |
1082 | 1096 |
1083 /** | 1097 /** |
1084 * Class for element hiding filters | 1098 * Class for element hiding filters |
1085 * @param {string} text see {@link Filter Filter()} | 1099 * @param {string} text see {@link Filter Filter()} |
1086 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} | 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} |
1087 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1088 * @constructor | 1102 * @constructor |
1089 * @augments ElemHideBase | 1103 * @augments ElemHideBase |
1090 */ | 1104 */ |
1091 function ElemHideFilter(text, domains, selector) | 1105 function ElemHideFilter(text, domains, selector) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1142 * @constructor | 1156 * @constructor |
1143 * @augments ContentFilter | 1157 * @augments ContentFilter |
1144 */ | 1158 */ |
1145 function SnippetFilter(text, domains, script) | 1159 function SnippetFilter(text, domains, script) |
1146 { | 1160 { |
1147 ContentFilter.call(this, text, domains, script); | 1161 ContentFilter.call(this, text, domains, script); |
1148 } | 1162 } |
1149 exports.SnippetFilter = SnippetFilter; | 1163 exports.SnippetFilter = SnippetFilter; |
1150 | 1164 |
1151 SnippetFilter.prototype = extend(ContentFilter, { | 1165 SnippetFilter.prototype = extend(ContentFilter, { |
1152 type: "snippet" | 1166 type: "snippet", |
1153 }); | 1167 |
1168 /** | |
1169 * Script that should be executed | |
1170 * @type {string} | |
1171 */ | |
1172 get script() | |
1173 { | |
1174 return this.body; | |
1175 } | |
1176 }); | |
LEFT | RIGHT |