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 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 { | 86 { |
87 let newSelector = `${node.tagName}:nth-child(${idx})`; | 87 let newSelector = `${node.tagName}:nth-child(${idx})`; |
88 if (selector) | 88 if (selector) |
89 newSelector += " > "; | 89 newSelector += " > "; |
90 return makeSelector(node.parentElement, newSelector + selector); | 90 return makeSelector(node.parentElement, newSelector + selector); |
91 } | 91 } |
92 | 92 |
93 return selector; | 93 return selector; |
94 } | 94 } |
95 | 95 |
96 const abpSelectorRegexp = /:-abp-(properties|has|[A-Za-z\d-]*)\(/i; | 96 const abpSelectorRegexp = /:-abp-(properties|has|contains|[A-Za-z\d-]*)\(/i; |
97 | 97 |
98 function parseSelectorContent(content, quoted = false) | 98 function parseSelectorContent(content, quoted = false) |
99 { | 99 { |
100 let parens = 1; | 100 let parens = 1; |
101 let i = 0; | 101 let i = 0; |
102 let quote = null; | 102 let quote = null; |
103 let originalLength = content.length; | 103 let originalLength = content.length; |
104 if (quoted) | 104 if (quoted) |
105 content = content.trim(); | 105 content = content.trim(); |
106 while (i < content.length) | 106 while (i < content.length) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 `selector ${selector}, didn't ` + | 189 `selector ${selector}, didn't ` + |
190 "find closing parenthesis.")); | 190 "find closing parenthesis.")); |
191 return null; | 191 return null; |
192 } | 192 } |
193 | 193 |
194 let hasSelector = new HasSelector(content.text); | 194 let hasSelector = new HasSelector(content.text); |
195 if (!hasSelector.valid()) | 195 if (!hasSelector.valid()) |
196 return null; | 196 return null; |
197 selectors.push(hasSelector); | 197 selectors.push(hasSelector); |
198 } | 198 } |
| 199 else if (match[1] == "contains") |
| 200 { |
| 201 content = parseSelectorContent(selector.substr(startIndex), true); |
| 202 |
| 203 selectors.push(new ContainsSelector(content.text)); |
| 204 } |
199 else | 205 else |
200 { | 206 { |
201 // this is an error, can't parse selector. | 207 // this is an error, can't parse selector. |
202 console.error(new SyntaxError("Failed parsing AdBlock Plus " + | 208 console.error(new SyntaxError("Failed parsing AdBlock Plus " + |
203 `selector ${selector}, invalid ` + | 209 `selector ${selector}, invalid ` + |
204 `pseudo-class -abp-${match[1]}.`)); | 210 `pseudo-class -abp-${match[1]}.`)); |
205 return null; | 211 return null; |
206 } | 212 } |
207 | 213 |
208 suffixStart = startIndex + content.end + 1; | 214 suffixStart = startIndex + content.end + 1; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 let iter = evaluate(this._innerSelectors, 0, "", element, styles); | 319 let iter = evaluate(this._innerSelectors, 0, "", element, styles); |
314 for (let selector of iter) | 320 for (let selector of iter) |
315 // we insert a space between the two. It becomes a no-op if selector | 321 // we insert a space between the two. It becomes a no-op if selector |
316 // doesn't have a combinator | 322 // doesn't have a combinator |
317 if (subtree.querySelector(newPrefix + " " + selector)) | 323 if (subtree.querySelector(newPrefix + " " + selector)) |
318 yield element; | 324 yield element; |
319 } | 325 } |
320 } | 326 } |
321 }; | 327 }; |
322 | 328 |
| 329 function ContainsSelector(textContent) |
| 330 { |
| 331 this._text = textContent; |
| 332 } |
| 333 |
| 334 ContainsSelector.prototype = { |
| 335 |
| 336 *getSelectors(prefix, subtree, stylesheet) |
| 337 { |
| 338 for (let element of this.getElements(prefix, subtree, stylesheet)) |
| 339 yield [makeSelector(element, ""), subtree]; |
| 340 }, |
| 341 |
| 342 *getElements(prefix, subtree, stylesheet) |
| 343 { |
| 344 let actualPrefix = (!prefix || prefixEndRegexp.test(prefix)) ? |
| 345 prefix + "*" : prefix; |
| 346 let elements = subtree.querySelectorAll(actualPrefix); |
| 347 for (let element of elements) |
| 348 if (element.textContent == this._text) |
| 349 yield element; |
| 350 } |
| 351 }; |
| 352 |
323 function PropsSelector(propertyExpression) | 353 function PropsSelector(propertyExpression) |
324 { | 354 { |
325 let regexpString; | 355 let regexpString; |
326 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | 356 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
327 propertyExpression[propertyExpression.length - 1] == "/") | 357 propertyExpression[propertyExpression.length - 1] == "/") |
328 { | 358 { |
329 regexpString = propertyExpression.slice(1, -1) | 359 regexpString = propertyExpression.slice(1, -1) |
330 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | 360 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
331 } | 361 } |
332 else | 362 else |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 | 463 |
434 if (this.selPatterns.length > 0) | 464 if (this.selPatterns.length > 0) |
435 { | 465 { |
436 let {document} = this.window; | 466 let {document} = this.window; |
437 this.addSelectors(document.styleSheets); | 467 this.addSelectors(document.styleSheets); |
438 document.addEventListener("load", this.onLoad.bind(this), true); | 468 document.addEventListener("load", this.onLoad.bind(this), true); |
439 } | 469 } |
440 }); | 470 }); |
441 } | 471 } |
442 }; | 472 }; |
OLD | NEW |