Left: | ||
Right: |
OLD | NEW |
---|---|
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 } | 77 } |
78 }; | 78 }; |
79 | 79 |
80 /** | 80 /** |
81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. |
82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} |
83 */ | 83 */ |
84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); |
85 | 85 |
86 /** | 86 /** |
87 * Regular expression that element hiding filters should match | 87 * Regular expression that content filters should match |
88 * @type {RegExp} | 88 * @type {RegExp} |
89 */ | 89 */ |
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; | 90 Filter.contentRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; |
91 /** | 91 /** |
92 * Regular expression that RegExp filters specified as RegExps should match | 92 * Regular expression that RegExp filters specified as RegExps should match |
93 * @type {RegExp} | 93 * @type {RegExp} |
94 */ | 94 */ |
95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; | 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; |
96 /** | 96 /** |
97 * Regular expression that options on a RegExp filter should match | 97 * Regular expression that options on a RegExp filter should match |
98 * @type {RegExp} | 98 * @type {RegExp} |
99 */ | 99 */ |
100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; |
101 /** | 101 /** |
102 * Regular expression that matches an invalid Content Security Policy | 102 * Regular expression that matches an invalid Content Security Policy |
103 * @type {RegExp} | 103 * @type {RegExp} |
104 */ | 104 */ |
105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; |
106 | 106 |
107 /** | 107 /** |
108 * Creates a filter of correct type from its text representation - does the | 108 * Creates a filter of correct type from its text representation - does the |
109 * basic parsing and calls the right constructor then. | 109 * basic parsing and calls the right constructor then. |
110 * | 110 * |
111 * @param {string} text as in Filter() | 111 * @param {string} text as in Filter() |
112 * @return {Filter} | 112 * @return {Filter} |
113 */ | 113 */ |
114 Filter.fromText = function(text) | 114 Filter.fromText = function(text) |
115 { | 115 { |
116 let filter = Filter.knownFilters.get(text); | 116 let filter = Filter.knownFilters.get(text); |
117 if (filter) | 117 if (filter) |
118 return filter; | 118 return filter; |
119 | 119 |
120 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); | 120 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; |
121 if (match) | 121 if (match) |
122 { | 122 { |
123 let propsMatch; | 123 let propsMatch; |
124 if (!match[2] && | 124 if (!match[2] && |
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 filter = ElemHideBase.fromText( | 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); |
136 text, match[1], match[2], match[3] | |
137 ); | |
138 } | 136 } |
139 else if (text[0] == "!") | 137 else if (text[0] == "!") |
140 filter = new CommentFilter(text); | 138 filter = new CommentFilter(text); |
141 else | 139 else |
142 filter = RegExpFilter.fromText(text); | 140 filter = RegExpFilter.fromText(text); |
143 | 141 |
144 Filter.knownFilters.set(filter.text, filter); | 142 Filter.knownFilters.set(filter.text, filter); |
145 return filter; | 143 return filter; |
146 }; | 144 }; |
147 | 145 |
(...skipping 29 matching lines...) Expand all Loading... | |
177 if (!text) | 175 if (!text) |
178 return text; | 176 return text; |
179 | 177 |
180 // Remove line breaks, tabs etc | 178 // Remove line breaks, tabs etc |
181 text = text.replace(/[^\S ]+/g, ""); | 179 text = text.replace(/[^\S ]+/g, ""); |
182 | 180 |
183 // Don't remove spaces inside comments | 181 // Don't remove spaces inside comments |
184 if (/^ *!/.test(text)) | 182 if (/^ *!/.test(text)) |
185 return text.trim(); | 183 return text.trim(); |
186 | 184 |
187 // Special treatment for element hiding filters, right side is allowed to | 185 // Special treatment for content filters, right side is allowed to contain |
188 // contain spaces | 186 // spaces |
189 if (Filter.elemhideRegExp.test(text)) | 187 if (Filter.contentRegExp.test(text)) |
190 { | 188 { |
191 let [, domains, separator, selector] = /^(.*?)(#[@?]?#?)(.*)$/.exec(text); | 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); |
192 return domains.replace(/ +/g, "") + separator + selector.trim(); | 190 return domains.replace(/ +/g, "") + separator + body.trim(); |
193 } | 191 } |
194 | 192 |
195 // 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 |
196 // are allowed to contain single (non trailing) spaces. | 194 // are allowed to contain single (non trailing) spaces. |
197 let strippedText = text.replace(/ +/g, ""); | 195 let strippedText = text.replace(/ +/g, ""); |
198 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) |
199 return strippedText; | 197 return strippedText; |
200 | 198 |
201 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); |
202 if (!optionsMatch) | 200 if (!optionsMatch) |
(...skipping 785 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
988 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 986 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
989 thirdParty, sitekeys); | 987 thirdParty, sitekeys); |
990 } | 988 } |
991 exports.WhitelistFilter = WhitelistFilter; | 989 exports.WhitelistFilter = WhitelistFilter; |
992 | 990 |
993 WhitelistFilter.prototype = extend(RegExpFilter, { | 991 WhitelistFilter.prototype = extend(RegExpFilter, { |
994 type: "whitelist" | 992 type: "whitelist" |
995 }); | 993 }); |
996 | 994 |
997 /** | 995 /** |
998 * Base class for element hiding filters | 996 * Base class for content filters |
999 * @param {string} text see {@link Filter Filter()} | 997 * @param {string} text see {@link Filter Filter()} |
1000 * @param {string} [domains] Host names or domains the filter should be | 998 * @param {string} [domains] Host names or domains the filter should be |
1001 * restricted to | 999 * restricted to |
1002 * @param {string} selector CSS selector for the HTML elements that should be | 1000 * @param {string} body The body of the filter |
1003 * hidden | |
1004 * @constructor | 1001 * @constructor |
1005 * @augments ActiveFilter | 1002 * @augments ActiveFilter |
1006 */ | 1003 */ |
1007 function ElemHideBase(text, domains, selector) | 1004 function ContentFilter(text, domains, body) |
1008 { | 1005 { |
1009 ActiveFilter.call(this, text, domains || null); | 1006 ActiveFilter.call(this, text, domains || null); |
1010 | 1007 |
1011 // Braces are being escaped to prevent CSS rule injection. | 1008 this.body = body; |
1012 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D "); | |
1013 } | 1009 } |
1014 exports.ElemHideBase = ElemHideBase; | 1010 exports.ContentFilter = ContentFilter; |
1015 | 1011 |
1016 ElemHideBase.prototype = extend(ActiveFilter, { | 1012 ContentFilter.prototype = extend(ActiveFilter, { |
1017 /** | 1013 /** |
1018 * @see ActiveFilter.domainSeparator | 1014 * @see ActiveFilter.domainSeparator |
1019 */ | 1015 */ |
1020 domainSeparator: ",", | 1016 domainSeparator: ",", |
1021 | 1017 |
1022 /** | 1018 /** |
1023 * CSS selector for the HTML elements that should be hidden | 1019 * The body of the filter |
1024 * @type {string} | 1020 * @type {string} |
1025 */ | 1021 */ |
1026 selector: null | 1022 body: null |
1027 }); | 1023 }); |
1028 | 1024 |
1029 /** | 1025 /** |
1030 * Creates an element hiding filter from a pre-parsed text representation | 1026 * Creates a content filter from a pre-parsed text representation |
1031 * | 1027 * |
1032 * @param {string} text same as in Filter() | 1028 * @param {string} text same as in Filter() |
1033 * @param {string} [domains] | 1029 * @param {string} [domains] |
1034 * domains part of the text representation | 1030 * domains part of the text representation |
1035 * @param {string} [type] | 1031 * @param {string} [type] |
1036 * rule type, either empty or @ (exception) or ? (emulation rule) | 1032 * rule type, either empty or @ (exception) or ? (emulation rule) or |
1037 * @param {string} selector raw CSS selector | 1033 * $ (snippet) |
1034 * @param {string} body | |
1035 * body part of the text representation, either a CSS selector or a snippet | |
1036 * script | |
1038 * @return {ElemHideFilter|ElemHideException| | 1037 * @return {ElemHideFilter|ElemHideException| |
1039 * ElemHideEmulationFilter|InvalidFilter} | 1038 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} |
1040 */ | 1039 */ |
1041 ElemHideBase.fromText = function(text, domains, type, selector) | 1040 ContentFilter.fromText = function(text, domains, type, body) |
1042 { | 1041 { |
1043 // We don't allow ElemHide filters which have any empty domains. | 1042 // We don't allow content filters which have any empty domains. |
1044 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that | 1043 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if |
1045 // changes this must be changed too. | 1044 // that changes this must be changed too. |
1046 if (domains && /(^|,)~?(,|$)/.test(domains)) | 1045 if (domains && /(^|,)~?(,|$)/.test(domains)) |
1047 return new InvalidFilter(text, "filter_invalid_domain"); | 1046 return new InvalidFilter(text, "filter_invalid_domain"); |
1048 | 1047 |
1049 if (type == "@") | 1048 if (type == "@") |
1050 return new ElemHideException(text, domains, selector); | 1049 return new ElemHideException(text, domains, body); |
1050 | |
1051 if (type == "$") | |
1052 return new SnippetFilter(text, domains, body); | |
1051 | 1053 |
1052 if (type == "?") | 1054 if (type == "?") |
1053 { | 1055 { |
1054 // Element hiding emulation filters are inefficient so we need to make sure | 1056 // Element hiding emulation filters are inefficient so we need to make sure |
1055 // that they're only applied if they specify active domains | 1057 // that they're only applied if they specify active domains |
1056 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | 1058 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) |
1057 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | 1059 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); |
1058 | 1060 |
1059 return new ElemHideEmulationFilter(text, domains, selector); | 1061 return new ElemHideEmulationFilter(text, domains, body); |
1060 } | 1062 } |
1061 | 1063 |
1062 return new ElemHideFilter(text, domains, selector); | 1064 return new ElemHideFilter(text, domains, body); |
1063 }; | 1065 }; |
1064 | 1066 |
1065 /** | 1067 /** |
1068 * Base class for element hiding filters | |
1069 * @param {string} text see {@link Filter Filter()} | |
1070 * @param {string} [domains] see {@link ContentFilter ContentFilter()} | |
1071 * @param {string} selector CSS selector for the HTML elements that should be | |
1072 * hidden | |
1073 * @constructor | |
1074 * @augments ContentFilter | |
1075 */ | |
1076 function ElemHideBase(text, domains, selector) | |
1077 { | |
1078 ContentFilter.call(this, text, domains, selector); | |
1079 } | |
1080 exports.ElemHideBase = ElemHideBase; | |
1081 | |
1082 ElemHideBase.prototype = extend(ContentFilter, { | |
1083 /** | |
1084 * CSS selector for the HTML elements that should be hidden | |
1085 * @type {string} | |
1086 */ | |
1087 get selector() | |
1088 { | |
1089 // Braces are being escaped to prevent CSS rule injection. | |
1090 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); | |
1091 } | |
1092 }); | |
1093 | |
1094 /** | |
1066 * Class for element hiding filters | 1095 * Class for element hiding filters |
1067 * @param {string} text see {@link Filter Filter()} | 1096 * @param {string} text see {@link Filter Filter()} |
1068 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} | 1097 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} |
1069 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 1098 * @param {string} selector see {@link ElemHideBase ElemHideBase()} |
1070 * @constructor | 1099 * @constructor |
1071 * @augments ElemHideBase | 1100 * @augments ElemHideBase |
1072 */ | 1101 */ |
1073 function ElemHideFilter(text, domains, selector) | 1102 function ElemHideFilter(text, domains, selector) |
1074 { | 1103 { |
1075 ElemHideBase.call(this, text, domains, selector); | 1104 ElemHideBase.call(this, text, domains, selector); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1108 */ | 1137 */ |
1109 function ElemHideEmulationFilter(text, domains, selector) | 1138 function ElemHideEmulationFilter(text, domains, selector) |
1110 { | 1139 { |
1111 ElemHideBase.call(this, text, domains, selector); | 1140 ElemHideBase.call(this, text, domains, selector); |
1112 } | 1141 } |
1113 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1142 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; |
1114 | 1143 |
1115 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1144 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
1116 type: "elemhideemulation" | 1145 type: "elemhideemulation" |
1117 }); | 1146 }); |
1147 | |
1148 /** | |
1149 * Class for snippet filters | |
1150 * @param {string} text see Filter() | |
1151 * @param {string} [domains] see ContentFilter() | |
1152 * @param {string} script Script that should be executed | |
1153 * @constructor | |
1154 * @augments ContentFilter | |
1155 */ | |
1156 function SnippetFilter(text, domains, script) | |
1157 { | |
1158 ContentFilter.call(this, text, domains, script); | |
1159 } | |
1160 exports.SnippetFilter = SnippetFilter; | |
1161 | |
1162 SnippetFilter.prototype = extend(ContentFilter, { | |
1163 type: "snippet", | |
1164 | |
1165 /** | |
1166 * Script that should be executed | |
1167 * @type {string} | |
1168 */ | |
1169 get script() | |
Manish Jethani
2018/07/11 13:04:25
I would prefer to stick to this terminology for Sn
kzar
2018/07/11 17:17:52
Yes, I agree, "script" makes sense for snippet fil
| |
1170 { | |
1171 return this.body; | |
1172 } | |
1173 }); | |
OLD | NEW |