Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/abp2blocklist.js

Issue 29426594: Issue 3673 - Merge closely matching rules (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Patch Set: Look for matches only within a certain range Created May 2, 2017, 4:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/abp2blocklist.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Closely matching rules are likely to be within a certain range. We only
451 // look for matches within this range. If we increase this value, it can give
452 // us more matches and a smaller resulting rule set, but possibly at a
453 // significant performance cost.
454 const heuristicRange = 100;
455
456 let rulesInfo = new Array(rules.length);
457
458 rules.forEach((rule, index) =>
459 {
460 let skip = false;
461
462 if (rule.action.type == "ignore-previous-rules")
463 skip = true;
464
465 if (skip)
466 {
467 rulesInfo[index] = {skip: true};
468 }
469 else
470 {
471 // Save a stringified version of the rule, but without the URL filter. We
472 // use this for comparison later.
473 rulesInfo[index] = {
474 stringifiedWithoutURLFilter: JSON.stringify(ruleWithoutURLFilter(rule))
475 };
476 }
477 });
478
479 for (let i = 0; i < rules.length; i++)
480 {
481 if (rulesInfo[i].skip)
482 continue;
483
484 for (let j = i + 1; j < i + heuristicRange && j < rules.length; j++)
485 {
486 if (rulesInfo[j].skip)
487 continue;
488
489 // Check if the rules are identical except for the URL filter.
490 if (rulesInfo[i].stringifiedWithoutURLFilter ==
491 rulesInfo[j].stringifiedWithoutURLFilter)
492 {
493 let source = rules[i].trigger["url-filter"];
494 let target = rules[j].trigger["url-filter"];
495
496 // Find out if the Levenshtein distance between the rules is 1.
497 let edit = closeMatch(source, target);
498
499 if (edit)
500 {
501 let urlFilter, ruleInfo, match = {edit};
502
503 if (edit.type == "insert")
504 {
505 // Convert the insertion into a deletion and stick it on the target
506 // rule instead. We can only group deletions and substitutions;
507 // therefore insertions must be treated as deletions on the target
508 // rule, to be dealt with later.
509 urlFilter = target;
510 ruleInfo = rulesInfo[j];
511 match.index = i;
512 edit.type = "delete";
513 }
514 else
515 {
516 urlFilter = source;
517 ruleInfo = rulesInfo[i];
518 match.index = j;
519 }
520
521 if (!ruleInfo.matches)
522 ruleInfo.matches = new Array(urlFilter.length + 1);
523
524 let matchesForIndex = ruleInfo.matches[edit.index];
525
526 if (matchesForIndex)
527 matchesForIndex.push(match);
528 else
529 ruleInfo.matches[edit.index] = [match];
530 }
531 }
532 }
533 }
534
535 let mergedRules = [];
536
537 rules.forEach((rule, index) =>
538 {
539 let ruleInfo = rulesInfo[index];
540
541 if (ruleInfo.merged)
542 return;
543
544 // Find the best set of rules to group, which is simply the largest set.
545 let best = null;
546 for (let matchesForIndex of ruleInfo.matches || [])
547 {
548 if (matchesForIndex && (!best || matchesForIndex.length > best.length))
549 best = matchesForIndex;
550 }
551
552 if (best)
553 {
554 // Merge all the matching rules into this one.
555
556 let editIndex = best[0].edit.index;
557
558 let characters = [];
559 let quantifier = "";
560
561 for (let match of best)
562 {
563 if (match.edit.type == "delete")
564 quantifier = "?";
565 else
566 characters.push(rules[match.index].trigger["url-filter"][editIndex]);
567
568 rulesInfo[match.index].merged = true;
569 }
570
571 let urlFilter = rule.trigger["url-filter"];
572
573 urlFilter = urlFilter.substring(0, editIndex + 1) + quantifier +
574 urlFilter.substring(editIndex + 1);
575 if (characters.length > 0)
576 {
577 urlFilter = urlFilter.substring(0, editIndex) + "[" +
578 urlFilter[editIndex] + characters.join("") + "]" +
579 urlFilter.substring(editIndex + 1);
580 }
581
582 rule.trigger["url-filter"] = urlFilter;
583 }
584
585 mergedRules.push(rule);
586 });
587
588 return mergedRules;
589 }
590
369 let ContentBlockerList = 591 let ContentBlockerList =
370 /** 592 /**
371 * Create a new Adblock Plus filter to content blocker list converter 593 * Create a new Adblock Plus filter to content blocker list converter
372 * 594 *
373 * @constructor 595 * @constructor
374 */ 596 */
375 exports.ContentBlockerList = function () 597 exports.ContentBlockerList = function ()
376 { 598 {
377 this.requestFilters = []; 599 this.requestFilters = [];
378 this.requestExceptions = []; 600 this.requestExceptions = [];
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 } 687 }
466 }); 688 });
467 689
468 for (let filter of this.elemhideExceptions) 690 for (let filter of this.elemhideExceptions)
469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); 691 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
470 for (let filter of this.requestFilters) 692 for (let filter of this.requestFilters)
471 convertFilterAddRules(rules, filter, "block", true); 693 convertFilterAddRules(rules, filter, "block", true);
472 for (let filter of this.requestExceptions) 694 for (let filter of this.requestExceptions)
473 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); 695 convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
474 696
475 return rules.filter(rule => !hasNonASCI(rule)); 697 rules = rules.filter(rule => !hasNonASCI(rule));
698
699 rules = mergeCloselyMatchingRules(rules);
700
701 return rules;
476 }; 702 };
OLDNEW
« no previous file with comments | « no previous file | test/abp2blocklist.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld