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\]/; | 22 let pseudoClassHasSelectorRegExp = /:-abp-has\((.*)\)/; |
| 23 let pseudoClassPropsSelectorRegExp = /:-abp-properties\((["'])([^"']+)\1\)/; |
23 | 24 |
24 function splitSelector(selector) | 25 function splitSelector(selector) |
25 { | 26 { |
26 if (selector.indexOf(",") == -1) | 27 if (selector.indexOf(",") == -1) |
27 return [selector]; | 28 return [selector]; |
28 | 29 |
29 let selectors = []; | 30 let selectors = []; |
30 let start = 0; | 31 let start = 0; |
31 let level = 0; | 32 let level = 0; |
32 let sep = ""; | 33 let sep = ""; |
(...skipping 19 matching lines...) Expand all Loading... |
52 selectors.push(selector.substring(start, i)); | 53 selectors.push(selector.substring(start, i)); |
53 start = i + 1; | 54 start = i + 1; |
54 } | 55 } |
55 } | 56 } |
56 } | 57 } |
57 | 58 |
58 selectors.push(selector.substring(start)); | 59 selectors.push(selector.substring(start)); |
59 return selectors; | 60 return selectors; |
60 } | 61 } |
61 | 62 |
| 63 // Return position of node from parent. |
| 64 // 1 base index like for :nth-child() |
| 65 function positionInParent(node) |
| 66 { |
| 67 let parentNode = node ? node.parentNode : null; |
| 68 if (parentNode == null) |
| 69 return 0; |
| 70 |
| 71 let {children} = parentNode; |
| 72 if (!children) |
| 73 return 0; |
| 74 let i = 0; |
| 75 for (i = 0; i < children.length; i++) |
| 76 if (children[i] == node) |
| 77 break; |
| 78 return i + 1; |
| 79 } |
| 80 |
| 81 function makeSelector(node, selector) |
| 82 { |
| 83 if (node && node.id && node.id != "") |
| 84 { |
| 85 let newSelector = "#" + node.id; |
| 86 if (selector != "") |
| 87 newSelector += " > "; |
| 88 return newSelector + selector; |
| 89 } |
| 90 let idx = positionInParent(node); |
| 91 if (idx > 0) |
| 92 { |
| 93 let newSelector = `${node.tagName}:nth-child(${idx}) `; |
| 94 if (selector != "") |
| 95 newSelector += "> "; |
| 96 return makeSelector(node.parentNode, newSelector + selector); |
| 97 } |
| 98 |
| 99 return selector; |
| 100 } |
| 101 |
| 102 // return the regexString for the properties |
| 103 function parsePropSelPattern(propertyExpression) |
| 104 { |
| 105 let regexpString; |
| 106 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
| 107 propertyExpression[propertyExpression.length - 1] == "/") |
| 108 regexpString = propertyExpression.slice(1, -1) |
| 109 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
| 110 else |
| 111 regexpString = filterToRegExp(propertyExpression); |
| 112 return regexpString; |
| 113 } |
| 114 |
| 115 function parseSelector(selector) |
| 116 { |
| 117 if (selector.length == 0) |
| 118 return []; |
| 119 |
| 120 let abpSelectorIndex = selector.indexOf(":-abp-"); |
| 121 if (abpSelectorIndex == -1) |
| 122 return [new PlainSelector(selector)]; |
| 123 |
| 124 let selectors = []; |
| 125 |
| 126 let match = pseudoClassHasSelectorRegExp.exec(selector); |
| 127 if (match) |
| 128 { |
| 129 selectors.push(...parseSelector(selector.substr(0, match.index))); |
| 130 selectors.push(new HasSelector(match[1])); |
| 131 selectors.push(...parseSelector(selector.substr(match.index + |
| 132 match[0].length))); |
| 133 |
| 134 return selectors; |
| 135 } |
| 136 |
| 137 match = pseudoClassPropsSelectorRegExp.exec(selector); |
| 138 if (match) |
| 139 { |
| 140 selectors.push(...parseSelector(selector.substr(0, match.index))); |
| 141 selectors.push(new PropsSelector(match[2])); |
| 142 selectors.push(...parseSelector(selector.substr(match.index + |
| 143 match[0].length))); |
| 144 |
| 145 return selectors; |
| 146 } |
| 147 |
| 148 selectors.push(new PlainSelector(selector)); |
| 149 return selectors; |
| 150 } |
| 151 |
| 152 function matchStyleProps(style, rule, pattern, selectors, filters) |
| 153 { |
| 154 if (pattern.regexp.test(style)) |
| 155 { |
| 156 let subSelectors = splitSelector(rule.selectorText); |
| 157 for (let i = 0; i < subSelectors.length; i++) |
| 158 { |
| 159 let subSelector = subSelectors[i]; |
| 160 selectors.push(pattern.prefix + subSelector + pattern.suffix); |
| 161 filters.push(pattern.text); |
| 162 } |
| 163 } |
| 164 } |
| 165 |
| 166 function findPropsSelectors(stylesheet, pattern, selectors, filters) |
| 167 { |
| 168 let rules = stylesheet.cssRules; |
| 169 if (!rules) |
| 170 return; |
| 171 |
| 172 for (let rule of rules) |
| 173 { |
| 174 if (rule.type != rule.STYLE_RULE) |
| 175 continue; |
| 176 |
| 177 let style = stringifyStyle(rule.style); |
| 178 matchStyleProps(style, rule, pattern, selectors, filters); |
| 179 } |
| 180 } |
| 181 |
| 182 function stringifyStyle(style) |
| 183 { |
| 184 let styles = []; |
| 185 for (let i = 0; i < style.length; i++) |
| 186 { |
| 187 let property = style.item(i); |
| 188 let value = style.getPropertyValue(property); |
| 189 let priority = style.getPropertyPriority(property); |
| 190 styles.push(property + ": " + value + (priority ? " !" + priority : "") + |
| 191 ";"); |
| 192 } |
| 193 styles.sort(); |
| 194 return styles.join(" "); |
| 195 } |
| 196 |
| 197 function* evaluate(chain, index, prefix, subtree, stylesheet) |
| 198 { |
| 199 if (index >= chain.length) |
| 200 { |
| 201 yield prefix; |
| 202 return; |
| 203 } |
| 204 for (let [selector, element] of |
| 205 chain[index].getSelectors(prefix, subtree, stylesheet)) |
| 206 yield* evaluate(chain, index + 1, selector, element, stylesheet); |
| 207 } |
| 208 |
| 209 /* |
| 210 * getSelector() is a generator function returning a pair of selector |
| 211 * string and subtree. |
| 212 * getElements() is a generator function returning elements selected. |
| 213 */ |
| 214 function PlainSelector(selector) |
| 215 { |
| 216 this._selector = selector; |
| 217 } |
| 218 |
| 219 PlainSelector.prototype = { |
| 220 *getSelectors(prefix, subtree, stylesheet) |
| 221 { |
| 222 yield [prefix + this._selector, subtree]; |
| 223 }, |
| 224 |
| 225 *getElements(prefix, subtree, stylesheet) |
| 226 { |
| 227 for (let selector of this.getSelectors(prefix, subtree, stylesheet)) |
| 228 for (let element of subtree.querySelectorAll(selector[0])) |
| 229 yield element; |
| 230 } |
| 231 }; |
| 232 |
| 233 function HasSelector(selector) |
| 234 { |
| 235 this._innerSelectors = parseSelector(selector); |
| 236 } |
| 237 |
| 238 HasSelector.prototype = { |
| 239 *getSelectors(prefix, subtree, stylesheet) |
| 240 { |
| 241 for (let element of this.getElements(prefix, subtree, stylesheet)) |
| 242 { |
| 243 let selector = makeSelector(element, ""); |
| 244 yield [prefix + selector, subtree]; |
| 245 } |
| 246 }, |
| 247 |
| 248 *getElements(prefix, subtree, stylesheet) |
| 249 { |
| 250 let elements = subtree.querySelectorAll(prefix ? prefix : "*"); |
| 251 for (let element of elements) |
| 252 { |
| 253 let newPrefix = makeSelector(element, ""); |
| 254 let iter = evaluate(this._innerSelectors, 0, "", element, stylesheet); |
| 255 for (let selector of iter) { |
| 256 // we insert a space between the two. It becomes a no-op if selector |
| 257 // doesn't have a combinator |
| 258 if (subtree.querySelector(newPrefix + " " + selector)) |
| 259 yield element; |
| 260 } |
| 261 } |
| 262 } |
| 263 }; |
| 264 |
| 265 function PropsSelector(selector) |
| 266 { |
| 267 this._regexp = new RegExp(parsePropSelPattern(selector), "i"); |
| 268 } |
| 269 |
| 270 PropsSelector.prototype = { |
| 271 *getSelectors(prefix, subtree, stylesheet) |
| 272 { |
| 273 let selectors = []; |
| 274 let filters = []; |
| 275 let selPattern = { |
| 276 prefix, |
| 277 suffix: "", |
| 278 regexp: this._regexp |
| 279 }; |
| 280 |
| 281 findPropsSelectors(stylesheet, selPattern, selectors, filters); |
| 282 for (let selector of selectors) |
| 283 yield [selector, subtree]; |
| 284 }, |
| 285 |
| 286 *getElements(prefix, subtree, stylesheet) |
| 287 { |
| 288 for (let [selector, element] of |
| 289 this.getSelectors(prefix, subtree, stylesheet)) |
| 290 { |
| 291 for (let subElement of element.querySelectorAll(selector)) |
| 292 yield subElement; |
| 293 } |
| 294 } |
| 295 }; |
| 296 |
62 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | 297 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) |
63 { | 298 { |
64 this.window = window; | 299 this.window = window; |
65 this.getFiltersFunc = getFiltersFunc; | 300 this.getFiltersFunc = getFiltersFunc; |
66 this.addSelectorsFunc = addSelectorsFunc; | 301 this.addSelectorsFunc = addSelectorsFunc; |
67 } | 302 } |
68 | 303 |
69 ElemHideEmulation.prototype = { | 304 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 | 305 |
85 isSameOrigin(stylesheet) | 306 isSameOrigin(stylesheet) |
86 { | 307 { |
87 try | 308 try |
88 { | 309 { |
89 return new URL(stylesheet.href).origin == this.window.location.origin; | 310 return new URL(stylesheet.href).origin == this.window.location.origin; |
90 } | 311 } |
91 catch (e) | 312 catch (e) |
92 { | 313 { |
93 // Invalid URL, assume that it is first-party. | 314 // Invalid URL, assume that it is first-party. |
94 return true; | 315 return true; |
95 } | 316 } |
96 }, | 317 }, |
97 | 318 |
98 findSelectors(stylesheet, selectors, filters) | 319 addSelectors(stylesheet) |
99 { | 320 { |
| 321 let selectors = []; |
| 322 let filters = []; |
| 323 |
100 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 324 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
101 // between Firefox and Chrome. | 325 // between Firefox and Chrome. |
102 if (!this.isSameOrigin(stylesheet)) | 326 if (!this.isSameOrigin(stylesheet)) |
103 return; | 327 return; |
104 | 328 |
105 let rules = stylesheet.cssRules; | 329 for (let patterns of this.selPatterns) |
106 if (!rules) | 330 selectors.push(...evaluate(patterns, 0, "", document, stylesheet)); |
107 return; | 331 |
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) | |
131 { | |
132 let selectors = []; | |
133 let filters = []; | |
134 for (let stylesheet of stylesheets) | |
135 this.findSelectors(stylesheet, selectors, filters); | |
136 this.addSelectorsFunc(selectors, filters); | 332 this.addSelectorsFunc(selectors, filters); |
137 }, | 333 }, |
138 | 334 |
139 onLoad(event) | 335 onLoad(event) |
140 { | 336 { |
141 let stylesheet = event.target.sheet; | 337 let stylesheet = event.target.sheet; |
142 if (stylesheet) | 338 if (stylesheet) |
143 this.addSelectors([stylesheet]); | 339 this.addSelectors(stylesheet); |
144 }, | 340 }, |
145 | 341 |
146 apply() | 342 apply() |
147 { | 343 { |
148 this.getFiltersFunc(patterns => | 344 this.getFiltersFunc(patterns => |
149 { | 345 { |
150 this.patterns = []; | 346 this.selPatterns = []; |
| 347 |
151 for (let pattern of patterns) | 348 for (let pattern of patterns) |
152 { | 349 { |
153 let match = propertySelectorRegExp.exec(pattern.selector); | 350 let selectors = parseSelector(pattern.selector); |
154 if (!match) | 351 if (selectors.length > 0) |
155 continue; | 352 this.selPatterns.push(selectors); |
156 | |
157 let propertyExpression = match[2]; | |
158 let regexpString; | |
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 } | 353 } |
175 | 354 |
176 if (this.patterns.length > 0) | 355 if (this.selPatterns.length > 0) |
177 { | 356 { |
178 let {document} = this.window; | 357 let {document} = this.window; |
179 this.addSelectors(document.styleSheets); | 358 for (let stylesheet of document.styleSheets) |
| 359 this.addSelectors(stylesheet); |
180 document.addEventListener("load", this.onLoad.bind(this), true); | 360 document.addEventListener("load", this.onLoad.bind(this), true); |
181 } | 361 } |
182 }); | 362 }); |
183 } | 363 } |
184 }; | 364 }; |
OLD | NEW |