| 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-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. | |
|
Wladimir Palant
2017/06/01 10:16:36
Nit (here and elsewhere): the minus sign is unnece
hub
2017/06/01 18:22:55
Acknowledged.
| |
| 62 * @return {number} 1 base index like for :nth-child(), or 0 on error. | |
|
Wladimir Palant
2017/06/01 10:16:38
Nit: I think it's "One-based"
hub
2017/06/01 18:22:58
Acknowledged.
| |
| 63 */ | |
| 64 function positionInParent(node) | |
| 65 { | |
| 66 if (!node) | |
| 67 return 0; | |
|
Wladimir Palant
2017/06/01 10:16:37
Nit: With the current code we can no longer get nu
hub
2017/06/01 18:22:58
Acknowledged.
| |
| 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; | |
|
Wladimir Palant
2017/06/01 10:16:36
Ok, let's say that [A-Za-z\d-]* clause is forward
hub
2017/06/01 18:22:56
this regexp is now gone. I use the one from issue
| |
| 97 | |
| 98 function parseSelectorContent(content, quoted = false) | |
|
Wladimir Palant
2017/06/01 10:16:35
Why this quoted parameter? As I mentioned before,
hub
2017/06/01 18:22:56
this is gone too. I may have missed the comment ab
| |
| 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(); | |
|
Wladimir Palant
2017/06/01 10:16:35
This call will remove whitespace on both sides - y
hub
2017/06/01 18:22:56
it is gone. see above.
| |
| 106 while (i < content.length) | |
|
Wladimir Palant
2017/06/01 10:16:35
This should really be a for loop:
for (let i =
hub
2017/06/01 18:22:58
current version is your parser from issue 5287.
| |
| 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))); | |
|
Wladimir Palant
2017/06/01 10:16:34
I don't think that reusing suffixStart variable fo
hub
2017/06/01 18:22:58
Acknowledged.
| |
| 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 } | |
|
Wladimir Palant
2017/06/01 10:16:35
Setting and checking content variable should be do
hub
2017/06/01 18:22:57
there was the exception for "quoted", but now that
| |
| 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().")); | |
|
Wladimir Palant
2017/06/01 10:16:36
While nested :-abp-has() might not be very efficie
hub
2017/06/01 18:22:57
There was an issue, but it is fixed now. Removing
| |
| 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; | |
|
Wladimir Palant
2017/06/01 10:16:36
Really, content.end should just have the proper va
hub
2017/06/01 18:22:55
I preferred to just pass a string to parseSelector
Wladimir Palant
2017/06/07 08:32:57
Well, you didn't. Whatever, not that important I g
hub
2017/06/07 14:15:07
I did really intend to do it. Done now.
| |
| 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) | |
|
Wladimir Palant
2017/06/01 10:16:37
This is functionality that is only used by PropsSe
hub
2017/06/01 18:23:00
Acknowledged.
| |
| 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. | |
|
Wladimir Palant
2017/06/01 10:16:38
Nit: The type here should be more specific: {strin
hub
2017/06/01 18:23:00
They are not strings. I'll rephrase to say "the st
Wladimir Palant
2017/06/07 08:32:57
Right, they aren't, my original assumptions were w
hub
2017/06/07 14:15:07
Done.
| |
| 266 */ | |
| 267 *getSelectors(prefix, subtree, styles) | |
| 268 { | |
| 269 yield [prefix + this._selector, subtree]; | |
| 270 } | |
| 271 }; | |
| 272 | |
| 273 const prefixEndRegexp = /[\s>+~]$/; | |
|
Wladimir Palant
2017/06/01 10:16:34
This variable name doesn't really tell much about
hub
2017/06/01 18:22:57
Acknowledged.
| |
| 274 | |
| 275 function HasSelector(selector, level = 0) | |
| 276 { | |
| 277 this._innerSelectors = parseSelector(selector, level + 1); | |
| 278 } | |
| 279 | |
| 280 HasSelector.prototype = { | |
| 281 valid() | |
| 282 { | |
| 283 return this._innerSelectors != null; | |
| 284 }, | |
| 285 | |
| 286 *getSelectors(prefix, subtree, styles) | |
| 287 { | |
| 288 for (let element of this.getElements(prefix, subtree, styles)) | |
| 289 yield [makeSelector(element, ""), element]; | |
| 290 }, | |
| 291 | |
| 292 /** | |
| 293 * Generator function returning selected elements. | |
| 294 * @param {String} prefix - the prefix for the selector. | |
| 295 * @param {Node} subtree - the subtree we work on. | |
| 296 * @param {Array} styles - the stringified stylesheets. | |
|
Wladimir Palant
2017/06/01 10:16:37
Nit: Like above, the type is string[]
hub
2017/06/01 18:22:55
a above. not a string array.
| |
| 297 */ | |
| 298 *getElements(prefix, subtree, styles) | |
| 299 { | |
| 300 let actualPrefix = (!prefix || prefixEndRegexp.test(prefix)) ? | |
| 301 prefix + "*" : prefix; | |
| 302 let elements = subtree.querySelectorAll(actualPrefix); | |
| 303 for (let element of elements) | |
| 304 { | |
| 305 let newPrefix = makeSelector(element, ""); | |
| 306 let iter = evaluate(this._innerSelectors, 0, "", element, styles); | |
| 307 for (let selector of iter) | |
| 308 // we insert a space between the two. It becomes a no-op if selector | |
| 309 // doesn't have a combinator | |
| 310 if (subtree.querySelector(newPrefix + " " + selector)) | |
| 311 yield element; | |
| 312 } | |
| 313 } | |
| 314 }; | |
| 315 | |
| 316 function PropsSelector(propertyExpression) | |
| 317 { | |
| 318 let regexpString; | |
| 319 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
| 320 propertyExpression[propertyExpression.length - 1] == "/") | |
| 321 { | |
| 322 regexpString = propertyExpression.slice(1, -1) | |
| 323 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
| 324 } | |
| 325 else | |
| 326 regexpString = filterToRegExp(propertyExpression); | |
| 327 | |
| 328 this._regexp = new RegExp(regexpString, "i"); | |
| 329 } | |
| 330 | |
| 331 PropsSelector.prototype = { | |
| 332 *getSelectors(prefix, subtree, styles) | |
| 333 { | |
| 334 for (let selector of findPropsSelectors(styles, prefix, this._regexp)) | |
| 335 yield [selector, subtree]; | |
| 336 } | |
| 337 }; | |
| 338 | |
| 62 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | 339 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) |
| 63 { | 340 { |
| 64 this.window = window; | 341 this.window = window; |
| 65 this.getFiltersFunc = getFiltersFunc; | 342 this.getFiltersFunc = getFiltersFunc; |
| 66 this.addSelectorsFunc = addSelectorsFunc; | 343 this.addSelectorsFunc = addSelectorsFunc; |
| 67 } | 344 } |
| 68 | 345 |
| 69 ElemHideEmulation.prototype = { | 346 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) | 347 isSameOrigin(stylesheet) |
| 86 { | 348 { |
| 87 try | 349 try |
| 88 { | 350 { |
| 89 return new URL(stylesheet.href).origin == this.window.location.origin; | 351 return new URL(stylesheet.href).origin == this.window.location.origin; |
| 90 } | 352 } |
| 91 catch (e) | 353 catch (e) |
| 92 { | 354 { |
| 93 // Invalid URL, assume that it is first-party. | 355 // Invalid URL, assume that it is first-party. |
| 94 return true; | 356 return true; |
| 95 } | 357 } |
| 96 }, | 358 }, |
| 97 | 359 |
| 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) | 360 addSelectors(stylesheets) |
| 131 { | 361 { |
| 132 let selectors = []; | 362 let selectors = []; |
| 133 let filters = []; | 363 let filters = []; |
| 364 | |
| 365 let cssStyles = []; | |
| 366 | |
| 134 for (let stylesheet of stylesheets) | 367 for (let stylesheet of stylesheets) |
| 135 this.findSelectors(stylesheet, selectors, filters); | 368 { |
| 369 // Explicitly ignore third-party stylesheets to ensure consistent behavior | |
| 370 // between Firefox and Chrome. | |
| 371 if (!this.isSameOrigin(stylesheet)) | |
| 372 continue; | |
| 373 | |
| 374 let rules = stylesheet.cssRules; | |
| 375 if (!rules) | |
| 376 continue; | |
| 377 | |
| 378 for (let rule of rules) | |
| 379 { | |
| 380 if (rule.type != rule.STYLE_RULE) | |
| 381 continue; | |
| 382 | |
| 383 let style = stringifyStyle(rule.style); | |
| 384 let subSelectors = splitSelector(rule.selectorText); | |
| 385 cssStyles.push({style, subSelectors}); | |
| 386 } | |
| 387 } | |
| 388 | |
| 389 for (let patterns of this.selPatterns) | |
| 390 for (let selector of evaluate(patterns.selectors, | |
| 391 0, "", document, cssStyles)) | |
| 392 { | |
| 393 selectors.push(selector); | |
| 394 filters.push(patterns.text); | |
| 395 } | |
| 396 | |
| 136 this.addSelectorsFunc(selectors, filters); | 397 this.addSelectorsFunc(selectors, filters); |
| 137 }, | 398 }, |
| 138 | 399 |
| 139 onLoad(event) | 400 onLoad(event) |
| 140 { | 401 { |
| 141 let stylesheet = event.target.sheet; | 402 let stylesheet = event.target.sheet; |
| 142 if (stylesheet) | 403 if (stylesheet) |
| 143 this.addSelectors([stylesheet]); | 404 this.addSelectors([stylesheet]); |
| 144 }, | 405 }, |
| 145 | 406 |
| 146 apply() | 407 apply() |
| 147 { | 408 { |
| 148 this.getFiltersFunc(patterns => | 409 this.getFiltersFunc(patterns => |
| 149 { | 410 { |
| 150 this.patterns = []; | 411 this.selPatterns = []; |
| 412 | |
| 151 for (let pattern of patterns) | 413 for (let pattern of patterns) |
| 152 { | 414 { |
| 153 let match = propertySelectorRegExp.exec(pattern.selector); | 415 let selectors = parseSelector(pattern.selector); |
| 154 if (!match) | 416 if (selectors != null && selectors.length > 0) |
| 155 continue; | 417 this.selPatterns.push({selectors, text: pattern.text}); |
| 156 | 418 } |
| 157 let propertyExpression = match[2]; | 419 |
| 158 let regexpString; | 420 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 { | 421 { |
| 178 let {document} = this.window; | 422 let {document} = this.window; |
| 179 this.addSelectors(document.styleSheets); | 423 this.addSelectors(document.styleSheets); |
| 180 document.addEventListener("load", this.onLoad.bind(this), true); | 424 document.addEventListener("load", this.onLoad.bind(this), true); |
| 181 } | 425 } |
| 182 }); | 426 }); |
| 183 } | 427 } |
| 184 }; | 428 }; |
| OLD | NEW |