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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /* globals filterToRegExp */ | 18 /* globals filterToRegExp */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 let propertySelectorRegExp = /\[-abp-properties=(["'])([^"']+)\1\]/; | |
23 | |
24 function splitSelector(selector) | 22 function splitSelector(selector) |
25 { | 23 { |
26 if (selector.indexOf(",") == -1) | 24 if (selector.indexOf(",") == -1) |
27 return [selector]; | 25 return [selector]; |
28 | 26 |
29 let selectors = []; | 27 let selectors = []; |
30 let start = 0; | 28 let start = 0; |
31 let level = 0; | 29 let level = 0; |
32 let sep = ""; | 30 let sep = ""; |
33 | 31 |
(...skipping 18 matching lines...) Expand all Loading... |
52 selectors.push(selector.substring(start, i)); | 50 selectors.push(selector.substring(start, i)); |
53 start = i + 1; | 51 start = i + 1; |
54 } | 52 } |
55 } | 53 } |
56 } | 54 } |
57 | 55 |
58 selectors.push(selector.substring(start)); | 56 selectors.push(selector.substring(start)); |
59 return selectors; | 57 return selectors; |
60 } | 58 } |
61 | 59 |
| 60 /** Return position of node from parent. |
| 61 * @param {Node} node - the node to find the position of. |
| 62 * @return {number} 1 base index like for :nth-child(), or 0 on error. |
| 63 */ |
| 64 function positionInParent(node) |
| 65 { |
| 66 if (!node) |
| 67 return 0; |
| 68 let {children} = node.parentNode; |
| 69 for (let i = 0; i < children.length; i++) |
| 70 if (children[i] == node) |
| 71 return i + 1; |
| 72 return 0; |
| 73 } |
| 74 |
| 75 function makeSelector(node, selector) |
| 76 { |
| 77 if (!node.parentElement) |
| 78 { |
| 79 let newSelector = ":root"; |
| 80 if (selector) |
| 81 newSelector += " > "; |
| 82 return newSelector + selector; |
| 83 } |
| 84 let idx = positionInParent(node); |
| 85 if (idx > 0) |
| 86 { |
| 87 let newSelector = `${node.tagName}:nth-child(${idx})`; |
| 88 if (selector) |
| 89 newSelector += " > "; |
| 90 return makeSelector(node.parentElement, newSelector + selector); |
| 91 } |
| 92 |
| 93 return selector; |
| 94 } |
| 95 |
| 96 const abpSelectorRegexp = /:-abp-(properties|has|[A-Za-z\d-]*)\(/i; |
| 97 |
| 98 function parseSelectorContent(content, quoted = false) |
| 99 { |
| 100 let parens = 1; |
| 101 let i = 0; |
| 102 let quote = null; |
| 103 let originalLength = content.length; |
| 104 if (quoted) |
| 105 content = content.trim(); |
| 106 while (i < content.length) |
| 107 { |
| 108 let c = content[i]; |
| 109 if (quoted && i == 0) |
| 110 { |
| 111 if (c != "'" && c != '"') |
| 112 return null; |
| 113 } |
| 114 if (c == "\\") |
| 115 i++; |
| 116 else if (quote) |
| 117 { |
| 118 if (c == quote) |
| 119 quote = null; |
| 120 } |
| 121 else if (c == "'" || c == '"') |
| 122 { |
| 123 quote = c; |
| 124 } |
| 125 else if (c == "(") |
| 126 parens++; |
| 127 else if (c == ")") |
| 128 { |
| 129 parens--; |
| 130 if (parens == 0) |
| 131 break; |
| 132 } |
| 133 i++; |
| 134 } |
| 135 if (parens > 0) |
| 136 return null; |
| 137 if (quoted) |
| 138 { |
| 139 let end = content.substr(0, i).lastIndexOf(content[0]); |
| 140 return {text: content.substr(1, end - 1), |
| 141 end: i + (originalLength - content.length)}; |
| 142 } |
| 143 return {text: content.substr(0, i), end: i}; |
| 144 } |
| 145 |
| 146 function parseSelector(selector, level = 0) |
| 147 { |
| 148 if (selector.length == 0) |
| 149 return []; |
| 150 |
| 151 let match = abpSelectorRegexp.exec(selector); |
| 152 if (!match) |
| 153 return [new PlainSelector(selector)]; |
| 154 |
| 155 let selectors = []; |
| 156 let suffixStart = match.index; |
| 157 if (suffixStart > 0) |
| 158 selectors.push(new PlainSelector(selector.substr(0, suffixStart))); |
| 159 |
| 160 let startIndex = match.index + match[0].length; |
| 161 let content = null; |
| 162 if (match[1] == "properties") |
| 163 { |
| 164 content = parseSelectorContent(selector.substr(startIndex), true); |
| 165 if (content == null) |
| 166 { |
| 167 console.error(new SyntaxError("Failed to parse AdBlock Plus " + |
| 168 `selector ${selector}, invalid ` + |
| 169 "properties string.")); |
| 170 return null; |
| 171 } |
| 172 |
| 173 selectors.push(new PropsSelector(content.text)); |
| 174 } |
| 175 else if (match[1] == "has") |
| 176 { |
| 177 if (level > 0) |
| 178 { |
| 179 console.error(new SyntaxError("Failed to parse AdBlock Plus " + |
| 180 `selector ${selector}, invalid ` + |
| 181 "nested :-abp-has().")); |
| 182 return null; |
| 183 } |
| 184 |
| 185 content = parseSelectorContent(selector.substr(startIndex)); |
| 186 if (content == null) |
| 187 { |
| 188 console.error(new SyntaxError("Failed parsing AdBlock Plus " + |
| 189 `selector ${selector}, didn't ` + |
| 190 "find closing parenthesis.")); |
| 191 return null; |
| 192 } |
| 193 |
| 194 let hasSelector = new HasSelector(content.text); |
| 195 if (!hasSelector.valid()) |
| 196 return null; |
| 197 selectors.push(hasSelector); |
| 198 } |
| 199 else |
| 200 { |
| 201 // this is an error, can't parse selector. |
| 202 console.error(new SyntaxError("Failed parsing AdBlock Plus " + |
| 203 `selector ${selector}, invalid ` + |
| 204 `pseudo-class -abp-${match[1]}.`)); |
| 205 return null; |
| 206 } |
| 207 |
| 208 suffixStart = startIndex + content.end + 1; |
| 209 |
| 210 let suffix = parseSelector(selector.substr(suffixStart), level); |
| 211 if (suffix == null) |
| 212 return null; |
| 213 |
| 214 selectors.push(...suffix); |
| 215 |
| 216 return selectors; |
| 217 } |
| 218 |
| 219 function *findPropsSelectors(styles, prefix, regexp) |
| 220 { |
| 221 for (let style of styles) |
| 222 if (regexp.test(style.style)) |
| 223 for (let subSelector of style.subSelectors) |
| 224 yield prefix + subSelector; |
| 225 } |
| 226 |
| 227 function stringifyStyle(style) |
| 228 { |
| 229 let styles = []; |
| 230 for (let i = 0; i < style.length; i++) |
| 231 { |
| 232 let property = style.item(i); |
| 233 let value = style.getPropertyValue(property); |
| 234 let priority = style.getPropertyPriority(property); |
| 235 styles.push(property + ": " + value + (priority ? " !" + priority : "") + |
| 236 ";"); |
| 237 } |
| 238 styles.sort(); |
| 239 return styles.join(" "); |
| 240 } |
| 241 |
| 242 function* evaluate(chain, index, prefix, subtree, styles) |
| 243 { |
| 244 if (index >= chain.length) |
| 245 { |
| 246 yield prefix; |
| 247 return; |
| 248 } |
| 249 for (let [selector, element] of |
| 250 chain[index].getSelectors(prefix, subtree, styles)) |
| 251 yield* evaluate(chain, index + 1, selector, element, styles); |
| 252 } |
| 253 |
| 254 function PlainSelector(selector) |
| 255 { |
| 256 this._selector = selector; |
| 257 } |
| 258 |
| 259 PlainSelector.prototype = { |
| 260 /** |
| 261 * Generator function returning a pair of selector |
| 262 * string and subtree. |
| 263 * @param {String} prefix - the prefix for the selector. |
| 264 * @param {Node} subtree - the subtree we work on. |
| 265 * @param {Array} styles - the stringified stylesheets. |
| 266 */ |
| 267 *getSelectors(prefix, subtree, styles) |
| 268 { |
| 269 yield [prefix + this._selector, subtree]; |
| 270 }, |
| 271 |
| 272 /** |
| 273 * Generator function returning selected elements. |
| 274 * @param {String} prefix - the prefix for the selector. |
| 275 * @param {Node} subtree - the subtree we work on. |
| 276 * @param {Array} styles - the stringified stylesheets. |
| 277 */ |
| 278 *getElements(prefix, subtree, styles) |
| 279 { |
| 280 for (let [selector] of this.getSelectors(prefix, subtree, styles)) |
| 281 for (let element of subtree.querySelectorAll(selector)) |
| 282 yield element; |
| 283 } |
| 284 }; |
| 285 |
| 286 const prefixEndRegexp = /[\s>+~]$/; |
| 287 |
| 288 function HasSelector(selector, level = 0) |
| 289 { |
| 290 this._innerSelectors = parseSelector(selector, level + 1); |
| 291 } |
| 292 |
| 293 HasSelector.prototype = { |
| 294 valid() |
| 295 { |
| 296 return this._innerSelectors != null; |
| 297 }, |
| 298 |
| 299 *getSelectors(prefix, subtree, styles) |
| 300 { |
| 301 for (let element of this.getElements(prefix, subtree, styles)) |
| 302 yield [makeSelector(element, ""), element]; |
| 303 }, |
| 304 |
| 305 *getElements(prefix, subtree, styles) |
| 306 { |
| 307 let actualPrefix = (!prefix || prefixEndRegexp.test(prefix)) ? |
| 308 prefix + "*" : prefix; |
| 309 let elements = subtree.querySelectorAll(actualPrefix); |
| 310 for (let element of elements) |
| 311 { |
| 312 let newPrefix = makeSelector(element, ""); |
| 313 let iter = evaluate(this._innerSelectors, 0, "", element, styles); |
| 314 for (let selector of iter) |
| 315 // we insert a space between the two. It becomes a no-op if selector |
| 316 // doesn't have a combinator |
| 317 if (subtree.querySelector(newPrefix + " " + selector)) |
| 318 yield element; |
| 319 } |
| 320 } |
| 321 }; |
| 322 |
| 323 function PropsSelector(propertyExpression) |
| 324 { |
| 325 let regexpString; |
| 326 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
| 327 propertyExpression[propertyExpression.length - 1] == "/") |
| 328 { |
| 329 regexpString = propertyExpression.slice(1, -1) |
| 330 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
| 331 } |
| 332 else |
| 333 regexpString = filterToRegExp(propertyExpression); |
| 334 |
| 335 this._regexp = new RegExp(regexpString, "i"); |
| 336 } |
| 337 |
| 338 PropsSelector.prototype = { |
| 339 *getSelectors(prefix, subtree, styles) |
| 340 { |
| 341 for (let selector of findPropsSelectors(styles, prefix, this._regexp)) |
| 342 yield [selector, subtree]; |
| 343 }, |
| 344 |
| 345 *getElements(prefix, subtree, styles) |
| 346 { |
| 347 for (let [selector] of this.getSelectors(prefix, subtree, styles)) |
| 348 for (let subElement of subtree.querySelectorAll(selector)) |
| 349 yield subElement; |
| 350 } |
| 351 }; |
| 352 |
62 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | 353 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) |
63 { | 354 { |
64 this.window = window; | 355 this.window = window; |
65 this.getFiltersFunc = getFiltersFunc; | 356 this.getFiltersFunc = getFiltersFunc; |
66 this.addSelectorsFunc = addSelectorsFunc; | 357 this.addSelectorsFunc = addSelectorsFunc; |
67 } | 358 } |
68 | 359 |
69 ElemHideEmulation.prototype = { | 360 ElemHideEmulation.prototype = { |
70 stringifyStyle(style) | |
71 { | |
72 let styles = []; | |
73 for (let i = 0; i < style.length; i++) | |
74 { | |
75 let property = style.item(i); | |
76 let value = style.getPropertyValue(property); | |
77 let priority = style.getPropertyPriority(property); | |
78 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
79 ";"); | |
80 } | |
81 styles.sort(); | |
82 return styles.join(" "); | |
83 }, | |
84 | |
85 isSameOrigin(stylesheet) | 361 isSameOrigin(stylesheet) |
86 { | 362 { |
87 try | 363 try |
88 { | 364 { |
89 return new URL(stylesheet.href).origin == this.window.location.origin; | 365 return new URL(stylesheet.href).origin == this.window.location.origin; |
90 } | 366 } |
91 catch (e) | 367 catch (e) |
92 { | 368 { |
93 // Invalid URL, assume that it is first-party. | 369 // Invalid URL, assume that it is first-party. |
94 return true; | 370 return true; |
95 } | 371 } |
96 }, | 372 }, |
97 | 373 |
98 findSelectors(stylesheet, selectors, filters) | |
99 { | |
100 // Explicitly ignore third-party stylesheets to ensure consistent behavior | |
101 // between Firefox and Chrome. | |
102 if (!this.isSameOrigin(stylesheet)) | |
103 return; | |
104 | |
105 let rules = stylesheet.cssRules; | |
106 if (!rules) | |
107 return; | |
108 | |
109 for (let rule of rules) | |
110 { | |
111 if (rule.type != rule.STYLE_RULE) | |
112 continue; | |
113 | |
114 let style = this.stringifyStyle(rule.style); | |
115 for (let pattern of this.patterns) | |
116 { | |
117 if (pattern.regexp.test(style)) | |
118 { | |
119 let subSelectors = splitSelector(rule.selectorText); | |
120 for (let subSelector of subSelectors) | |
121 { | |
122 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
123 filters.push(pattern.text); | |
124 } | |
125 } | |
126 } | |
127 } | |
128 }, | |
129 | |
130 addSelectors(stylesheets) | 374 addSelectors(stylesheets) |
131 { | 375 { |
132 let selectors = []; | 376 let selectors = []; |
133 let filters = []; | 377 let filters = []; |
| 378 |
| 379 let cssStyles = []; |
| 380 |
134 for (let stylesheet of stylesheets) | 381 for (let stylesheet of stylesheets) |
135 this.findSelectors(stylesheet, selectors, filters); | 382 { |
| 383 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
| 384 // between Firefox and Chrome. |
| 385 if (!this.isSameOrigin(stylesheet)) |
| 386 continue; |
| 387 |
| 388 let rules = stylesheet.cssRules; |
| 389 if (!rules) |
| 390 continue; |
| 391 |
| 392 for (let rule of rules) |
| 393 { |
| 394 if (rule.type != rule.STYLE_RULE) |
| 395 continue; |
| 396 |
| 397 let style = stringifyStyle(rule.style); |
| 398 let subSelectors = splitSelector(rule.selectorText); |
| 399 cssStyles.push({style, subSelectors}); |
| 400 } |
| 401 } |
| 402 |
| 403 for (let patterns of this.selPatterns) |
| 404 for (let selector of evaluate(patterns.selectors, |
| 405 0, "", document, cssStyles)) |
| 406 { |
| 407 selectors.push(selector); |
| 408 filters.push(patterns.text); |
| 409 } |
| 410 |
136 this.addSelectorsFunc(selectors, filters); | 411 this.addSelectorsFunc(selectors, filters); |
137 }, | 412 }, |
138 | 413 |
139 onLoad(event) | 414 onLoad(event) |
140 { | 415 { |
141 let stylesheet = event.target.sheet; | 416 let stylesheet = event.target.sheet; |
142 if (stylesheet) | 417 if (stylesheet) |
143 this.addSelectors([stylesheet]); | 418 this.addSelectors([stylesheet]); |
144 }, | 419 }, |
145 | 420 |
146 apply() | 421 apply() |
147 { | 422 { |
148 this.getFiltersFunc(patterns => | 423 this.getFiltersFunc(patterns => |
149 { | 424 { |
150 this.patterns = []; | 425 this.selPatterns = []; |
| 426 |
151 for (let pattern of patterns) | 427 for (let pattern of patterns) |
152 { | 428 { |
153 let match = propertySelectorRegExp.exec(pattern.selector); | 429 let selectors = parseSelector(pattern.selector); |
154 if (!match) | 430 if (selectors != null && selectors.length > 0) |
155 continue; | 431 this.selPatterns.push({selectors, text: pattern.text}); |
156 | 432 } |
157 let propertyExpression = match[2]; | 433 |
158 let regexpString; | 434 if (this.selPatterns.length > 0) |
159 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
160 propertyExpression[propertyExpression.length - 1] == "/") | |
161 { | |
162 regexpString = propertyExpression.slice(1, -1) | |
163 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
164 } | |
165 else | |
166 regexpString = filterToRegExp(propertyExpression); | |
167 | |
168 this.patterns.push({ | |
169 text: pattern.text, | |
170 regexp: new RegExp(regexpString, "i"), | |
171 prefix: pattern.selector.substr(0, match.index), | |
172 suffix: pattern.selector.substr(match.index + match[0].length) | |
173 }); | |
174 } | |
175 | |
176 if (this.patterns.length > 0) | |
177 { | 435 { |
178 let {document} = this.window; | 436 let {document} = this.window; |
179 this.addSelectors(document.styleSheets); | 437 this.addSelectors(document.styleSheets); |
180 document.addEventListener("load", this.onLoad.bind(this), true); | 438 document.addEventListener("load", this.onLoad.bind(this), true); |
181 } | 439 } |
182 }); | 440 }); |
183 } | 441 } |
184 }; | 442 }; |
OLD | NEW |