 Issue 29737558:
  Issue 6538, 6781 - Implement support for snippet filters  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 29737558:
  Issue 6538, 6781 - Implement support for snippet filters  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| 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 | 
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 } | 77 } | 
| 78 }; | 78 }; | 
| 79 | 79 | 
| 80 /** | 80 /** | 
| 81 * Cache for known filters, maps string representation to filter objects. | 81 * Cache for known filters, maps string representation to filter objects. | 
| 82 * @type {Map.<string,Filter>} | 82 * @type {Map.<string,Filter>} | 
| 83 */ | 83 */ | 
| 84 Filter.knownFilters = new Map(); | 84 Filter.knownFilters = new Map(); | 
| 85 | 85 | 
| 86 /** | 86 /** | 
| 87 * Regular expression that code injection filters should match | 87 * Regular expression that content filters should match | 
| 88 * @type {RegExp} | 88 * @type {RegExp} | 
| 89 */ | 89 */ | 
| 90 Filter.codeInjectionRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; | 90 Filter.contentRegExp = /^([^/*|@"!]*?)#([@?$])?#(.+)$/; | 
| 91 /** | 91 /** | 
| 92 * Regular expression that RegExp filters specified as RegExps should match | 92 * Regular expression that RegExp filters specified as RegExps should match | 
| 93 * @type {RegExp} | 93 * @type {RegExp} | 
| 94 */ | 94 */ | 
| 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; | 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; | 
| 96 /** | 96 /** | 
| 97 * Regular expression that options on a RegExp filter should match | 97 * Regular expression that options on a RegExp filter should match | 
| 98 * @type {RegExp} | 98 * @type {RegExp} | 
| 99 */ | 99 */ | 
| 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,]+)?(?:,~?[\w-]+(?:=[^,]+)?)*)$/; | 
| 101 /** | 101 /** | 
| 102 * Regular expression that matches an invalid Content Security Policy | 102 * Regular expression that matches an invalid Content Security Policy | 
| 103 * @type {RegExp} | 103 * @type {RegExp} | 
| 104 */ | 104 */ | 
| 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | 105 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | 
| 106 | 106 | 
| 107 /** | 107 /** | 
| 108 * Creates a filter of correct type from its text representation - does the | 108 * Creates a filter of correct type from its text representation - does the | 
| 109 * basic parsing and calls the right constructor then. | 109 * basic parsing and calls the right constructor then. | 
| 110 * | 110 * | 
| 111 * @param {string} text as in Filter() | 111 * @param {string} text as in Filter() | 
| 112 * @return {Filter} | 112 * @return {Filter} | 
| 113 */ | 113 */ | 
| 114 Filter.fromText = function(text) | 114 Filter.fromText = function(text) | 
| 115 { | 115 { | 
| 116 let filter = Filter.knownFilters.get(text); | 116 let filter = Filter.knownFilters.get(text); | 
| 117 if (filter) | 117 if (filter) | 
| 118 return filter; | 118 return filter; | 
| 119 | 119 | 
| 120 let match = text.includes("#") ? Filter.codeInjectionRegExp.exec(text) : null; | 120 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; | 
| 121 if (match) | 121 if (match) | 
| 122 { | 122 { | 
| 123 let propsMatch; | 123 let propsMatch; | 
| 124 if (!match[2] && | 124 if (!match[2] && | 
| 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 125 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) | 
| 126 { | 126 { | 
| 127 // This is legacy CSS properties syntax, convert to current syntax | 127 // This is legacy CSS properties syntax, convert to current syntax | 
| 128 let prefix = match[3].substr(0, propsMatch.index); | 128 let prefix = match[3].substr(0, propsMatch.index); | 
| 129 let expression = propsMatch[2]; | 129 let expression = propsMatch[2]; | 
| 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 130 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); | 
| 131 return Filter.fromText(`${match[1]}#?#` + | 131 return Filter.fromText(`${match[1]}#?#` + | 
| 132 `${prefix}:-abp-properties(${expression})${suffix}`); | 132 `${prefix}:-abp-properties(${expression})${suffix}`); | 
| 133 } | 133 } | 
| 134 | 134 | 
| 135 if (match[2] == "$") | 135 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); | 
| 136 filter = new SnippetFilter(text, match[1], match[3]); | |
| 137 else | |
| 138 filter = ElemHideBase.fromText(text, match[1], match[2], match[3]); | |
| 139 } | 136 } | 
| 140 else if (text[0] == "!") | 137 else if (text[0] == "!") | 
| 141 filter = new CommentFilter(text); | 138 filter = new CommentFilter(text); | 
| 142 else | 139 else | 
| 143 filter = RegExpFilter.fromText(text); | 140 filter = RegExpFilter.fromText(text); | 
| 144 | 141 | 
| 145 Filter.knownFilters.set(filter.text, filter); | 142 Filter.knownFilters.set(filter.text, filter); | 
| 146 return filter; | 143 return filter; | 
| 147 }; | 144 }; | 
| 148 | 145 | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 178 if (!text) | 175 if (!text) | 
| 179 return text; | 176 return text; | 
| 180 | 177 | 
| 181 // Remove line breaks, tabs etc | 178 // Remove line breaks, tabs etc | 
| 182 text = text.replace(/[^\S ]+/g, ""); | 179 text = text.replace(/[^\S ]+/g, ""); | 
| 183 | 180 | 
| 184 // Don't remove spaces inside comments | 181 // Don't remove spaces inside comments | 
| 185 if (/^ *!/.test(text)) | 182 if (/^ *!/.test(text)) | 
| 186 return text.trim(); | 183 return text.trim(); | 
| 187 | 184 | 
| 188 // Special treatment for code injection filters, right side is allowed to | 185 // Special treatment for content filters, right side is allowed to contain | 
| 189 // contain spaces | 186 // spaces | 
| 190 if (Filter.codeInjectionRegExp.test(text)) | 187 if (Filter.contentRegExp.test(text)) | 
| 191 { | 188 { | 
| 192 let [, domains, separator, code] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); | 189 let [, domains, separator, body] = /^(.*?)(#[@?$]?#?)(.*)$/.exec(text); | 
| 193 return domains.replace(/ +/g, "") + separator + code.trim(); | 190 return domains.replace(/ +/g, "") + separator + body.trim(); | 
| 194 } | 191 } | 
| 195 | 192 | 
| 196 // For most regexp filters we strip all spaces, but $csp filter options | 193 // For most regexp filters we strip all spaces, but $csp filter options | 
| 197 // are allowed to contain single (non trailing) spaces. | 194 // are allowed to contain single (non trailing) spaces. | 
| 198 let strippedText = text.replace(/ +/g, ""); | 195 let strippedText = text.replace(/ +/g, ""); | 
| 199 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 196 if (!strippedText.includes("$") || !/\bcsp=/i.test(strippedText)) | 
| 200 return strippedText; | 197 return strippedText; | 
| 201 | 198 | 
| 202 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 199 let optionsMatch = Filter.optionsRegExp.exec(strippedText); | 
| 203 if (!optionsMatch) | 200 if (!optionsMatch) | 
| (...skipping 30 matching lines...) Expand all Loading... | |
| 234 return beforeOptions + "$" + options.join(); | 231 return beforeOptions + "$" + options.join(); | 
| 235 }; | 232 }; | 
| 236 | 233 | 
| 237 /** | 234 /** | 
| 238 * @see filterToRegExp | 235 * @see filterToRegExp | 
| 239 */ | 236 */ | 
| 240 Filter.toRegExp = filterToRegExp; | 237 Filter.toRegExp = filterToRegExp; | 
| 241 | 238 | 
| 242 /** | 239 /** | 
| 243 * Class for invalid filters | 240 * Class for invalid filters | 
| 244 * @param {string} text see Filter() | 241 * @param {string} text see {@link Filter Filter()} | 
| 245 * @param {string} reason Reason why this filter is invalid | 242 * @param {string} reason Reason why this filter is invalid | 
| 246 * @constructor | 243 * @constructor | 
| 247 * @augments Filter | 244 * @augments Filter | 
| 248 */ | 245 */ | 
| 249 function InvalidFilter(text, reason) | 246 function InvalidFilter(text, reason) | 
| 250 { | 247 { | 
| 251 Filter.call(this, text); | 248 Filter.call(this, text); | 
| 252 | 249 | 
| 253 this.reason = reason; | 250 this.reason = reason; | 
| 254 } | 251 } | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 265 | 262 | 
| 266 /** | 263 /** | 
| 267 * See Filter.serialize() | 264 * See Filter.serialize() | 
| 268 * @inheritdoc | 265 * @inheritdoc | 
| 269 */ | 266 */ | 
| 270 serialize(buffer) {} | 267 serialize(buffer) {} | 
| 271 }); | 268 }); | 
| 272 | 269 | 
| 273 /** | 270 /** | 
| 274 * Class for comments | 271 * Class for comments | 
| 275 * @param {string} text see Filter() | 272 * @param {string} text see {@link Filter Filter()} | 
| 276 * @constructor | 273 * @constructor | 
| 277 * @augments Filter | 274 * @augments Filter | 
| 278 */ | 275 */ | 
| 279 function CommentFilter(text) | 276 function CommentFilter(text) | 
| 280 { | 277 { | 
| 281 Filter.call(this, text); | 278 Filter.call(this, text); | 
| 282 } | 279 } | 
| 283 exports.CommentFilter = CommentFilter; | 280 exports.CommentFilter = CommentFilter; | 
| 284 | 281 | 
| 285 CommentFilter.prototype = extend(Filter, { | 282 CommentFilter.prototype = extend(Filter, { | 
| 286 type: "comment", | 283 type: "comment", | 
| 287 | 284 | 
| 288 /** | 285 /** | 
| 289 * See Filter.serialize() | 286 * See Filter.serialize() | 
| 290 * @inheritdoc | 287 * @inheritdoc | 
| 291 */ | 288 */ | 
| 292 serialize(buffer) {} | 289 serialize(buffer) {} | 
| 293 }); | 290 }); | 
| 294 | 291 | 
| 295 /** | 292 /** | 
| 296 * Abstract base class for filters that can get hits | 293 * Abstract base class for filters that can get hits | 
| 297 * @param {string} text | 294 * @param {string} text | 
| 298 * see Filter() | 295 * see {@link Filter Filter()} | 
| 299 * @param {string} [domains] | 296 * @param {string} [domains] | 
| 300 * Domains that the filter is restricted to separated by domainSeparator | 297 * Domains that the filter is restricted to separated by domainSeparator | 
| 301 * e.g. "foo.com|bar.com|~baz.com" | 298 * e.g. "foo.com|bar.com|~baz.com" | 
| 302 * @constructor | 299 * @constructor | 
| 303 * @augments Filter | 300 * @augments Filter | 
| 304 */ | 301 */ | 
| 305 function ActiveFilter(text, domains) | 302 function ActiveFilter(text, domains) | 
| 306 { | 303 { | 
| 307 Filter.call(this, text); | 304 Filter.call(this, text); | 
| 308 | 305 | 
| 309 this.domainSource = domains; | 306 if (domains) | 
| 307 this.domainSource = domains; | |
| 310 } | 308 } | 
| 311 exports.ActiveFilter = ActiveFilter; | 309 exports.ActiveFilter = ActiveFilter; | 
| 312 | 310 | 
| 313 ActiveFilter.prototype = extend(Filter, { | 311 ActiveFilter.prototype = extend(Filter, { | 
| 314 _disabled: false, | 312 _disabled: false, | 
| 315 _hitCount: 0, | 313 _hitCount: 0, | 
| 316 _lastHit: 0, | 314 _lastHit: 0, | 
| 317 | 315 | 
| 318 /** | 316 /** | 
| 319 * Defines whether the filter is disabled | 317 * Defines whether the filter is disabled | 
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 { | 366 { | 
| 369 let oldValue = this._lastHit; | 367 let oldValue = this._lastHit; | 
| 370 this._lastHit = value; | 368 this._lastHit = value; | 
| 371 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue); | 369 FilterNotifier.triggerListeners("filter.lastHit", this, value, oldValue); | 
| 372 } | 370 } | 
| 373 return this._lastHit; | 371 return this._lastHit; | 
| 374 }, | 372 }, | 
| 375 | 373 | 
| 376 /** | 374 /** | 
| 377 * String that the domains property should be generated from | 375 * String that the domains property should be generated from | 
| 378 * @type {string} | 376 * @type {?string} | 
| 379 */ | 377 */ | 
| 380 domainSource: null, | 378 domainSource: null, | 
| 381 | 379 | 
| 382 /** | 380 /** | 
| 383 * Separator character used in domainSource property, must be | 381 * Separator character used in domainSource property, must be | 
| 384 * overridden by subclasses | 382 * overridden by subclasses | 
| 385 * @type {string} | 383 * @type {string} | 
| 386 */ | 384 */ | 
| 387 domainSeparator: null, | 385 domainSeparator: null, | 
| 388 | 386 | 
| 389 /** | 387 /** | 
| 390 * Determines whether the trailing dot in domain names isn't important and | 388 * Determines whether domainSource is already lower-case, | 
| 391 * should be ignored, must be overridden by subclasses. | |
| 392 * @type {boolean} | |
| 393 */ | |
| 394 ignoreTrailingDot: true, | |
| 395 | |
| 396 /** | |
| 397 * Determines whether domainSource is already upper-case, | |
| 398 * can be overridden by subclasses. | 389 * can be overridden by subclasses. | 
| 399 * @type {boolean} | 390 * @type {boolean} | 
| 400 */ | 391 */ | 
| 401 domainSourceIsUpperCase: false, | 392 domainSourceIsLowerCase: false, | 
| 402 | 393 | 
| 403 /** | 394 /** | 
| 404 * Map containing domains that this filter should match on/not match | 395 * Map containing domains that this filter should match on/not match | 
| 405 * on or null if the filter should match on all domains | 396 * on or null if the filter should match on all domains | 
| 406 * @type {?Map.<string,boolean>} | 397 * @type {?Map.<string,boolean>} | 
| 407 */ | 398 */ | 
| 408 get domains() | 399 get domains() | 
| 409 { | 400 { | 
| 410 // Despite this property being cached, the getter is called | 401 let prop = Object.getOwnPropertyDescriptor(this, "_domains"); | 
| 411 // several times on Safari, due to WebKit bug 132872 | |
| 412 let prop = Object.getOwnPropertyDescriptor(this, "domains"); | |
| 413 if (prop) | 402 if (prop) | 
| 414 return prop.value; | 403 { | 
| 404 let {value} = prop; | |
| 405 return typeof value == "string" ? | |
| 406 new Map([[value, true], ["", false]]) : value; | |
| 407 } | |
| 415 | 408 | 
| 416 let domains = null; | 409 let domains = null; | 
| 417 | 410 | 
| 418 if (this.domainSource) | 411 if (this.domainSource) | 
| 419 { | 412 { | 
| 420 let source = this.domainSource; | 413 let source = this.domainSource; | 
| 421 if (!this.domainSourceIsUpperCase) | 414 if (!this.domainSourceIsLowerCase) | 
| 422 { | 415 { | 
| 423 // RegExpFilter already have uppercase domains | 416 // RegExpFilter already have lowercase domains | 
| 424 source = source.toUpperCase(); | 417 source = source.toLowerCase(); | 
| 425 } | 418 } | 
| 426 let list = source.split(this.domainSeparator); | 419 let list = source.split(this.domainSeparator); | 
| 427 if (list.length == 1 && list[0][0] != "~") | 420 if (list.length == 1 && list[0][0] != "~") | 
| 428 { | 421 { | 
| 429 // Fast track for the common one-domain scenario | 422 // Fast track for the common one-domain scenario | 
| 430 if (this.ignoreTrailingDot) | 423 domains = list[0]; | 
| 431 list[0] = list[0].replace(/\.+$/, ""); | |
| 432 domains = new Map([["", false], [list[0], true]]); | |
| 433 } | 424 } | 
| 434 else | 425 else | 
| 435 { | 426 { | 
| 436 let hasIncludes = false; | 427 let hasIncludes = false; | 
| 437 for (let i = 0; i < list.length; i++) | 428 for (let i = 0; i < list.length; i++) | 
| 438 { | 429 { | 
| 439 let domain = list[i]; | 430 let domain = list[i]; | 
| 440 if (this.ignoreTrailingDot) | |
| 441 domain = domain.replace(/\.+$/, ""); | |
| 442 if (domain == "") | 431 if (domain == "") | 
| 443 continue; | 432 continue; | 
| 444 | 433 | 
| 445 let include; | 434 let include; | 
| 446 if (domain[0] == "~") | 435 if (domain[0] == "~") | 
| 447 { | 436 { | 
| 448 include = false; | 437 include = false; | 
| 449 domain = domain.substr(1); | 438 domain = domain.substr(1); | 
| 450 } | 439 } | 
| 451 else | 440 else | 
| 452 { | 441 { | 
| 453 include = true; | 442 include = true; | 
| 454 hasIncludes = true; | 443 hasIncludes = true; | 
| 455 } | 444 } | 
| 456 | 445 | 
| 457 if (!domains) | 446 if (!domains) | 
| 458 domains = new Map(); | 447 domains = new Map(); | 
| 459 | 448 | 
| 460 domains.set(domain, include); | 449 domains.set(domain, include); | 
| 461 } | 450 } | 
| 462 if (domains) | 451 if (domains) | 
| 463 domains.set("", !hasIncludes); | 452 domains.set("", !hasIncludes); | 
| 464 } | 453 } | 
| 465 | 454 | 
| 466 this.domainSource = null; | 455 this.domainSource = null; | 
| 467 } | 456 } | 
| 468 | 457 | 
| 469 Object.defineProperty(this, "domains", {value: domains, enumerable: true}); | 458 Object.defineProperty(this, "_domains", {value: domains}); | 
| 470 return this.domains; | 459 return this.domains; | 
| 471 }, | 460 }, | 
| 472 | 461 | 
| 473 /** | 462 /** | 
| 474 * Array containing public keys of websites that this filter should apply to | 463 * Array containing public keys of websites that this filter should apply to | 
| 475 * @type {string[]} | 464 * @type {?string[]} | 
| 476 */ | 465 */ | 
| 477 sitekeys: null, | 466 sitekeys: null, | 
| 478 | 467 | 
| 479 /** | 468 /** | 
| 480 * Checks whether this filter is active on a domain. | 469 * Checks whether this filter is active on a domain. | 
| 481 * @param {string} docDomain domain name of the document that loads the URL | 470 * @param {string} [docDomain] domain name of the document that loads the URL | 
| 482 * @param {string} [sitekey] public key provided by the document | 471 * @param {string} [sitekey] public key provided by the document | 
| 483 * @return {boolean} true in case of the filter being active | 472 * @return {boolean} true in case of the filter being active | 
| 484 */ | 473 */ | 
| 485 isActiveOnDomain(docDomain, sitekey) | 474 isActiveOnDomain(docDomain, sitekey) | 
| 486 { | 475 { | 
| 487 // Sitekeys are case-sensitive so we shouldn't convert them to | 476 // Sitekeys are case-sensitive so we shouldn't convert them to | 
| 488 // upper-case to avoid false positives here. Instead we need to | 477 // upper-case to avoid false positives here. Instead we need to | 
| 489 // change the way filter options are parsed. | 478 // change the way filter options are parsed. | 
| 490 if (this.sitekeys && | 479 if (this.sitekeys && | 
| 491 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) | 480 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) | 
| 492 { | 481 { | 
| 493 return false; | 482 return false; | 
| 494 } | 483 } | 
| 495 | 484 | 
| 496 // If no domains are set the rule matches everywhere | 485 // If no domains are set the rule matches everywhere | 
| 497 if (!this.domains) | 486 if (!this.domains) | 
| 498 return true; | 487 return true; | 
| 499 | 488 | 
| 500 // If the document has no host name, match only if the filter | 489 // If the document has no host name, match only if the filter | 
| 501 // isn't restricted to specific domains | 490 // isn't restricted to specific domains | 
| 502 if (!docDomain) | 491 if (!docDomain) | 
| 503 return this.domains.get(""); | 492 return this.domains.get(""); | 
| 504 | 493 | 
| 505 if (this.ignoreTrailingDot) | 494 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); | 
| 506 docDomain = docDomain.replace(/\.+$/, ""); | |
| 507 docDomain = docDomain.toUpperCase(); | |
| 508 | 495 | 
| 509 while (true) | 496 while (true) | 
| 510 { | 497 { | 
| 511 let isDomainIncluded = this.domains.get(docDomain); | 498 let isDomainIncluded = this.domains.get(docDomain); | 
| 512 if (typeof isDomainIncluded != "undefined") | 499 if (typeof isDomainIncluded != "undefined") | 
| 513 return isDomainIncluded; | 500 return isDomainIncluded; | 
| 514 | 501 | 
| 515 let nextDot = docDomain.indexOf("."); | 502 let nextDot = docDomain.indexOf("."); | 
| 516 if (nextDot < 0) | 503 if (nextDot < 0) | 
| 517 break; | 504 break; | 
| 518 docDomain = docDomain.substr(nextDot + 1); | 505 docDomain = docDomain.substr(nextDot + 1); | 
| 519 } | 506 } | 
| 520 return this.domains.get(""); | 507 return this.domains.get(""); | 
| 521 }, | 508 }, | 
| 522 | 509 | 
| 523 /** | 510 /** | 
| 524 * Checks whether this filter is active only on a domain and its subdomains. | 511 * Checks whether this filter is active only on a domain and its subdomains. | 
| 525 * @param {string} docDomain | 512 * @param {string} docDomain | 
| 526 * @return {boolean} | 513 * @return {boolean} | 
| 527 */ | 514 */ | 
| 528 isActiveOnlyOnDomain(docDomain) | 515 isActiveOnlyOnDomain(docDomain) | 
| 529 { | 516 { | 
| 530 if (!docDomain || !this.domains || this.domains.get("")) | 517 if (!docDomain || !this.domains || this.domains.get("")) | 
| 531 return false; | 518 return false; | 
| 532 | 519 | 
| 533 if (this.ignoreTrailingDot) | 520 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); | 
| 534 docDomain = docDomain.replace(/\.+$/, ""); | |
| 535 docDomain = docDomain.toUpperCase(); | |
| 536 | 521 | 
| 537 for (let [domain, isIncluded] of this.domains) | 522 for (let [domain, isIncluded] of this.domains) | 
| 538 { | 523 { | 
| 539 if (isIncluded && domain != docDomain) | 524 if (isIncluded && domain != docDomain) | 
| 540 { | 525 { | 
| 541 if (domain.length <= docDomain.length) | 526 if (domain.length <= docDomain.length) | 
| 542 return false; | 527 return false; | 
| 543 | 528 | 
| 544 if (!domain.endsWith("." + docDomain)) | 529 if (!domain.endsWith("." + docDomain)) | 
| 545 return false; | 530 return false; | 
| (...skipping 27 matching lines...) Expand all Loading... | |
| 573 if (this._hitCount) | 558 if (this._hitCount) | 
| 574 buffer.push("hitCount=" + this._hitCount); | 559 buffer.push("hitCount=" + this._hitCount); | 
| 575 if (this._lastHit) | 560 if (this._lastHit) | 
| 576 buffer.push("lastHit=" + this._lastHit); | 561 buffer.push("lastHit=" + this._lastHit); | 
| 577 } | 562 } | 
| 578 } | 563 } | 
| 579 }); | 564 }); | 
| 580 | 565 | 
| 581 /** | 566 /** | 
| 582 * Abstract base class for RegExp-based filters | 567 * Abstract base class for RegExp-based filters | 
| 583 * @param {string} text see Filter() | 568 * @param {string} text see {@link Filter Filter()} | 
| 584 * @param {string} regexpSource | 569 * @param {string} regexpSource | 
| 585 * filter part that the regular expression should be build from | 570 * filter part that the regular expression should be build from | 
| 586 * @param {number} [contentType] | 571 * @param {number} [contentType] | 
| 587 * Content types the filter applies to, combination of values from | 572 * Content types the filter applies to, combination of values from | 
| 588 * RegExpFilter.typeMap | 573 * RegExpFilter.typeMap | 
| 589 * @param {boolean} [matchCase] | 574 * @param {boolean} [matchCase] | 
| 590 * Defines whether the filter should distinguish between lower and upper case | 575 * Defines whether the filter should distinguish between lower and upper case | 
| 591 * letters | 576 * letters | 
| 592 * @param {string} [domains] | 577 * @param {string} [domains] | 
| 593 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 578 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 626 else | 611 else | 
| 627 { | 612 { | 
| 628 // No need to convert this filter to regular expression yet, do it on demand | 613 // No need to convert this filter to regular expression yet, do it on demand | 
| 629 this.regexpSource = regexpSource; | 614 this.regexpSource = regexpSource; | 
| 630 } | 615 } | 
| 631 } | 616 } | 
| 632 exports.RegExpFilter = RegExpFilter; | 617 exports.RegExpFilter = RegExpFilter; | 
| 633 | 618 | 
| 634 RegExpFilter.prototype = extend(ActiveFilter, { | 619 RegExpFilter.prototype = extend(ActiveFilter, { | 
| 635 /** | 620 /** | 
| 636 * @see ActiveFilter.domainSourceIsUpperCase | 621 * @see ActiveFilter.domainSourceIsLowerCase | 
| 637 */ | 622 */ | 
| 638 domainSourceIsUpperCase: true, | 623 domainSourceIsLowerCase: true, | 
| 639 | 624 | 
| 640 /** | 625 /** | 
| 641 * Number of filters contained, will always be 1 (required to | 626 * Number of filters contained, will always be 1 (required to | 
| 642 * optimize Matcher). | 627 * optimize Matcher). | 
| 643 * @type {number} | 628 * @type {number} | 
| 644 */ | 629 */ | 
| 645 length: 1, | 630 length: 1, | 
| 646 | 631 | 
| 647 /** | 632 /** | 
| 648 * @see ActiveFilter.domainSeparator | 633 * @see ActiveFilter.domainSeparator | 
| 649 */ | 634 */ | 
| 650 domainSeparator: "|", | 635 domainSeparator: "|", | 
| 651 | 636 | 
| 652 /** | 637 /** | 
| 653 * Expression from which a regular expression should be generated - | 638 * Expression from which a regular expression should be generated - | 
| 654 * for delayed creation of the regexp property | 639 * for delayed creation of the regexp property | 
| 655 * @type {string} | 640 * @type {string} | 
| 656 */ | 641 */ | 
| 657 regexpSource: null, | 642 regexpSource: null, | 
| 658 /** | 643 /** | 
| 659 * Regular expression to be used when testing against this filter | 644 * Regular expression to be used when testing against this filter | 
| 660 * @type {RegExp} | 645 * @type {RegExp} | 
| 661 */ | 646 */ | 
| 662 get regexp() | 647 get regexp() | 
| 663 { | 648 { | 
| 664 // Despite this property being cached, the getter is called | |
| 665 // several times on Safari, due to WebKit bug 132872 | |
| 666 let prop = Object.getOwnPropertyDescriptor(this, "regexp"); | |
| 667 if (prop) | |
| 668 return prop.value; | |
| 669 | |
| 670 let source = Filter.toRegExp(this.regexpSource); | 649 let source = Filter.toRegExp(this.regexpSource); | 
| 671 let regexp = new RegExp(source, this.matchCase ? "" : "i"); | 650 let regexp = new RegExp(source, this.matchCase ? "" : "i"); | 
| 672 Object.defineProperty(this, "regexp", {value: regexp}); | 651 Object.defineProperty(this, "regexp", {value: regexp}); | 
| 652 this.regexpSource = null; | |
| 673 return regexp; | 653 return regexp; | 
| 674 }, | 654 }, | 
| 675 /** | 655 /** | 
| 676 * Content types the filter applies to, combination of values from | 656 * Content types the filter applies to, combination of values from | 
| 677 * RegExpFilter.typeMap | 657 * RegExpFilter.typeMap | 
| 678 * @type {number} | 658 * @type {number} | 
| 679 */ | 659 */ | 
| 680 contentType: 0x7FFFFFFF, | 660 contentType: 0x7FFFFFFF, | 
| 681 /** | 661 /** | 
| 682 * Defines whether the filter should distinguish between lower and | 662 * Defines whether the filter should distinguish between lower and | 
| 683 * upper case letters | 663 * upper case letters | 
| 684 * @type {boolean} | 664 * @type {boolean} | 
| 685 */ | 665 */ | 
| 686 matchCase: false, | 666 matchCase: false, | 
| 687 /** | 667 /** | 
| 688 * Defines whether the filter should apply to third-party or | 668 * Defines whether the filter should apply to third-party or | 
| 689 * first-party content only. Can be null (apply to all content). | 669 * first-party content only. Can be null (apply to all content). | 
| 690 * @type {boolean} | 670 * @type {?boolean} | 
| 691 */ | 671 */ | 
| 692 thirdParty: null, | 672 thirdParty: null, | 
| 693 | 673 | 
| 694 /** | 674 /** | 
| 695 * String that the sitekey property should be generated from | 675 * String that the sitekey property should be generated from | 
| 696 * @type {string} | 676 * @type {?string} | 
| 697 */ | 677 */ | 
| 698 sitekeySource: null, | 678 sitekeySource: null, | 
| 699 | 679 | 
| 700 /** | 680 /** | 
| 701 * Array containing public keys of websites that this filter should apply to | 681 * @see ActiveFilter.sitekeys | 
| 702 * @type {string[]} | |
| 703 */ | 682 */ | 
| 704 get sitekeys() | 683 get sitekeys() | 
| 705 { | 684 { | 
| 706 // Despite this property being cached, the getter is called | |
| 707 // several times on Safari, due to WebKit bug 132872 | |
| 708 let prop = Object.getOwnPropertyDescriptor(this, "sitekeys"); | |
| 709 if (prop) | |
| 710 return prop.value; | |
| 711 | |
| 712 let sitekeys = null; | 685 let sitekeys = null; | 
| 713 | 686 | 
| 714 if (this.sitekeySource) | 687 if (this.sitekeySource) | 
| 715 { | 688 { | 
| 716 sitekeys = this.sitekeySource.split("|"); | 689 sitekeys = this.sitekeySource.split("|"); | 
| 717 this.sitekeySource = null; | 690 this.sitekeySource = null; | 
| 718 } | 691 } | 
| 719 | 692 | 
| 720 Object.defineProperty( | 693 Object.defineProperty( | 
| 721 this, "sitekeys", {value: sitekeys, enumerable: true} | 694 this, "sitekeys", {value: sitekeys, enumerable: true} | 
| 722 ); | 695 ); | 
| 723 return this.sitekeys; | 696 return this.sitekeys; | 
| 724 }, | 697 }, | 
| 725 | 698 | 
| 726 /** | 699 /** | 
| 727 * Tests whether the URL matches this filter | 700 * Tests whether the URL matches this filter | 
| 728 * @param {string} location URL to be tested | 701 * @param {string} location URL to be tested | 
| 729 * @param {number} typeMask bitmask of content / request types to match | 702 * @param {number} typeMask bitmask of content / request types to match | 
| 730 * @param {string} docDomain domain name of the document that loads the URL | 703 * @param {string} [docDomain] domain name of the document that loads the URL | 
| 731 * @param {boolean} thirdParty should be true if the URL is a third-party | 704 * @param {boolean} [thirdParty] should be true if the URL is a third-party | 
| 732 * request | 705 * request | 
| 733 * @param {string} sitekey public key provided by the document | 706 * @param {string} [sitekey] public key provided by the document | 
| 734 * @return {boolean} true in case of a match | 707 * @return {boolean} true in case of a match | 
| 735 */ | 708 */ | 
| 736 matches(location, typeMask, docDomain, thirdParty, sitekey) | 709 matches(location, typeMask, docDomain, thirdParty, sitekey) | 
| 737 { | 710 { | 
| 738 if (this.contentType & typeMask && | 711 if (this.contentType & typeMask && | 
| 739 (this.thirdParty == null || this.thirdParty == thirdParty) && | 712 (this.thirdParty == null || this.thirdParty == thirdParty) && | 
| 740 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) | 713 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) | 
| 741 { | 714 { | 
| 742 return true; | 715 return true; | 
| 743 } | 716 } | 
| 744 return false; | 717 return false; | 
| 745 } | 718 } | 
| 746 }); | 719 }); | 
| 747 | 720 | 
| 748 // Required to optimize Matcher, see also RegExpFilter.prototype.length | 721 // Required to optimize Matcher, see also RegExpFilter.prototype.length | 
| 749 Object.defineProperty(RegExpFilter.prototype, "0", { | 722 Object.defineProperty(RegExpFilter.prototype, "0", { | 
| 750 get() { return this; } | 723 get() { return this; } | 
| 751 }); | 724 }); | 
| 752 | 725 | 
| 753 /** | 726 /** | 
| 754 * Creates a RegExp filter from its text representation | 727 * Creates a RegExp filter from its text representation | 
| 755 * @param {string} text same as in Filter() | 728 * @param {string} text same as in Filter() | 
| 756 * @return {Filter} | 729 * @return {Filter} | 
| 757 */ | 730 */ | 
| 758 RegExpFilter.fromText = function(text) | 731 RegExpFilter.fromText = function(text) | 
| 759 { | 732 { | 
| 760 let blocking = true; | 733 let blocking = true; | 
| 761 let origText = text; | 734 let origText = text; | 
| 762 if (text.indexOf("@@") == 0) | 735 if (text[0] == "@" && text[1] == "@") | 
| 763 { | 736 { | 
| 764 blocking = false; | 737 blocking = false; | 
| 765 text = text.substr(2); | 738 text = text.substr(2); | 
| 766 } | 739 } | 
| 767 | 740 | 
| 768 let contentType = null; | 741 let contentType = null; | 
| 769 let matchCase = null; | 742 let matchCase = null; | 
| 770 let domains = null; | 743 let domains = null; | 
| 771 let sitekeys = null; | 744 let sitekeys = null; | 
| 772 let thirdParty = null; | 745 let thirdParty = null; | 
| 773 let collapse = null; | 746 let collapse = null; | 
| 774 let csp = null; | 747 let csp = null; | 
| 748 let rewrite = null; | |
| 775 let options; | 749 let options; | 
| 776 let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null); | 750 let match = text.includes("$") ? Filter.optionsRegExp.exec(text) : null; | 
| 777 if (match) | 751 if (match) | 
| 778 { | 752 { | 
| 779 options = match[1].split(","); | 753 options = match[1].split(","); | 
| 780 text = match.input.substr(0, match.index); | 754 text = match.input.substr(0, match.index); | 
| 781 for (let option of options) | 755 for (let option of options) | 
| 782 { | 756 { | 
| 783 let value = null; | 757 let value = null; | 
| 784 let separatorIndex = option.indexOf("="); | 758 let separatorIndex = option.indexOf("="); | 
| 785 if (separatorIndex >= 0) | 759 if (separatorIndex >= 0) | 
| 786 { | 760 { | 
| 787 value = option.substr(separatorIndex + 1); | 761 value = option.substr(separatorIndex + 1); | 
| 788 option = option.substr(0, separatorIndex); | 762 option = option.substr(0, separatorIndex); | 
| 789 } | 763 } | 
| 790 option = option.replace(/-/, "_").toUpperCase(); | 764 | 
| 791 if (option in RegExpFilter.typeMap) | 765 let inverse = option[0] == "~"; | 
| 766 if (inverse) | |
| 767 option = option.substr(1); | |
| 768 | |
| 769 let type = RegExpFilter.typeMap[option.replace(/-/, "_").toUpperCase()]; | |
| 770 if (type) | |
| 792 { | 771 { | 
| 793 if (contentType == null) | 772 if (inverse) | 
| 794 contentType = 0; | 773 { | 
| 795 contentType |= RegExpFilter.typeMap[option]; | 774 if (contentType == null) | 
| 796 | 775 ({contentType} = RegExpFilter.prototype); | 
| 797 if (option == "CSP" && value) | 776 contentType &= ~type; | 
| 798 csp = value; | 777 } | 
| 778 else | |
| 779 { | |
| 780 contentType |= type; | |
| 781 | |
| 782 if (type == RegExpFilter.typeMap.CSP && value) | |
| 783 csp = value; | |
| 784 } | |
| 799 } | 785 } | 
| 800 else if (option[0] == "~" && option.substr(1) in RegExpFilter.typeMap) | 786 else | 
| 801 { | 787 { | 
| 802 if (contentType == null) | 788 switch (option.toLowerCase()) | 
| 803 ({contentType} = RegExpFilter.prototype); | 789 { | 
| 804 contentType &= ~RegExpFilter.typeMap[option.substr(1)]; | 790 case "match-case": | 
| 791 matchCase = !inverse; | |
| 792 break; | |
| 793 case "domain": | |
| 794 if (!value) | |
| 795 return new InvalidFilter(origText, "filter_unknown_option"); | |
| 796 domains = value.toLowerCase(); | |
| 797 break; | |
| 798 case "third-party": | |
| 799 thirdParty = !inverse; | |
| 800 break; | |
| 801 case "collapse": | |
| 802 collapse = !inverse; | |
| 803 break; | |
| 804 case "sitekey": | |
| 805 if (!value) | |
| 806 return new InvalidFilter(origText, "filter_unknown_option"); | |
| 807 sitekeys = value.toUpperCase(); | |
| 808 break; | |
| 809 case "rewrite": | |
| 810 if (!value) | |
| 811 return new InvalidFilter(origText, "filter_unknown_option"); | |
| 812 rewrite = value; | |
| 813 break; | |
| 814 default: | |
| 815 return new InvalidFilter(origText, "filter_unknown_option"); | |
| 816 } | |
| 805 } | 817 } | 
| 806 else if (option == "MATCH_CASE") | 818 } | 
| 807 matchCase = true; | 819 } | 
| 808 else if (option == "~MATCH_CASE") | 820 | 
| 809 matchCase = false; | 821 // For security reasons, never match $rewrite filters | 
| 810 else if (option == "DOMAIN" && value) | 822 // against requests that might load any code to be executed. | 
| 811 domains = value.toUpperCase(); | 823 if (rewrite != null) | 
| 812 else if (option == "THIRD_PARTY") | 824 { | 
| 813 thirdParty = true; | 825 if (contentType == null) | 
| 814 else if (option == "~THIRD_PARTY") | 826 ({contentType} = RegExpFilter.prototype); | 
| 815 thirdParty = false; | 827 contentType &= ~(RegExpFilter.typeMap.SCRIPT | | 
| 816 else if (option == "COLLAPSE") | 828 RegExpFilter.typeMap.SUBDOCUMENT | | 
| 817 collapse = true; | 829 RegExpFilter.typeMap.OBJECT | | 
| 818 else if (option == "~COLLAPSE") | 830 RegExpFilter.typeMap.OBJECT_SUBREQUEST); | 
| 819 collapse = false; | |
| 820 else if (option == "SITEKEY" && value) | |
| 821 sitekeys = value.toUpperCase(); | |
| 822 else | |
| 823 return new InvalidFilter(origText, "filter_unknown_option"); | |
| 824 } | |
| 825 } | 831 } | 
| 826 | 832 | 
| 827 try | 833 try | 
| 828 { | 834 { | 
| 829 if (blocking) | 835 if (blocking) | 
| 830 { | 836 { | 
| 831 if (csp && Filter.invalidCSPRegExp.test(csp)) | 837 if (csp && Filter.invalidCSPRegExp.test(csp)) | 
| 832 return new InvalidFilter(origText, "filter_invalid_csp"); | 838 return new InvalidFilter(origText, "filter_invalid_csp"); | 
| 833 | 839 | 
| 834 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 840 return new BlockingFilter(origText, text, contentType, matchCase, domains, | 
| 835 thirdParty, sitekeys, collapse, csp); | 841 thirdParty, sitekeys, collapse, csp, rewrite); | 
| 836 } | 842 } | 
| 837 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 843 return new WhitelistFilter(origText, text, contentType, matchCase, domains, | 
| 838 thirdParty, sitekeys); | 844 thirdParty, sitekeys); | 
| 839 } | 845 } | 
| 840 catch (e) | 846 catch (e) | 
| 841 { | 847 { | 
| 842 return new InvalidFilter(origText, "filter_invalid_regexp"); | 848 return new InvalidFilter(origText, "filter_invalid_regexp"); | 
| 843 } | 849 } | 
| 844 }; | 850 }; | 
| 845 | 851 | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 877 // shouldn't be there by default | 883 // shouldn't be there by default | 
| 878 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | | 884 RegExpFilter.prototype.contentType &= ~(RegExpFilter.typeMap.CSP | | 
| 879 RegExpFilter.typeMap.DOCUMENT | | 885 RegExpFilter.typeMap.DOCUMENT | | 
| 880 RegExpFilter.typeMap.ELEMHIDE | | 886 RegExpFilter.typeMap.ELEMHIDE | | 
| 881 RegExpFilter.typeMap.POPUP | | 887 RegExpFilter.typeMap.POPUP | | 
| 882 RegExpFilter.typeMap.GENERICHIDE | | 888 RegExpFilter.typeMap.GENERICHIDE | | 
| 883 RegExpFilter.typeMap.GENERICBLOCK); | 889 RegExpFilter.typeMap.GENERICBLOCK); | 
| 884 | 890 | 
| 885 /** | 891 /** | 
| 886 * Class for blocking filters | 892 * Class for blocking filters | 
| 887 * @param {string} text see Filter() | 893 * @param {string} text see {@link Filter Filter()} | 
| 888 * @param {string} regexpSource see RegExpFilter() | 894 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()} | 
| 889 * @param {number} contentType see RegExpFilter() | 895 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()} | 
| 890 * @param {boolean} matchCase see RegExpFilter() | 896 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()} | 
| 891 * @param {string} domains see RegExpFilter() | 897 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()} | 
| 892 * @param {boolean} thirdParty see RegExpFilter() | 898 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()} | 
| 893 * @param {string} sitekeys see RegExpFilter() | 899 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()} | 
| 894 * @param {boolean} collapse | 900 * @param {boolean} [collapse] | 
| 895 * defines whether the filter should collapse blocked content, can be null | 901 * defines whether the filter should collapse blocked content, can be null | 
| 896 * @param {string} [csp] | 902 * @param {string} [csp] | 
| 897 * Content Security Policy to inject when the filter matches | 903 * Content Security Policy to inject when the filter matches | 
| 904 * @param {?string} [rewrite] | |
| 905 * The (optional) rule specifying how to rewrite the URL. See | |
| 906 * BlockingFilter.prototype.rewrite. | |
| 898 * @constructor | 907 * @constructor | 
| 899 * @augments RegExpFilter | 908 * @augments RegExpFilter | 
| 900 */ | 909 */ | 
| 901 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 910 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 
| 902 thirdParty, sitekeys, collapse, csp) | 911 thirdParty, sitekeys, collapse, csp, rewrite) | 
| 903 { | 912 { | 
| 904 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 913 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 
| 905 thirdParty, sitekeys); | 914 thirdParty, sitekeys); | 
| 906 | 915 | 
| 907 this.collapse = collapse; | 916 if (collapse != null) | 
| 908 this.csp = csp; | 917 this.collapse = collapse; | 
| 918 | |
| 919 if (csp != null) | |
| 920 this.csp = csp; | |
| 921 | |
| 922 if (rewrite != null) | |
| 923 this.rewrite = rewrite; | |
| 909 } | 924 } | 
| 910 exports.BlockingFilter = BlockingFilter; | 925 exports.BlockingFilter = BlockingFilter; | 
| 911 | 926 | 
| 912 BlockingFilter.prototype = extend(RegExpFilter, { | 927 BlockingFilter.prototype = extend(RegExpFilter, { | 
| 913 type: "blocking", | 928 type: "blocking", | 
| 914 | 929 | 
| 915 /** | 930 /** | 
| 916 * Defines whether the filter should collapse blocked content. | 931 * Defines whether the filter should collapse blocked content. | 
| 917 * Can be null (use the global preference). | 932 * Can be null (use the global preference). | 
| 918 * @type {boolean} | 933 * @type {?boolean} | 
| 919 */ | 934 */ | 
| 920 collapse: null, | 935 collapse: null, | 
| 921 | 936 | 
| 922 /** | 937 /** | 
| 923 * Content Security Policy to inject for matching requests. | 938 * Content Security Policy to inject for matching requests. | 
| 924 * @type {string} | 939 * @type {?string} | 
| 925 */ | 940 */ | 
| 926 csp: null | 941 csp: null, | 
| 942 | |
| 943 /** | |
| 944 * The rule specifying how to rewrite the URL. | |
| 945 * The syntax is similar to the one of String.prototype.replace(). | |
| 946 * @type {?string} | |
| 947 */ | |
| 948 rewrite: null, | |
| 949 | |
| 950 /** | |
| 951 * Rewrites an URL. | |
| 952 * @param {string} url the URL to rewrite | |
| 953 * @return {string} the rewritten URL, or the original in case of failure | |
| 954 */ | |
| 955 rewriteUrl(url) | |
| 956 { | |
| 957 try | |
| 958 { | |
| 959 let rewrittenUrl = new URL(url.replace(this.regexp, this.rewrite), url); | |
| 960 if (rewrittenUrl.origin == new URL(url).origin) | |
| 961 return rewrittenUrl.href; | |
| 962 } | |
| 963 catch (e) | |
| 964 { | |
| 965 } | |
| 966 | |
| 967 return url; | |
| 968 } | |
| 927 }); | 969 }); | 
| 928 | 970 | 
| 929 /** | 971 /** | 
| 930 * Class for whitelist filters | 972 * Class for whitelist filters | 
| 931 * @param {string} text see Filter() | 973 * @param {string} text see {@link Filter Filter()} | 
| 932 * @param {string} regexpSource see RegExpFilter() | 974 * @param {string} regexpSource see {@link RegExpFilter RegExpFilter()} | 
| 933 * @param {number} contentType see RegExpFilter() | 975 * @param {number} [contentType] see {@link RegExpFilter RegExpFilter()} | 
| 934 * @param {boolean} matchCase see RegExpFilter() | 976 * @param {boolean} [matchCase] see {@link RegExpFilter RegExpFilter()} | 
| 935 * @param {string} domains see RegExpFilter() | 977 * @param {string} [domains] see {@link RegExpFilter RegExpFilter()} | 
| 936 * @param {boolean} thirdParty see RegExpFilter() | 978 * @param {boolean} [thirdParty] see {@link RegExpFilter RegExpFilter()} | 
| 937 * @param {string} sitekeys see RegExpFilter() | 979 * @param {string} [sitekeys] see {@link RegExpFilter RegExpFilter()} | 
| 938 * @constructor | 980 * @constructor | 
| 939 * @augments RegExpFilter | 981 * @augments RegExpFilter | 
| 940 */ | 982 */ | 
| 941 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, | 983 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, | 
| 942 thirdParty, sitekeys) | 984 thirdParty, sitekeys) | 
| 943 { | 985 { | 
| 944 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 986 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 
| 945 thirdParty, sitekeys); | 987 thirdParty, sitekeys); | 
| 946 } | 988 } | 
| 947 exports.WhitelistFilter = WhitelistFilter; | 989 exports.WhitelistFilter = WhitelistFilter; | 
| 948 | 990 | 
| 949 WhitelistFilter.prototype = extend(RegExpFilter, { | 991 WhitelistFilter.prototype = extend(RegExpFilter, { | 
| 950 type: "whitelist" | 992 type: "whitelist" | 
| 951 }); | 993 }); | 
| 952 | 994 | 
| 953 /** | 995 /** | 
| 954 * Base class for code injection filters | 996 * Base class for content filters | 
| 955 * @param {string} text see Filter() | 997 * @param {string} text see {@link Filter Filter()} | 
| 956 * @param {string} [domains] Host names or domains the filter should be | 998 * @param {string} [domains] Host names or domains the filter should be | 
| 957 * restricted to | 999 * restricted to | 
| 958 * @param {string} code Code that should be injected | 1000 * @param {string} body The body of the filter | 
| 959 * @constructor | 1001 * @constructor | 
| 960 * @augments ActiveFilter | 1002 * @augments ActiveFilter | 
| 961 */ | 1003 */ | 
| 962 function CodeInjectionFilter(text, domains, code) | 1004 function ContentFilter(text, domains, body) | 
| 963 { | 1005 { | 
| 964 ActiveFilter.call(this, text, domains || null); | 1006 ActiveFilter.call(this, text, domains || null); | 
| 965 | 1007 | 
| 966 if (domains) | 1008 this.body = body; | 
| 967 { | 1009 } | 
| 968 this.injectionDomains = domains.replace(/,~[^,]+/g, "") | 1010 exports.ContentFilter = ContentFilter; | 
| 969 .replace(/^~[^,]+,?/, "").toLowerCase(); | 1011 | 
| 970 } | 1012 ContentFilter.prototype = extend(ActiveFilter, { | 
| 971 | |
| 972 this.code = code; | |
| 973 } | |
| 974 exports.CodeInjectionFilter = CodeInjectionFilter; | |
| 975 | |
| 976 CodeInjectionFilter.prototype = extend(ActiveFilter, { | |
| 977 /** | 1013 /** | 
| 978 * @see ActiveFilter.domainSeparator | 1014 * @see ActiveFilter.domainSeparator | 
| 979 */ | 1015 */ | 
| 980 domainSeparator: ",", | 1016 domainSeparator: ",", | 
| 981 | 1017 | 
| 982 /** | 1018 /** | 
| 983 * @see ActiveFilter.ignoreTrailingDot | 1019 * The body of the filter | 
| 984 */ | |
| 985 ignoreTrailingDot: false, | |
| 986 | |
| 987 /** | |
| 988 * Host names or domains the filter should be restricted to (can be null for | |
| 989 * no restriction) | |
| 990 * @type {string} | 1020 * @type {string} | 
| 991 */ | 1021 */ | 
| 992 injectionDomains: null, | 1022 body: null | 
| 993 | 1023 }); | 
| 994 /** | 1024 | 
| 995 * Code that should be injected | 1025 /** | 
| 996 * @type {string} | 1026 * Creates a content filter from a pre-parsed text representation | 
| 997 */ | |
| 998 code: null | |
| 999 }); | |
| 1000 | |
| 1001 /** | |
| 1002 * Base class for element hiding filters | |
| 1003 * @param {string} text see Filter() | |
| 1004 * @param {string} [domains] see CodeInjectionFilter() | |
| 1005 * @param {string} selector CSS selector for the HTML elements that should be | |
| 1006 * hidden | |
| 1007 * @constructor | |
| 1008 * @augments CodeInjectionFilter | |
| 1009 */ | |
| 1010 function ElemHideBase(text, domains, selector) | |
| 1011 { | |
| 1012 CodeInjectionFilter.call(this, text, domains, selector); | |
| 1013 | |
| 1014 // Braces are being escaped to prevent CSS rule injection. | |
| 1015 this.code = this.code.replace("{", "\\7B ").replace("}", "\\7D "); | |
| 1016 } | |
| 1017 exports.ElemHideBase = ElemHideBase; | |
| 1018 | |
| 1019 ElemHideBase.prototype = extend(CodeInjectionFilter, {}); | |
| 1020 | |
| 1021 /** | |
| 1022 * Creates an element hiding filter from a pre-parsed text representation | |
| 1023 * | 1027 * | 
| 1024 * @param {string} text same as in Filter() | 1028 * @param {string} text same as in Filter() | 
| 1025 * @param {string?} domains | 1029 * @param {string} [domains] | 
| 1026 * domains part of the text representation | 1030 * domains part of the text representation | 
| 1027 * @param {string?} type | 1031 * @param {string} [type] | 
| 1028 * rule type, either empty or @ (exception) or ? (emulation rule) | 1032 * rule type, either: | 
| 1029 * @param {string} selector raw CSS selector | 1033 * <li>"" for an element hiding filter</li> | 
| 
Manish Jethani
2018/07/11 13:04:26
Using just "-" doesn't work in the HTML version, b
 
kzar
2018/07/11 17:17:52
Acknowledged.
 | |
| 1034 * <li>"@" for an element hiding exception filter</li> | |
| 1035 * <li>"?" for an element hiding emulation filter</li> | |
| 1036 * <li>"$" for a snippet filter</li> | |
| 1037 * @param {string} body | |
| 1038 * body part of the text representation, either a CSS selector or a snippet | |
| 1039 * script | |
| 1030 * @return {ElemHideFilter|ElemHideException| | 1040 * @return {ElemHideFilter|ElemHideException| | 
| 1031 * ElemHideEmulationFilter|InvalidFilter} | 1041 * ElemHideEmulationFilter|SnippetFilter|InvalidFilter} | 
| 1032 */ | 1042 */ | 
| 1033 ElemHideBase.fromText = function(text, domains, type, selector) | 1043 ContentFilter.fromText = function(text, domains, type, body) | 
| 1034 { | 1044 { | 
| 1035 // We don't allow ElemHide filters which have any empty domains. | 1045 // We don't allow content filters which have any empty domains. | 
| 1036 // Note: The ElemHide.prototype.domainSeparator is duplicated here, if that | 1046 // Note: The ContentFilter.prototype.domainSeparator is duplicated here, if | 
| 1037 // changes this must be changed too. | 1047 // that changes this must be changed too. | 
| 1038 if (domains && /(^|,)~?(,|$)/.test(domains)) | 1048 if (domains && /(^|,)~?(,|$)/.test(domains)) | 
| 1039 return new InvalidFilter(text, "filter_invalid_domain"); | 1049 return new InvalidFilter(text, "filter_invalid_domain"); | 
| 1040 | 1050 | 
| 1041 if (type == "@") | 1051 if (type == "@") | 
| 1042 return new ElemHideException(text, domains, selector); | 1052 return new ElemHideException(text, domains, body); | 
| 1053 | |
| 1054 if (type == "$") | |
| 1055 return new SnippetFilter(text, domains, body); | |
| 1043 | 1056 | 
| 1044 if (type == "?") | 1057 if (type == "?") | 
| 1045 { | 1058 { | 
| 1046 // Element hiding emulation filters are inefficient so we need to make sure | 1059 // Element hiding emulation filters are inefficient so we need to make sure | 
| 1047 // that they're only applied if they specify active domains | 1060 // that they're only applied if they specify active domains | 
| 1048 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | 1061 if (!/,[^~][^,.]*\.[^,]/.test("," + domains)) | 
| 1049 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | 1062 return new InvalidFilter(text, "filter_elemhideemulation_nodomain"); | 
| 1050 | 1063 | 
| 1051 return new ElemHideEmulationFilter(text, domains, selector); | 1064 return new ElemHideEmulationFilter(text, domains, body); | 
| 1052 } | 1065 } | 
| 1053 | 1066 | 
| 1054 return new ElemHideFilter(text, domains, selector); | 1067 return new ElemHideFilter(text, domains, body); | 
| 1055 }; | 1068 }; | 
| 1056 | 1069 | 
| 1057 /** | 1070 /** | 
| 1071 * Base class for element hiding filters | |
| 1072 * @param {string} text see {@link Filter Filter()} | |
| 1073 * @param {string} [domains] see {@link ContentFilter ContentFilter()} | |
| 1074 * @param {string} selector CSS selector for the HTML elements that should be | |
| 1075 * hidden | |
| 1076 * @constructor | |
| 1077 * @augments ContentFilter | |
| 1078 */ | |
| 1079 function ElemHideBase(text, domains, selector) | |
| 1080 { | |
| 1081 ContentFilter.call(this, text, domains, selector); | |
| 1082 } | |
| 1083 exports.ElemHideBase = ElemHideBase; | |
| 1084 | |
| 1085 ElemHideBase.prototype = extend(ContentFilter, { | |
| 1086 /** | |
| 1087 * CSS selector for the HTML elements that should be hidden | |
| 1088 * @type {string} | |
| 1089 */ | |
| 1090 get selector() | |
| 1091 { | |
| 1092 // Braces are being escaped to prevent CSS rule injection. | |
| 1093 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); | |
| 1094 } | |
| 1095 }); | |
| 1096 | |
| 1097 /** | |
| 1058 * Class for element hiding filters | 1098 * Class for element hiding filters | 
| 1059 * @param {string} text see Filter() | 1099 * @param {string} text see {@link Filter Filter()} | 
| 1060 * @param {string} domains see ElemHideBase() | 1100 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} | 
| 1061 * @param {string} selector see ElemHideBase() | 1101 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 
| 1062 * @constructor | 1102 * @constructor | 
| 1063 * @augments ElemHideBase | 1103 * @augments ElemHideBase | 
| 1064 */ | 1104 */ | 
| 1065 function ElemHideFilter(text, domains, selector) | 1105 function ElemHideFilter(text, domains, selector) | 
| 1066 { | 1106 { | 
| 1067 ElemHideBase.call(this, text, domains, selector); | 1107 ElemHideBase.call(this, text, domains, selector); | 
| 1068 } | 1108 } | 
| 1069 exports.ElemHideFilter = ElemHideFilter; | 1109 exports.ElemHideFilter = ElemHideFilter; | 
| 1070 | 1110 | 
| 1071 ElemHideFilter.prototype = extend(ElemHideBase, { | 1111 ElemHideFilter.prototype = extend(ElemHideBase, { | 
| 1072 type: "elemhide" | 1112 type: "elemhide" | 
| 1073 }); | 1113 }); | 
| 1074 | 1114 | 
| 1075 /** | 1115 /** | 
| 1076 * Class for element hiding exceptions | 1116 * Class for element hiding exceptions | 
| 1077 * @param {string} text see Filter() | 1117 * @param {string} text see {@link Filter Filter()} | 
| 1078 * @param {string} domains see ElemHideBase() | 1118 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} | 
| 1079 * @param {string} selector see ElemHideBase() | 1119 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 
| 1080 * @constructor | 1120 * @constructor | 
| 1081 * @augments ElemHideBase | 1121 * @augments ElemHideBase | 
| 1082 */ | 1122 */ | 
| 1083 function ElemHideException(text, domains, selector) | 1123 function ElemHideException(text, domains, selector) | 
| 1084 { | 1124 { | 
| 1085 ElemHideBase.call(this, text, domains, selector); | 1125 ElemHideBase.call(this, text, domains, selector); | 
| 1086 } | 1126 } | 
| 1087 exports.ElemHideException = ElemHideException; | 1127 exports.ElemHideException = ElemHideException; | 
| 1088 | 1128 | 
| 1089 ElemHideException.prototype = extend(ElemHideBase, { | 1129 ElemHideException.prototype = extend(ElemHideBase, { | 
| 1090 type: "elemhideexception" | 1130 type: "elemhideexception" | 
| 1091 }); | 1131 }); | 
| 1092 | 1132 | 
| 1093 /** | 1133 /** | 
| 1094 * Class for element hiding emulation filters | 1134 * Class for element hiding emulation filters | 
| 1095 * @param {string} text see Filter() | 1135 * @param {string} text see {@link Filter Filter()} | 
| 1096 * @param {string} domains see ElemHideBase() | 1136 * @param {string} domains see {@link ElemHideBase ElemHideBase()} | 
| 1097 * @param {string} selector see ElemHideBase() | 1137 * @param {string} selector see {@link ElemHideBase ElemHideBase()} | 
| 1098 * @constructor | 1138 * @constructor | 
| 1099 * @augments ElemHideBase | 1139 * @augments ElemHideBase | 
| 1100 */ | 1140 */ | 
| 1101 function ElemHideEmulationFilter(text, domains, selector) | 1141 function ElemHideEmulationFilter(text, domains, selector) | 
| 1102 { | 1142 { | 
| 1103 ElemHideBase.call(this, text, domains, selector); | 1143 ElemHideBase.call(this, text, domains, selector); | 
| 1104 } | 1144 } | 
| 1105 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 1145 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | 
| 1106 | 1146 | 
| 1107 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1147 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 
| 1108 type: "elemhideemulation" | 1148 type: "elemhideemulation" | 
| 1109 }); | 1149 }); | 
| 1110 | 1150 | 
| 1111 /** | 1151 /** | 
| 1112 * Class for snippet filters | 1152 * Class for snippet filters | 
| 1113 * @param {string} text see Filter() | 1153 * @param {string} text see Filter() | 
| 1114 * @param {string} [domains] see CodeInjectionFilter() | 1154 * @param {string} [domains] see ContentFilter() | 
| 1115 * @param {string} script Script that should be executed | 1155 * @param {string} script Script that should be executed | 
| 1116 * @constructor | 1156 * @constructor | 
| 1117 * @augments CodeInjectionFilter | 1157 * @augments ContentFilter | 
| 1118 */ | 1158 */ | 
| 1119 function SnippetFilter(text, domains, script) | 1159 function SnippetFilter(text, domains, script) | 
| 1120 { | 1160 { | 
| 1121 CodeInjectionFilter.call(this, text, domains, script); | 1161 ContentFilter.call(this, text, domains, script); | 
| 1122 } | 1162 } | 
| 1123 exports.SnippetFilter = SnippetFilter; | 1163 exports.SnippetFilter = SnippetFilter; | 
| 1124 | 1164 | 
| 1125 SnippetFilter.prototype = extend(CodeInjectionFilter, { | 1165 SnippetFilter.prototype = extend(ContentFilter, { | 
| 1126 type: "snippet" | 1166 type: "snippet", | 
| 1127 }); | 1167 | 
| 1168 /** | |
| 1169 * Script that should be executed | |
| 1170 * @type {string} | |
| 1171 */ | |
| 1172 get script() | |
| 1173 { | |
| 1174 return this.body; | |
| 1175 } | |
| 1176 }); | |
| LEFT | RIGHT |