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 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 { | 359 { |
360 newSelector.push(selector.substring(i, pos.start)); | 360 newSelector.push(selector.substring(i, pos.start)); |
361 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); | 361 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); |
362 i = pos.end; | 362 i = pos.end; |
363 } | 363 } |
364 newSelector.push(selector.substring(i)); | 364 newSelector.push(selector.substring(i)); |
365 | 365 |
366 return newSelector.join(""); | 366 return newSelector.join(""); |
367 } | 367 } |
368 | 368 |
| 369 function closeMatch(s, t) |
| 370 { |
| 371 // This function returns an edit operation (one of "substitute", "delete", |
| 372 // and "insert") along with an index in the source string where the edit |
| 373 // should occur in order to arrive at the target string. |
| 374 |
| 375 let diff = s.length - t.length; |
| 376 |
| 377 // If the string lenghts differ by more than one character, we cannot arrive |
| 378 // at target from source in a single edit operation. |
| 379 if (diff < -1 || diff > 1) |
| 380 return null; |
| 381 |
| 382 // If target is longer than source, swap them for the purpose of our |
| 383 // calculation. |
| 384 if (diff == -1) |
| 385 { |
| 386 let tmp = s; |
| 387 s = t; |
| 388 t = tmp; |
| 389 } |
| 390 |
| 391 let edit = null; |
| 392 |
| 393 for (let i = 0, j = 0; i < s.length; i++) |
| 394 { |
| 395 if (s[i] == t[j]) |
| 396 { |
| 397 j++; |
| 398 } |
| 399 else if (edit) |
| 400 { |
| 401 // Since we want one and only one edit operation, we must bail here. |
| 402 return null; |
| 403 } |
| 404 else if ((s[i] == "." || s[i] == "+" || s[i] == "$" || s[i] == "?" || |
| 405 s[i] == "{" || s[i] == "}" || s[i] == "(" || s[i] == ")" || |
| 406 s[i] == "[" || s[i] == "]" || s[i] == "\\") || |
| 407 (t[j] == "." || t[j] == "+" || t[j] == "$" || t[j] == "?" || |
| 408 t[j] == "{" || t[j] == "}" || t[j] == "(" || t[j] == ")" || |
| 409 t[j] == "[" || t[j] == "]" || t[j] == "\\")) |
| 410 { |
| 411 // We don't deal with special characters for now. |
| 412 return null; |
| 413 } |
| 414 else |
| 415 { |
| 416 switch (diff) |
| 417 { |
| 418 case 0: |
| 419 // If both strings are equal in length, this is a substitution. |
| 420 edit = {type: "substitute", index: i}; |
| 421 j++; |
| 422 break; |
| 423 case 1: |
| 424 // If the source string is longer, this is a deletion. |
| 425 edit = {type: "delete", index: i}; |
| 426 break; |
| 427 default: |
| 428 edit = {type: "insert", index: i}; |
| 429 } |
| 430 } |
| 431 } |
| 432 |
| 433 return edit; |
| 434 } |
| 435 |
| 436 function ruleWithoutURLFilter(rule) |
| 437 { |
| 438 let copy = { |
| 439 trigger: Object.assign({}, rule.trigger), |
| 440 action: Object.assign({}, rule.action) |
| 441 }; |
| 442 |
| 443 delete copy.trigger["url-filter"]; |
| 444 |
| 445 return copy; |
| 446 } |
| 447 |
| 448 function mergeCloselyMatchingRules(rules) |
| 449 { |
| 450 let rulesInfo = new Array(rules.length); |
| 451 |
| 452 rules.forEach((rule, index) => |
| 453 { |
| 454 let skip = false; |
| 455 |
| 456 // For these rules we're getting dimishing returns. i.e. there aren't too |
| 457 // many of these that qualify anyway, they just slow us down. |
| 458 if (rule.action.type == "css-display-none" || |
| 459 rule.action.type == "ignore-previous-rules" || |
| 460 rule.trigger["url-filter-is-case-sensitive"] || |
| 461 rule.trigger["load-type"] || |
| 462 rule.trigger["if-domain"] || |
| 463 rule.trigger["unless-domain"]) |
| 464 skip = true; |
| 465 |
| 466 if (skip) |
| 467 { |
| 468 rulesInfo[index] = {skip: true}; |
| 469 } |
| 470 else |
| 471 { |
| 472 // Save a stringified version of the rule, but without the URL filter. We |
| 473 // use this for comparison later. |
| 474 rulesInfo[index] = { |
| 475 stringifiedWithoutURLFilter: JSON.stringify(ruleWithoutURLFilter(rule)) |
| 476 }; |
| 477 } |
| 478 }); |
| 479 |
| 480 for (let i = 0; i < rules.length; i++) |
| 481 { |
| 482 if (rulesInfo[i].skip) |
| 483 continue; |
| 484 |
| 485 for (let j = i + 1; j < rules.length; j++) |
| 486 { |
| 487 if (rulesInfo[j].skip) |
| 488 continue; |
| 489 |
| 490 // Check if the rules are identical except for the URL filter. |
| 491 if (rulesInfo[i].stringifiedWithoutURLFilter == |
| 492 rulesInfo[j].stringifiedWithoutURLFilter) |
| 493 { |
| 494 let source = rules[i].trigger["url-filter"]; |
| 495 let target = rules[j].trigger["url-filter"]; |
| 496 |
| 497 // Find out if the Levenshtein distance between the rules is 1. |
| 498 let edit = closeMatch(source, target); |
| 499 |
| 500 if (edit) |
| 501 { |
| 502 let urlFilter, ruleInfo, match = {edit}; |
| 503 |
| 504 if (edit.type == "insert") |
| 505 { |
| 506 // Convert the insertion into a deletion and stick it on the target |
| 507 // rule instead. We can only group deletions and substitutions; |
| 508 // therefore insertions must be treated as deletions on the target |
| 509 // rule, to be dealt with later. |
| 510 urlFilter = target; |
| 511 ruleInfo = rulesInfo[j]; |
| 512 match.index = i; |
| 513 edit.type = "delete"; |
| 514 } |
| 515 else |
| 516 { |
| 517 urlFilter = source; |
| 518 ruleInfo = rulesInfo[i]; |
| 519 match.index = j; |
| 520 } |
| 521 |
| 522 if (!ruleInfo.matches) |
| 523 ruleInfo.matches = new Array(urlFilter.length + 1); |
| 524 |
| 525 let matchesForIndex = ruleInfo.matches[edit.index]; |
| 526 |
| 527 if (matchesForIndex) |
| 528 matchesForIndex.push(match); |
| 529 else |
| 530 ruleInfo.matches[edit.index] = [match]; |
| 531 } |
| 532 } |
| 533 } |
| 534 } |
| 535 |
| 536 let mergedRules = []; |
| 537 |
| 538 rules.forEach((rule, index) => |
| 539 { |
| 540 let ruleInfo = rulesInfo[index]; |
| 541 |
| 542 if (ruleInfo.merged) |
| 543 return; |
| 544 |
| 545 // Find the best set of rules to group, which is simply the largest set. |
| 546 let best = null; |
| 547 for (let matchesForIndex of ruleInfo.matches || []) |
| 548 { |
| 549 if (matchesForIndex && (!best || matchesForIndex.length > best.length)) |
| 550 best = matchesForIndex; |
| 551 } |
| 552 |
| 553 if (best) |
| 554 { |
| 555 // Merge all the matching rules into this one. |
| 556 |
| 557 let editIndex = best[0].edit.index; |
| 558 |
| 559 let characters = []; |
| 560 let quantifier = ""; |
| 561 |
| 562 for (let match of best) |
| 563 { |
| 564 if (match.edit.type == "delete") |
| 565 quantifier = "?"; |
| 566 else |
| 567 characters.push(rules[match.index].trigger["url-filter"][editIndex]); |
| 568 |
| 569 rulesInfo[match.index].merged = true; |
| 570 } |
| 571 |
| 572 let urlFilter = rule.trigger["url-filter"]; |
| 573 |
| 574 urlFilter = urlFilter.substring(0, editIndex + 1) + quantifier + |
| 575 urlFilter.substring(editIndex + 1); |
| 576 if (characters.length > 0) |
| 577 { |
| 578 urlFilter = urlFilter.substring(0, editIndex) + "[" + |
| 579 urlFilter[editIndex] + characters.join("") + "]" + |
| 580 urlFilter.substring(editIndex + 1); |
| 581 } |
| 582 |
| 583 rule.trigger["url-filter"] = urlFilter; |
| 584 } |
| 585 |
| 586 mergedRules.push(rule); |
| 587 }); |
| 588 |
| 589 return mergedRules; |
| 590 } |
| 591 |
369 let ContentBlockerList = | 592 let ContentBlockerList = |
370 /** | 593 /** |
371 * Create a new Adblock Plus filter to content blocker list converter | 594 * Create a new Adblock Plus filter to content blocker list converter |
372 * | 595 * |
373 * @constructor | 596 * @constructor |
374 */ | 597 */ |
375 exports.ContentBlockerList = function () | 598 exports.ContentBlockerList = function () |
376 { | 599 { |
377 this.requestFilters = []; | 600 this.requestFilters = []; |
378 this.requestExceptions = []; | 601 this.requestExceptions = []; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 } | 688 } |
466 }); | 689 }); |
467 | 690 |
468 for (let filter of this.elemhideExceptions) | 691 for (let filter of this.elemhideExceptions) |
469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 692 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); |
470 for (let filter of this.requestFilters) | 693 for (let filter of this.requestFilters) |
471 convertFilterAddRules(rules, filter, "block", true); | 694 convertFilterAddRules(rules, filter, "block", true); |
472 for (let filter of this.requestExceptions) | 695 for (let filter of this.requestExceptions) |
473 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 696 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
474 | 697 |
475 return rules.filter(rule => !hasNonASCI(rule)); | 698 rules = rules.filter(rule => !hasNonASCI(rule)); |
| 699 |
| 700 rules = mergeCloselyMatchingRules(rules); |
| 701 |
| 702 return rules; |
476 }; | 703 }; |
OLD | NEW |