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

Unified Diff: lib/abp2blocklist.js

Issue 29426594: Issue 3673 - Merge closely matching rules (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Patch Set: Improved matching algorithm Created May 4, 2017, 2:36 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « abp2blocklist.js ('k') | test/abp2blocklist.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/abp2blocklist.js
===================================================================
--- a/lib/abp2blocklist.js
+++ b/lib/abp2blocklist.js
@@ -14,16 +14,18 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/** @module abp2blocklist */
"use strict";
+const crypto = require("crypto");
+
let filterClasses = require("filterClasses");
let tldjs = require("tldjs");
let punycode = require("punycode");
const selectorLimit = 5000;
const typeMap = filterClasses.RegExpFilter.typeMap;
const whitelistableRequestTypes = (typeMap.IMAGE
| typeMap.STYLESHEET
@@ -361,16 +363,412 @@
newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']');
i = pos.end;
}
newSelector.push(selector.substring(i));
return newSelector.join("");
}
+function closeMatch(s, t, {singleCharacterOnly = false} = {})
+{
+ // This function returns an edit operation, one of "substitute", "delete",
+ // and "insert", along with an index in the source string where the edit must
+ // occur in order to arrive at the target string. If the strings are not a
+ // close match, it returns null.
+
+ // If singleCharacterOnly is false, deletions or insertions of a contiguous
+ // range of characters from one string into the other, at the same index, are
+ // treated as a single edit. For example, "internal" and "international" are
+ // considered to be one edit apart, inserting the substring "tiona" from the
+ // latter into the former.
+
+ // A few things to note:
+ //
+ // 1) This function does not care about how the input strings are treated
+ // by the caller. It only treats them as raw strings. For example, the
+ // caller may treat them as regular expressions, where "[ab]" and "[bc]"
+ // could be considered to have an edit distance of 1, since the order
+ // within the brackets does not matter. This function will still return
+ // null for this set of inputs since they are two edits apart.
+ //
+ // 2) To be friendly to calling code that might be passing in regular
+ // expressions anyway, this function will simply return null if it
+ // encounters a special character (e.g. "\", "?", "+", "*", etc.) in the
+ // delta. For example, given "Hello" and "Hello, how are you?", it will
+ // return null instead of "{type: 'insert', index: 5, endIndex: 19}".
+ //
+ // 3) The calling code within this file does indeed pass in regular
+ // expressions (the strict subset of JavaScript regular expressions
+ // supported by WebKit for content blockers), making the important
+ // assumption that the parts where two such regular expressions may
+ // differ can always be treated as normal strings.
+ //
+ // For example, "^https?://.*/ads" and "^https?://.*/adv" differ only in
+ // the last character, therefore the regular expressions can safely be
+ // merged into "^https?://.*/ad[sv]". If, for example, the characters in
+ // the delta were to appear within square brackets originally in the
+ // input strings (e.g. "^https?://.*/ad[sx]" and "^https?://.*/ad[vx]"),
+ // the calling code would have to do extra work to merge the two regular
+ // expressions correctly. The calling code within this file assumes that
+ // this is never the case.
+
+ let diff = s.length - t.length;
+
+ // If the string lengths differ by more than one character, we cannot arrive
+ // at target from source in a single edit operation.
+ if (singleCharacterOnly && (diff < -1 || diff > 1))
+ return null;
+
+ // If target is longer than source, swap them for the purpose of our
+ // calculation.
+ if (diff < 0)
+ {
+ let tmp = s;
+ s = t;
+ t = tmp;
+ }
+
+ let edit = null;
+
+ // If the string lengths differ by only one character at most, use the simple
+ // algorithm to find a single character edit.
+ if (diff == 0 || diff == 1 || diff == -1)
+ {
+ for (let i = 0, j = 0; i < s.length; i++)
+ {
+ if (s[i] == t[j])
+ {
+ j++;
+ }
+ else if (edit)
+ {
+ // Since we want one and only one edit operation, we must bail here.
+ return null;
+ }
+ else if ((s[i] == "." || s[i] == "+" || s[i] == "$" || s[i] == "?" ||
+ s[i] == "{" || s[i] == "}" || s[i] == "(" || s[i] == ")" ||
+ s[i] == "[" || s[i] == "]" || s[i] == "\\") ||
+ (t[j] == "." || t[j] == "+" || t[j] == "$" || t[j] == "?" ||
+ t[j] == "{" || t[j] == "}" || t[j] == "(" || t[j] == ")" ||
+ t[j] == "[" || t[j] == "]" || t[j] == "\\"))
+ {
+ // We don't deal with special characters for now.
+ return null;
+ }
+ else if (diff == 0)
+ {
+ // If both strings are equal in length, this is a substitution.
+ edit = {type: "substitute", index: i};
+ j++;
+ }
+ else if (diff > 0)
+ {
+ // If the source string is longer, this is a deletion.
+ edit = {type: "delete", index: i};
+ }
+ else
+ {
+ edit = {type: "insert", index: i};
+ }
+ }
+ }
+ else if (!singleCharacterOnly)
+ {
+ // Try another algorithm to find a multiple character deletion or
+ // insertion.
+
+ let i = 0, j = 0;
+
+ for (; i < s.length; i++)
+ {
+ if (s[i] != t[i])
+ break;
+ }
+
+ for (; j < t.length; j++)
+ {
+ if (t.length - j == i ||
+ s[s.length - j - 1] != t[t.length - j - 1])
+ break;
+ }
+
+ if (i != t.length - j)
+ return null;
+
+ for (let k = i; k < s.length - j; k++)
+ {
+ // If there are any special characters in the delta, bail.
+ if (s[k] == "." || s[k] == "+" || s[k] == "$" || s[k] == "?" ||
+ s[k] == "{" || s[k] == "}" || s[k] == "(" || s[k] == ")" ||
+ s[k] == "[" || s[k] == "]" || s[k] == "\\")
+ return null;
+ }
+
+ if (diff > 0)
+ {
+ edit = {type: "delete", index: i, endIndex: s.length - j};
+ }
+ else
+ {
+ edit = {type: "insert", index: i, endIndex: s.length - j};
+ }
+ }
+
+ return edit;
+}
+
+function mergeCloselyMatchingRules(rules,
+ {advanced = false, exhaustive = false} = {})
+{
+ // Closely matching rules are likely to be within a certain range. We only
+ // look for matches within this range. If we increase this value, it can give
+ // us more matches and a smaller resulting rule set, but possibly at a
+ // significant performance cost.
+ const heuristicRange = 100;
+
+ let rulesInfo = new Array(rules.length);
+
+ rules.forEach((rule, index) =>
+ {
+ rulesInfo[index] = {rule};
+
+ if (rule.action.type == "ignore-previous-rules")
+ {
+ rulesInfo[index].skip = true;
+ }
+ else
+ {
+ // Save a hash of the rule but without the URL filter. We use this for
+ // comparison later.
+ let copy = {
+ trigger: Object.assign({}, rule.trigger),
+ action: Object.assign({}, rule.action)
+ };
+
+ delete copy.trigger["url-filter"];
+
+ rulesInfo[index].ruleHash = crypto.createHash("sha1")
+ .update(JSON.stringify(copy))
+ .digest("hex")
+ .substring(0, 8);
+ }
+ });
+
+ for (let i = 0; i < rules.length; i++)
+ {
+ if (rulesInfo[i].skip)
+ continue;
+
+ let limit = exhaustive ? rules.length :
+ Math.min(i + heuristicRange, rules.length);
+
+ for (let j = i + 1; j < limit; j++)
+ {
+ if (rulesInfo[j].skip)
+ continue;
+
+ // Check if the rules are identical except for the URL filter.
+ if (rulesInfo[i].ruleHash == rulesInfo[j].ruleHash)
+ {
+ let source = rules[i].trigger["url-filter"];
+ let target = rules[j].trigger["url-filter"];
+
+ let edit = closeMatch(source, target, {singleCharacterOnly: !advanced});
+
+ if (edit)
+ {
+ let urlFilter, ruleInfo, match = {edit};
+
+ if (edit.type == "insert")
+ {
+ // Convert the insertion into a deletion and stick it on the target
+ // rule instead. We can only group deletions and substitutions;
+ // therefore insertions must be treated as deletions on the target
+ // rule.
+ urlFilter = target;
+ ruleInfo = rulesInfo[j];
+ match.index = i;
+ edit.type = "delete";
+ }
+ else
+ {
+ urlFilter = source;
+ ruleInfo = rulesInfo[i];
+ match.index = j;
+ }
+
+ // If the edit has an end index, it represents a multiple character
+ // edit.
+ let multiEdit = !!edit.endIndex;
+
+ if (multiEdit)
+ {
+ // We only care about a single multiple character edit because the
+ // number of characters for such a match doesn't matter, we can
+ // only merge with one other rule.
+ if (!ruleInfo.multiEditMatch)
+ ruleInfo.multiEditMatch = match;
+ }
+ else
+ {
+ // For single character edits, multiple rules can be merged into
+ // one. e.g. "ad", "ads", and "adv" can be merged into "ad[sv]?".
+ if (!ruleInfo.matches)
+ ruleInfo.matches = new Array(urlFilter.length + 1);
+
+ // Matches at a particular index. For example, for a source string
+ // "ads", both target strings "ad" (deletion) and "adv"
+ // (substitution) match at index 2, hence they are grouped together
+ // to possibly be merged later into "ad[sv]?".
+ let matchesForIndex = ruleInfo.matches[edit.index];
+
+ if (matchesForIndex)
+ {
+ matchesForIndex.push(match);
+ }
+ else
+ {
+ matchesForIndex = [match];
+ ruleInfo.matches[edit.index] = matchesForIndex;
+ }
+
+ // Keep track of the best set of matches. We later sort by this to
+ // get best results.
+ if (!ruleInfo.bestMatches ||
+ matchesForIndex.length > ruleInfo.bestMatches.length)
+ ruleInfo.bestMatches = matchesForIndex;
+ }
+ }
+ }
+ }
+ }
+
+ // Filter out rules that have no matches at all.
+ let candidateRulesInfo = rulesInfo.filter(ruleInfo =>
+ {
+ return ruleInfo.bestMatches || ruleInfo.multiEditMatch
+ });
+
+ // For best results, we have to sort the candidates by the largest set of
+ // matches.
+ //
+ // For example, we want "ads", "bds", "adv", "bdv", "adx", and "bdx" to
+ // generate "ad[svx]" and "bd[svx]" (2 rules), not "[ab]ds", "[ab]dv", and
+ // "[ab]dx" (3 rules).
+ candidateRulesInfo.sort((ruleInfo1, ruleInfo2) =>
+ {
+ let weight1 = ruleInfo1.bestMatches ? ruleInfo1.bestMatches.length :
+ ruleInfo1.multiEditMatch ? 1 : 0;
+ let weight2 = ruleInfo2.bestMatches ? ruleInfo2.bestMatches.length :
+ ruleInfo2.multiEditMatch ? 1 : 0;
+
+ return weight2 - weight1;
+ });
+
+ for (let ruleInfo of candidateRulesInfo)
+ {
+ let rule = ruleInfo.rule;
+
+ // If this rule has already been merged into another rule, we skip it.
+ if (ruleInfo.merged)
+ continue;
+
+ // Find the best set of rules to group, which is simply the largest set.
+ let best = (ruleInfo.matches || []).reduce((best, matchesForIndex) =>
+ {
+ matchesForIndex = (matchesForIndex || []).filter(match =>
+ {
+ // Filter out rules that have either already been merged into other
+ // rules or have had other rules merged into them.
+ return !rulesInfo[match.index].merged &&
+ !rulesInfo[match.index].mergedInto;
+ });
+
+ return matchesForIndex.length > best.length ? matchesForIndex : best;
+ },
+ []);
+
+ let multiEdit = false;
+
+ // If we couldn't find a single rule to merge with, let's see if we have a
+ // multiple character edit. e.g. we could merge "ad" and "adserver" into
+ // "ad(server)?".
+ if (best.length == 0 && ruleInfo.multiEditMatch &&
+ !rulesInfo[ruleInfo.multiEditMatch.index].merged &&
+ !rulesInfo[ruleInfo.multiEditMatch.index].mergedInto)
+ {
+ best = [ruleInfo.multiEditMatch];
+ multiEdit = true;
+ }
+
+ if (best.length > 0)
+ {
+ let urlFilter = rule.trigger["url-filter"];
+
+ let editIndex = best[0].edit.index;
+
+ if (!multiEdit)
+ {
+ // Merge all the matching rules into this one.
+
+ let characters = [];
+ let quantifier = "";
+
+ for (let match of best)
+ {
+ if (match.edit.type == "delete")
+ {
+ quantifier = "?";
+ }
+ else
+ {
+ let character = rules[match.index].trigger["url-filter"][editIndex];
+ characters.push(character);
+ }
+
+ // Mark the target rule as merged so other rules don't try to merge
+ // it again.
+ rulesInfo[match.index].merged = true;
+ }
+
+ urlFilter = urlFilter.substring(0, editIndex + 1) + quantifier +
+ urlFilter.substring(editIndex + 1);
+ if (characters.length > 0)
+ {
+ urlFilter = urlFilter.substring(0, editIndex) + "[" +
+ urlFilter[editIndex] + characters.join("") + "]" +
+ urlFilter.substring(editIndex + 1);
+ }
+ }
+ else
+ {
+ let editEndIndex = best[0].edit.endIndex;
+
+ // Mark the target rule as merged so other rules don't try to merge it
+ // again.
+ rulesInfo[best[0].index].merged = true;
+
+ urlFilter = urlFilter.substring(0, editIndex) + "(" +
+ urlFilter.substring(editIndex, editEndIndex) + ")?" +
+ urlFilter.substring(editEndIndex);
+ }
+
+ rule.trigger["url-filter"] = urlFilter;
+
+ // Mark this rule as one that has had other rules merged into it.
+ ruleInfo.mergedInto = true;
+ }
+ }
+
+ // Filter out rules that have been merged into other rules.
+ return rulesInfo.filter(ruleInfo => !ruleInfo.merged)
+ .map(ruleInfo => ruleInfo.rule);
+}
+
let ContentBlockerList =
/**
* Create a new Adblock Plus filter to content blocker list converter
*
* @constructor
*/
exports.ContentBlockerList = function ()
{
@@ -419,17 +817,22 @@
}
};
/**
* Generate content blocker list for all filters that were added
*
* @returns {Filter} filter Filter to convert
*/
-ContentBlockerList.prototype.generateRules = function(filter)
+ContentBlockerList.prototype.generateRules = function({
+ merge = false,
+ fastMerge = true,
+ advancedMerge,
+ exhaustiveMerge
+} = {})
{
let rules = [];
let groupedElemhideFilters = new Map();
for (let filter of this.elemhideFilters)
{
let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions);
if (!result)
@@ -467,10 +870,24 @@
for (let filter of this.elemhideExceptions)
convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
for (let filter of this.requestFilters)
convertFilterAddRules(rules, filter, "block", true);
for (let filter of this.requestExceptions)
convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
- return rules.filter(rule => !hasNonASCI(rule));
+ rules = rules.filter(rule => !hasNonASCI(rule));
+
+ if (merge)
+ {
+ // If the more specific options are specified (e.g. "advanced" and
+ // "exhaustive"), they override the more general options (e.g. "fast").
+ let mergeOptions = {
+ advanced: advancedMerge || (!fastMerge && advancedMerge != false),
+ exhaustive: exhaustiveMerge || (!fastMerge && exhaustiveMerge != false)
+ };
+
+ rules = mergeCloselyMatchingRules(rules, mergeOptions);
+ }
+
+ return rules;
};
« no previous file with comments | « abp2blocklist.js ('k') | test/abp2blocklist.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld