Left: | ||
Right: |
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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 { | 343 { |
344 newSelector.push(selector.substring(i, pos.start)); | 344 newSelector.push(selector.substring(i, pos.start)); |
345 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); | 345 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); |
346 i = pos.end; | 346 i = pos.end; |
347 } | 347 } |
348 newSelector.push(selector.substring(i)); | 348 newSelector.push(selector.substring(i)); |
349 | 349 |
350 return newSelector.join(""); | 350 return newSelector.join(""); |
351 } | 351 } |
352 | 352 |
353 function addCSSRules(rules, selectors, matchDomain) | |
354 { | |
355 if (!matchDomain) | |
356 matchDomain = "^https?://"; | |
357 | |
358 while (selectors.length) | |
Manish Jethani
2017/05/17 14:54:04
The rest of this function is a cut and paste from
| |
359 { | |
360 let selector = selectors.splice(0, selectorLimit).join(", "); | |
361 | |
362 // As of Safari 9.0 element IDs are matched as lowercase. We work around | |
363 // this by converting to the attribute format [id="elementID"] | |
364 selector = convertIDSelectorsToAttributeSelectors(selector); | |
365 | |
366 rules.push({ | |
367 trigger: {"url-filter": matchDomain, | |
368 "url-filter-is-case-sensitive": true}, | |
369 action: {type: "css-display-none", | |
370 selector: selector} | |
371 }); | |
372 } | |
373 } | |
374 | |
353 let ContentBlockerList = | 375 let ContentBlockerList = |
354 /** | 376 /** |
355 * Create a new Adblock Plus filter to content blocker list converter | 377 * Create a new Adblock Plus filter to content blocker list converter |
356 * | 378 * |
357 * @constructor | 379 * @constructor |
358 */ | 380 */ |
359 exports.ContentBlockerList = function () | 381 exports.ContentBlockerList = function () |
360 { | 382 { |
361 this.requestFilters = []; | 383 this.requestFilters = []; |
362 this.requestExceptions = []; | 384 this.requestExceptions = []; |
363 this.elemhideFilters = []; | 385 this.elemhideFilters = []; |
364 this.elemhideExceptions = []; | 386 this.elemhideExceptions = []; |
387 this.generichideExceptions = []; | |
365 this.elemhideSelectorExceptions = new Map(); | 388 this.elemhideSelectorExceptions = new Map(); |
366 }; | 389 }; |
367 | 390 |
368 /** | 391 /** |
369 * Add Adblock Plus filter to be converted | 392 * Add Adblock Plus filter to be converted |
370 * | 393 * |
371 * @param {Filter} filter Filter to convert | 394 * @param {Filter} filter Filter to convert |
372 */ | 395 */ |
373 ContentBlockerList.prototype.addFilter = function(filter) | 396 ContentBlockerList.prototype.addFilter = function(filter) |
374 { | 397 { |
375 if (filter.sitekeys) | 398 if (filter.sitekeys) |
376 return; | 399 return; |
377 if (filter instanceof filterClasses.RegExpFilter && | 400 if (filter instanceof filterClasses.RegExpFilter && |
378 filter.regexpSource == null) | 401 filter.regexpSource == null) |
379 return; | 402 return; |
380 | 403 |
381 if (filter instanceof filterClasses.BlockingFilter) | 404 if (filter instanceof filterClasses.BlockingFilter) |
382 this.requestFilters.push(filter); | 405 this.requestFilters.push(filter); |
383 | 406 |
384 if (filter instanceof filterClasses.WhitelistFilter) | 407 if (filter instanceof filterClasses.WhitelistFilter) |
385 { | 408 { |
386 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes)) | 409 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes)) |
387 this.requestExceptions.push(filter); | 410 this.requestExceptions.push(filter); |
388 | 411 |
389 if (filter.contentType & typeMap.ELEMHIDE) | 412 if (filter.contentType & typeMap.ELEMHIDE) |
390 this.elemhideExceptions.push(filter); | 413 this.elemhideExceptions.push(filter); |
414 else if (filter.contentType & typeMap.GENERICHIDE) | |
415 this.generichideExceptions.push(filter); | |
391 } | 416 } |
392 | 417 |
393 if (filter instanceof filterClasses.ElemHideFilter) | 418 if (filter instanceof filterClasses.ElemHideFilter) |
394 this.elemhideFilters.push(filter); | 419 this.elemhideFilters.push(filter); |
395 | 420 |
396 if (filter instanceof filterClasses.ElemHideException) | 421 if (filter instanceof filterClasses.ElemHideException) |
397 { | 422 { |
398 let domains = this.elemhideSelectorExceptions[filter.selector]; | 423 let domains = this.elemhideSelectorExceptions[filter.selector]; |
399 if (!domains) | 424 if (!domains) |
400 domains = this.elemhideSelectorExceptions[filter.selector] = []; | 425 domains = this.elemhideSelectorExceptions[filter.selector] = []; |
401 | 426 |
402 parseDomains(filter.domains, domains, []); | 427 parseDomains(filter.domains, domains, []); |
403 } | 428 } |
404 }; | 429 }; |
405 | 430 |
406 /** | 431 /** |
407 * Generate content blocker list for all filters that were added | 432 * Generate content blocker list for all filters that were added |
408 * | 433 * |
409 * @returns {Filter} filter Filter to convert | 434 * @returns {Filter} filter Filter to convert |
410 */ | 435 */ |
411 ContentBlockerList.prototype.generateRules = function(filter) | 436 ContentBlockerList.prototype.generateRules = function(filter) |
412 { | 437 { |
413 let rules = []; | 438 let rules = []; |
414 | 439 |
440 let genericSelectors = []; | |
415 let groupedElemhideFilters = new Map(); | 441 let groupedElemhideFilters = new Map(); |
442 | |
416 for (let filter of this.elemhideFilters) | 443 for (let filter of this.elemhideFilters) |
417 { | 444 { |
418 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions); | 445 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions); |
419 if (!result) | 446 if (!result) |
420 continue; | 447 continue; |
421 | 448 |
422 if (result.matchDomains.length == 0) | 449 if (result.matchDomains.length == 0) |
423 result.matchDomains = ["^https?://"]; | |
424 | |
425 for (let matchDomain of result.matchDomains) | |
426 { | 450 { |
427 let group = groupedElemhideFilters.get(matchDomain) || []; | 451 genericSelectors.push(result.selector); |
428 group.push(result.selector); | 452 } |
429 groupedElemhideFilters.set(matchDomain, group); | 453 else |
454 { | |
455 for (let matchDomain of result.matchDomains) | |
456 { | |
457 let group = groupedElemhideFilters.get(matchDomain) || []; | |
458 group.push(result.selector); | |
459 groupedElemhideFilters.set(matchDomain, group); | |
460 } | |
430 } | 461 } |
431 } | 462 } |
432 | 463 |
464 addCSSRules(rules, genericSelectors); | |
kzar
2017/05/19 12:12:22
Just pass matchDomain of ^https?:// here, then rem
| |
465 | |
466 // Right after the generic element hiding filters, add the exceptions that | |
467 // should apply only to those filters. | |
468 for (let filter of this.generichideExceptions) | |
469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | |
470 | |
433 groupedElemhideFilters.forEach((selectors, matchDomain) => | 471 groupedElemhideFilters.forEach((selectors, matchDomain) => |
434 { | 472 { |
435 while (selectors.length) | 473 addCSSRules(rules, selectors, matchDomain); |
436 { | |
437 let selector = selectors.splice(0, selectorLimit).join(", "); | |
438 | |
439 // As of Safari 9.0 element IDs are matched as lowercase. We work around | |
440 // this by converting to the attribute format [id="elementID"] | |
441 selector = convertIDSelectorsToAttributeSelectors(selector); | |
442 | |
443 rules.push({ | |
444 trigger: {"url-filter": matchDomain, | |
445 "url-filter-is-case-sensitive": true}, | |
446 action: {type: "css-display-none", | |
447 selector: selector} | |
448 }); | |
449 } | |
450 }); | 474 }); |
451 | 475 |
452 for (let filter of this.elemhideExceptions) | 476 for (let filter of this.elemhideExceptions) |
453 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 477 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); |
454 for (let filter of this.requestFilters) | 478 for (let filter of this.requestFilters) |
455 convertFilterAddRules(rules, filter, "block", true); | 479 convertFilterAddRules(rules, filter, "block", true); |
456 for (let filter of this.requestExceptions) | 480 for (let filter of this.requestExceptions) |
457 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 481 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
458 | 482 |
459 return rules.filter(rule => !hasNonASCI(rule)); | 483 return rules.filter(rule => !hasNonASCI(rule)); |
460 }; | 484 }; |
OLD | NEW |