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