| 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 |
| 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 "use strict"; | 18 "use strict"; |
| 19 | 19 |
| 20 const {filterToRegExp, splitSelector} = require("../common"); | 20 const {textToRegExp, filterToRegExp, splitSelector} = require("../common"); |
| 21 | 21 |
| 22 let MIN_INVOCATION_INTERVAL = 3000; | 22 let MIN_INVOCATION_INTERVAL = 3000; |
| 23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; | 23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; |
| 24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; | 24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; |
| 25 | 25 |
| 26 /** Return position of node from parent. | 26 /** Return position of node from parent. |
| 27 * @param {Node} node the node to find the position of. | 27 * @param {Node} node the node to find the position of. |
| 28 * @return {number} One-based index like for :nth-child(), or 0 on error. | 28 * @return {number} One-based index like for :nth-child(), or 0 on error. |
| 29 */ | 29 */ |
| 30 function positionInParent(node) | 30 function positionInParent(node) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 let priority = rule.style.getPropertyPriority(property); | 115 let priority = rule.style.getPropertyPriority(property); |
| 116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); | 116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); |
| 117 } | 117 } |
| 118 styles.sort(); | 118 styles.sort(); |
| 119 return { | 119 return { |
| 120 style: styles.join(" "), | 120 style: styles.join(" "), |
| 121 subSelectors: splitSelector(rule.selectorText) | 121 subSelectors: splitSelector(rule.selectorText) |
| 122 }; | 122 }; |
| 123 } | 123 } |
| 124 | 124 |
| 125 const regexpRegexp = /^\/(.*)\/([gimuy]*)$/; | 125 let scopeSupported = null; |
|
Manish Jethani
2018/01/10 11:22:15
We don't have to support all these flags, some of
hub
2018/01/10 18:27:22
let's restrict to "im" for the flags.
| |
| 126 | |
| 127 function tryQuerySelector(subtree, selector, all) | |
| 128 { | |
| 129 let elements = null; | |
| 130 try | |
| 131 { | |
| 132 elements = all ? subtree.querySelectorAll(selector) : | |
| 133 subtree.querySelector(selector); | |
| 134 scopeSupported = true; | |
| 135 } | |
| 136 catch (e) | |
| 137 { | |
| 138 // Edge doesn't support ":scope" | |
| 139 scopeSupported = false; | |
| 140 } | |
| 141 return elements; | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * Query selector. If it is relative, will try :scope. | |
| 146 * @param {Node} subtree the element to query selector | |
| 147 * @param {string} selector the selector to query | |
| 148 * @param {bool} [all=false] true to perform querySelectorAll() | |
| 149 * @returns {?(Node|NodeList)} result of the query. null in case of error. | |
| 150 */ | |
| 151 function scopedQuerySelector(subtree, selector, all) | |
| 152 { | |
| 153 if (selector[0] == ">") | |
| 154 { | |
| 155 selector = ":scope" + selector; | |
| 156 if (scopeSupported) | |
| 157 { | |
| 158 return all ? subtree.querySelectorAll(selector) : | |
| 159 subtree.querySelector(selector); | |
| 160 } | |
| 161 if (scopeSupported == null) | |
| 162 return tryQuerySelector(subtree, selector, all); | |
| 163 return null; | |
| 164 } | |
| 165 return all ? subtree.querySelectorAll(selector) : | |
| 166 subtree.querySelector(selector); | |
| 167 } | |
| 168 | |
| 169 function scopedQuerySelectorAll(subtree, selector) | |
| 170 { | |
| 171 return scopedQuerySelector(subtree, selector, true); | |
| 172 } | |
| 173 | |
| 174 const regexpRegexp = /^\/(.*)\/([im]*)$/; | |
| 126 | 175 |
| 127 /** | 176 /** |
| 128 * Make a regular expression from a text argument. If it can be parsed as a | 177 * Make a regular expression from a text argument. If it can be parsed as a |
| 129 * regular expression, parse it and the flags. | 178 * regular expression, parse it and the flags. |
| 130 * @param {string} text the text argument. | 179 * @param {string} text the text argument. |
| 131 * @param {?string} flag the regexp flag passed to the RegExp constructor: | 180 * @return {?RegExp} a RegExp object or null in case of error. |
| 132 * it overrides flags passed in the text argument. | |
| 133 * @return {RegExp} a RegExp object. | |
| 134 */ | 181 */ |
| 135 function makeRegExpParameter(text, flag) | 182 function makeRegExpParameter(text) |
|
Manish Jethani
2018/01/10 11:22:14
Shouldn't the parameter be called "flags" (plural)
hub
2018/01/10 18:27:21
Done.
| |
| 136 { | 183 { |
| 137 let regexpString = null; | 184 let [, pattern, flags] = |
| 138 let match = regexpRegexp.exec(text); | 185 regexpRegexp.exec(text) || [undefined, textToRegExp(text)]; |
| 139 if (match) | 186 |
| 140 { | 187 try |
| 141 regexpString = match[1].replace("\\7B ", "{").replace("\\7D ", "}"); | 188 { |
| 142 if (!flag) | 189 return new RegExp(pattern, flags); |
|
Manish Jethani
2018/01/10 11:22:15
This changes the syntax of PropsSelector so that n
hub
2018/01/10 18:27:22
It is just more permissive to allow the regexp syn
| |
| 143 flag = match[2]; | 190 } |
| 144 } | 191 catch (e) |
| 145 else | 192 { |
| 146 regexpString = filterToRegExp(text); | 193 } |
| 147 | 194 return null; |
| 148 return new RegExp(regexpString, flag); | |
|
Manish Jethani
2018/01/10 11:22:14
We still need to handle the error if any.
hub
2018/01/10 18:27:21
Done.
| |
| 149 } | 195 } |
| 150 | 196 |
| 151 function* evaluate(chain, index, prefix, subtree, styles) | 197 function* evaluate(chain, index, prefix, subtree, styles) |
| 152 { | 198 { |
| 153 if (index >= chain.length) | 199 if (index >= chain.length) |
| 154 { | 200 { |
| 155 yield prefix; | 201 yield prefix; |
| 156 return; | 202 return; |
| 157 } | 203 } |
| 158 for (let [selector, element] of | 204 for (let [selector, element] of |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 182 * @param {Node} subtree the subtree we work on. | 228 * @param {Node} subtree the subtree we work on. |
| 183 * @param {StringifiedStyle[]} styles the stringified style objects. | 229 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 184 */ | 230 */ |
| 185 *getSelectors(prefix, subtree, styles) | 231 *getSelectors(prefix, subtree, styles) |
| 186 { | 232 { |
| 187 yield [prefix + this._selector, subtree]; | 233 yield [prefix + this._selector, subtree]; |
| 188 } | 234 } |
| 189 }; | 235 }; |
| 190 | 236 |
| 191 const incompletePrefixRegexp = /[\s>+~]$/; | 237 const incompletePrefixRegexp = /[\s>+~]$/; |
| 192 const relativeSelectorRegexp = /^[>+~]/; | |
| 193 | 238 |
| 194 function HasSelector(selectors) | 239 function HasSelector(selectors) |
| 195 { | 240 { |
| 196 this._innerSelectors = selectors; | 241 this._innerSelectors = selectors; |
| 197 } | 242 } |
| 198 | 243 |
| 199 HasSelector.prototype = { | 244 HasSelector.prototype = { |
| 200 requiresHiding: true, | 245 requiresHiding: true, |
| 201 | 246 |
| 202 get dependsOnStyles() | 247 get dependsOnStyles() |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 213 /** | 258 /** |
| 214 * Generator function returning selected elements. | 259 * Generator function returning selected elements. |
| 215 * @param {string} prefix the prefix for the selector. | 260 * @param {string} prefix the prefix for the selector. |
| 216 * @param {Node} subtree the subtree we work on. | 261 * @param {Node} subtree the subtree we work on. |
| 217 * @param {StringifiedStyle[]} styles the stringified style objects. | 262 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 218 */ | 263 */ |
| 219 *getElements(prefix, subtree, styles) | 264 *getElements(prefix, subtree, styles) |
| 220 { | 265 { |
| 221 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | 266 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
| 222 prefix + "*" : prefix; | 267 prefix + "*" : prefix; |
| 223 let elements = subtree.querySelectorAll(actualPrefix); | 268 let elements = scopedQuerySelectorAll(subtree, actualPrefix); |
| 224 for (let element of elements) | 269 if (elements) |
| 225 { | 270 { |
| 226 let iter = evaluate(this._innerSelectors, 0, "", element, styles); | 271 for (let element of elements) |
| 227 for (let selector of iter) | 272 { |
| 228 { | 273 let iter = evaluate(this._innerSelectors, 0, "", element, styles); |
| 229 if (selector == null) | 274 for (let selector of iter) |
| 230 { | 275 { |
| 231 yield null; | 276 if (selector == null) |
| 232 continue; | 277 yield null; |
| 233 } | 278 else if (scopedQuerySelector(element, selector)) |
| 234 if (relativeSelectorRegexp.test(selector)) | |
| 235 selector = ":scope" + selector; | |
| 236 try | |
| 237 { | |
| 238 if (element.querySelector(selector)) | |
| 239 yield element; | 279 yield element; |
| 240 } | 280 } |
| 241 catch (e) | 281 yield null; |
| 242 { | |
| 243 // :scope isn't supported on Edge, ignore error caused by it. | |
| 244 } | |
| 245 } | 282 } |
| 246 yield null; | |
| 247 } | 283 } |
| 248 } | 284 } |
| 249 }; | 285 }; |
| 250 | 286 |
| 251 function ContainsSelector(textContent) | 287 function ContainsSelector(textContent) |
| 252 { | 288 { |
| 253 this._regexp = makeRegExpParameter(textContent); | 289 this._regexp = makeRegExpParameter(textContent); |
| 254 } | 290 } |
| 255 | 291 |
| 256 ContainsSelector.prototype = { | 292 ContainsSelector.prototype = { |
| 257 requiresHiding: true, | 293 requiresHiding: true, |
| 258 | 294 |
| 259 *getSelectors(prefix, subtree, stylesheet) | 295 *getSelectors(prefix, subtree, stylesheet) |
| 260 { | 296 { |
| 261 for (let element of this.getElements(prefix, subtree, stylesheet)) | 297 for (let element of this.getElements(prefix, subtree, stylesheet)) |
| 262 yield [makeSelector(element, ""), subtree]; | 298 yield [makeSelector(element, ""), subtree]; |
| 263 }, | 299 }, |
| 264 | 300 |
| 265 *getElements(prefix, subtree, stylesheet) | 301 *getElements(prefix, subtree, stylesheet) |
| 266 { | 302 { |
| 267 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | 303 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
| 268 prefix + "*" : prefix; | 304 prefix + "*" : prefix; |
| 269 let elements = subtree.querySelectorAll(actualPrefix); | 305 |
| 270 | 306 let elements = scopedQuerySelectorAll(subtree, actualPrefix); |
| 271 for (let element of elements) | 307 if (elements) |
| 272 { | 308 { |
| 273 if (this._regexp && this._regexp.test(element.textContent)) | 309 for (let element of elements) |
| 274 yield element; | 310 { |
| 275 else | 311 if (this._regexp && this._regexp.test(element.textContent)) |
| 276 yield null; | 312 yield element; |
|
lainverse
2018/01/10 09:14:02
BTW, while we are at it why do we even want to yie
lainverse
2018/01/10 19:10:22
Checked. makeSelectors returns null if null is pas
| |
| 313 else | |
| 314 yield null; | |
| 315 } | |
| 277 } | 316 } |
| 278 } | 317 } |
| 279 }; | 318 }; |
| 280 | 319 |
| 281 function PropsSelector(propertyExpression) | 320 function PropsSelector(propertyExpression) |
| 282 { | 321 { |
| 283 this._regexp = makeRegExpParameter(propertyExpression, "i"); | 322 let regexpString; |
| 323 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
| 324 propertyExpression[propertyExpression.length - 1] == "/") | |
| 325 { | |
| 326 regexpString = propertyExpression.slice(1, -1) | |
| 327 .replace("\\7B ", "{").replace("\\7D ", "}"); | |
| 328 } | |
| 329 else | |
| 330 regexpString = filterToRegExp(propertyExpression); | |
| 331 | |
| 332 this._regexp = new RegExp(regexpString, "i"); | |
| 284 } | 333 } |
| 285 | 334 |
| 286 PropsSelector.prototype = { | 335 PropsSelector.prototype = { |
| 287 preferHideWithSelector: true, | 336 preferHideWithSelector: true, |
| 288 dependsOnStyles: true, | 337 dependsOnStyles: true, |
| 289 | 338 |
| 290 *findPropsSelectors(styles, prefix, regexp) | 339 *findPropsSelectors(styles, prefix, regexp) |
| 291 { | 340 { |
| 292 for (let style of styles) | 341 for (let style of styles) |
| 293 if (regexp.test(style.style)) | 342 if (regexp.test(style.style)) |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 620 characterData: true, | 669 characterData: true, |
| 621 subtree: true | 670 subtree: true |
| 622 } | 671 } |
| 623 ); | 672 ); |
| 624 this.document.addEventListener("load", this.onLoad.bind(this), true); | 673 this.document.addEventListener("load", this.onLoad.bind(this), true); |
| 625 } | 674 } |
| 626 } | 675 } |
| 627 }; | 676 }; |
| 628 | 677 |
| 629 exports.ElemHideEmulation = ElemHideEmulation; | 678 exports.ElemHideEmulation = ElemHideEmulation; |
| LEFT | RIGHT |