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

Delta Between Two Patch Sets: lib/abp2blocklist.js

Issue 29452289: Issue 5283 - Add support for $websocket and $webrtc (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Left Patch Set: Generate additional rules if filter contains URL scheme Created June 2, 2017, 7:43 a.m.
Right Patch Set: Rebase Created July 13, 2017, 11:41 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | node_modules/filterClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); 61 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
62 } 62 }
63 63
64 function matchDomain(domain) 64 function matchDomain(domain)
65 { 65 {
66 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]"; 66 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]";
67 } 67 }
68 68
69 function getURLSchemes(contentType) 69 function getURLSchemes(contentType)
70 { 70 {
71 if (contentType == typeMap.WEBSOCKET) 71 // If the given content type includes all supported URL schemes, simply
72 return ["wss?://"]; 72 // return a single generic URL scheme pattern. This minimizes the size of the
73 73 // generated rule set. The downside to this is that it will also match
74 if (contentType == typeMap.WEBRTC) 74 // schemes that we do not want to match (e.g. "ftp://"), but this can be
75 return ["stuns?:", "turns?:"]; 75 // mitigated by adding exceptions for those schemes.
76 76 if (contentType & typeMap.WEBSOCKET && contentType & typeMap.WEBRTC &&
77 return ["https?://"]; 77 contentType & ~(typeMap.WEBSOCKET | typeMap.WEBRTC))
78 return ["[^:]+:(//)?"];
79
80 let urlSchemes = [];
81
82 if (contentType & typeMap.WEBSOCKET)
83 urlSchemes.push("wss?://");
84
85 if (contentType & typeMap.WEBRTC)
86 urlSchemes.push("stuns?:", "turns?:");
87
88 if (contentType & ~(typeMap.WEBSOCKET | typeMap.WEBRTC))
89 urlSchemes.push("https?://");
90
91 return urlSchemes;
78 } 92 }
79 93
80 function findSubdomainsInList(domain, list) 94 function findSubdomainsInList(domain, list)
81 { 95 {
82 let subdomains = []; 96 let subdomains = [];
83 let suffixLength = domain.length + 1; 97 let suffixLength = domain.length + 1;
84 98
85 for (let name of list) 99 for (let name of list)
86 { 100 {
87 if (name.length > suffixLength && name.slice(-suffixLength) == "." + domain) 101 if (name.length > suffixLength && name.slice(-suffixLength) == "." + domain)
88 subdomains.push(name.slice(0, -suffixLength)); 102 subdomains.push(name.slice(0, -suffixLength));
89 } 103 }
90 104
91 return subdomains; 105 return subdomains;
106 }
107
108 function extractFilterDomains(filters)
109 {
110 let domains = new Set();
111 for (let filter of filters)
112 {
113 let parsed = parseFilterRegexpSource(filter.regexpSource);
114 if (parsed.justHostname)
115 domains.add(parsed.hostname);
116 }
117 return domains;
92 } 118 }
93 119
94 function convertElemHideFilter(filter, elemhideSelectorExceptions) 120 function convertElemHideFilter(filter, elemhideSelectorExceptions)
95 { 121 {
96 let included = []; 122 let included = [];
97 let excluded = []; 123 let excluded = [];
98 let rules = []; 124 let rules = [];
99 125
100 parseDomains(filter.domains, included, excluded); 126 parseDomains(filter.domains, included, excluded);
101 127
(...skipping 14 matching lines...) Expand all
116 * case, a hostname string (or undefined) and a bool 142 * case, a hostname string (or undefined) and a bool
117 * indicating if the source only contains a hostname or not: 143 * indicating if the source only contains a hostname or not:
118 * {regexp: "...", 144 * {regexp: "...",
119 * canSafelyMatchAsLowercase: true/false, 145 * canSafelyMatchAsLowercase: true/false,
120 * hostname: "...", 146 * hostname: "...",
121 * justHostname: true/false} 147 * justHostname: true/false}
122 */ 148 */
123 function parseFilterRegexpSource(text, urlScheme) 149 function parseFilterRegexpSource(text, urlScheme)
124 { 150 {
125 let regexp = []; 151 let regexp = [];
126 let lastIndex = text.length - 1; 152
153 // Convert the text into an array of Unicode characters.
154 //
155 // In the case of surrogate pairs (the smiley emoji, for example), one
156 // Unicode code point is represented by two JavaScript characters together.
157 // We want to iterate over Unicode code points rather than JavaScript
158 // characters.
159 let characters = Array.from(text);
160
161 let lastIndex = characters.length - 1;
127 let hostname; 162 let hostname;
128 let hostnameStart = null; 163 let hostnameStart = null;
129 let hostnameFinished = false; 164 let hostnameFinished = false;
130 let justHostname = false; 165 let justHostname = false;
131 let canSafelyMatchAsLowercase = false; 166 let canSafelyMatchAsLowercase = false;
132 167
133 if (!urlScheme) 168 if (!urlScheme)
134 urlScheme = getURLSchemes()[0]; 169 urlScheme = getURLSchemes()[0];
135 170
136 for (let i = 0; i < text.length; i++) 171 for (let i = 0; i < characters.length; i++)
137 { 172 {
138 let c = text[i]; 173 let c = characters[i];
139 174
140 if (hostnameFinished) 175 if (hostnameFinished)
141 justHostname = false; 176 justHostname = false;
142 177
143 // If we're currently inside the hostname we have to be careful not to 178 // If we're currently inside the hostname we have to be careful not to
144 // escape any characters until after we have converted it to punycode. 179 // escape any characters until after we have converted it to punycode.
145 if (hostnameStart != null && !hostnameFinished) 180 if (hostnameStart != null && !hostnameFinished)
146 { 181 {
147 let endingChar = (c == "*" || c == "^" || 182 let endingChar = (c == "*" || c == "^" ||
148 c == "?" || c == "/" || c == "|"); 183 c == "?" || c == "/" || c == "|");
149 if (!endingChar && i != lastIndex) 184 if (!endingChar && i != lastIndex)
150 continue; 185 continue;
151 186
152 hostname = punycode.toASCII( 187 hostname = punycode.toASCII(
153 text.substring(hostnameStart, endingChar ? i : i + 1) 188 characters.slice(hostnameStart, endingChar ? i : i + 1).join("")
189 .toLowerCase()
154 ); 190 );
155 hostnameFinished = justHostname = true; 191 hostnameFinished = justHostname = true;
156 regexp.push(escapeRegExp(hostname)); 192 regexp.push(escapeRegExp(hostname));
157 if (!endingChar) 193 if (!endingChar)
158 break; 194 break;
159 } 195 }
160 196
161 switch (c) 197 switch (c)
162 { 198 {
163 case "*": 199 case "*":
164 if (regexp.length > 0 && i < lastIndex && text[i + 1] != "*") 200 if (regexp.length > 0 && i < lastIndex && characters[i + 1] != "*")
165 regexp.push(".*"); 201 regexp.push(".*");
166 break; 202 break;
167 case "^": 203 case "^":
168 if (i < lastIndex) 204 let alphabet = "a-z";
169 regexp.push("."); 205 // If justHostname is true and we've encountered a "^", it means we're
206 // still in the hostname part of the URL. Since hostnames are always
207 // lower case (Punycode), there's no need to include "A-Z" in the
208 // pattern. Further, subsequent code may lower-case the entire regular
209 // expression (if the URL contains only the hostname part), leaving us
210 // with "a-za-z", which would be redundant.
211 if (!justHostname)
212 alphabet = "A-Z" + alphabet;
213 let digits = "0-9";
214 // Note that the "-" must appear first here in order to retain its
215 // literal meaning within the brackets.
216 let specialCharacters = "-_.%";
217 let separator = "[^" + specialCharacters + alphabet + digits + "]";
218 if (i == 0)
219 regexp.push("^" + urlScheme + "(.*" + separator + ")?");
220 else if (i == lastIndex)
221 regexp.push("(" + separator + ".*)?$");
222 else
223 regexp.push(separator);
170 break; 224 break;
171 case "|": 225 case "|":
172 if (i == 0) 226 if (i == 0)
173 { 227 {
174 regexp.push("^"); 228 regexp.push("^");
175 break; 229 break;
176 } 230 }
177 if (i == lastIndex) 231 if (i == lastIndex)
178 { 232 {
179 regexp.push("$"); 233 regexp.push("$");
180 break; 234 break;
181 } 235 }
182 if (i == 1 && text[0] == "|") 236 if (i == 1 && characters[0] == "|")
183 { 237 {
184 hostnameStart = i + 1; 238 hostnameStart = i + 1;
185 canSafelyMatchAsLowercase = true; 239 canSafelyMatchAsLowercase = true;
186 regexp.push(urlScheme + "([^/]+\\.)?"); 240 regexp.push(urlScheme + "([^/]+\\.)?");
187 break; 241 break;
188 } 242 }
189 regexp.push("\\|"); 243 regexp.push("\\|");
190 break; 244 break;
191 case "/": 245 case "/":
192 if (!hostnameFinished && 246 if (!hostnameFinished &&
193 text.charAt(i-2) == ":" && text.charAt(i-1) == "/") 247 characters[i - 2] == ":" && characters[i - 1] == "/")
194 { 248 {
195 hostnameStart = i + 1; 249 hostnameStart = i + 1;
196 canSafelyMatchAsLowercase = true; 250 canSafelyMatchAsLowercase = true;
197 } 251 }
198 regexp.push("/"); 252 regexp.push("/");
199 break; 253 break;
200 case ".": case "+": case "$": case "?": 254 case ".": case "+": case "$": case "?":
201 case "{": case "}": case "(": case ")": 255 case "{": case "}": case "(": case ")":
202 case "[": case "]": case "\\": 256 case "[": case "]": case "\\":
203 regexp.push("\\", c); 257 regexp.push("\\", c);
204 break; 258 break;
205 default: 259 default:
206 if (hostnameFinished && (c >= "a" && c <= "z" || 260 if (hostnameFinished && (c >= "a" && c <= "z" ||
207 c >= "A" && c <= "Z")) 261 c >= "A" && c <= "Z"))
208 canSafelyMatchAsLowercase = false; 262 canSafelyMatchAsLowercase = false;
209 regexp.push(c); 263 regexp.push(c == "%" ? c : encodeURI(c));
210 } 264 }
211 } 265 }
212 266
213 return { 267 return {
214 regexp: regexp.join(""), 268 regexp: regexp.join(""),
215 canSafelyMatchAsLowercase: canSafelyMatchAsLowercase, 269 canSafelyMatchAsLowercase: canSafelyMatchAsLowercase,
216 hostname: hostname, 270 hostname: hostname,
217 justHostname: justHostname 271 justHostname: justHostname
218 }; 272 };
219 } 273 }
220 274
221 function getResourceTypes(contentType) 275 function getResourceTypes(contentType)
222 { 276 {
223 let types = []; 277 let types = [];
224 278
225 if (contentType & typeMap.IMAGE) 279 if (contentType & typeMap.IMAGE)
226 types.push("image"); 280 types.push("image");
227 if (contentType & typeMap.STYLESHEET) 281 if (contentType & typeMap.STYLESHEET)
228 types.push("style-sheet"); 282 types.push("style-sheet");
229 if (contentType & typeMap.SCRIPT) 283 if (contentType & typeMap.SCRIPT)
230 types.push("script"); 284 types.push("script");
231 if (contentType & typeMap.FONT) 285 if (contentType & typeMap.FONT)
232 types.push("font"); 286 types.push("font");
233 if (contentType & (typeMap.MEDIA | typeMap.OBJECT)) 287 if (contentType & (typeMap.MEDIA | typeMap.OBJECT))
234 types.push("media"); 288 types.push("media");
235 if (contentType & typeMap.POPUP) 289 if (contentType & typeMap.POPUP)
236 types.push("popup"); 290 types.push("popup");
237 if (contentType & (typeMap.XMLHTTPREQUEST | 291 if (contentType & (typeMap.XMLHTTPREQUEST |
238 typeMap.WEBSOCKET | 292 typeMap.WEBSOCKET |
239 typeMap.WEBRTC | 293 typeMap.WEBRTC |
240 typeMap.OBJECT_SUBREQUEST | 294 typeMap.OBJECT_SUBREQUEST |
241 typeMap.PING | 295 typeMap.PING |
242 typeMap.OTHER)) 296 typeMap.OTHER))
243 { 297 {
244 types.push("raw"); 298 types.push("raw");
245 } 299 }
246 if (contentType & typeMap.SUBDOCUMENT) 300 if (contentType & typeMap.SUBDOCUMENT)
247 types.push("document"); 301 types.push("document");
248 302
249 return types; 303 return types;
250 } 304 }
251 305
252 function makeRuleCopies(trigger, action, urlSchemes) 306 function makeRuleCopies(trigger, action, urlSchemes)
(...skipping 23 matching lines...) Expand all
276 { 330 {
277 let copyTrigger = Object.assign(JSON.parse(stringifiedTrigger), { 331 let copyTrigger = Object.assign(JSON.parse(stringifiedTrigger), {
278 "url-filter": "^" + urlSchemes[i] + filterPattern 332 "url-filter": "^" + urlSchemes[i] + filterPattern
279 }); 333 });
280 copies.push({trigger: copyTrigger, action}); 334 copies.push({trigger: copyTrigger, action});
281 } 335 }
282 336
283 return copies; 337 return copies;
284 } 338 }
285 339
340 function excludeTopURLFromTrigger(trigger)
341 {
342 trigger["unless-top-url"] = [trigger["url-filter"]];
343 if (trigger["url-filter-is-case-sensitive"])
344 trigger["top-url-filter-is-case-sensitive"] = true;
345 }
346
286 function convertFilterAddRules(rules, filter, action, withResourceTypes, 347 function convertFilterAddRules(rules, filter, action, withResourceTypes,
287 exceptionDomains) 348 exceptionDomains, contentType)
288 { 349 {
289 let contentType = filter.contentType; 350 if (!contentType)
290 351 contentType = filter.contentType;
291 // Support WebSocket and WebRTC only if they're the only option. If we try to 352
292 // support them otherwise (e.g. $xmlhttprequest,websocket,webrtc), we end up 353 // If WebSocket or WebRTC are given along with other options but not
293 // having to generate multiple rules, which bloats the rule set and is not 354 // including all three of WebSocket, WebRTC, and XMLHttpRequest, we must
294 // really necessary in practice. 355 // generate multiple rules. For example, for the filter
295 if ((contentType & typeMap.WEBSOCKET && contentType != typeMap.WEBSOCKET) || 356 // "foo$websocket,image", we must generate one rule with "^wss?://" and "raw"
296 (contentType & typeMap.WEBRTC && contentType != typeMap.WEBRTC)) 357 // and another rule with "^https?://" and "image". If we merge the two, we
297 { 358 // end up blocking requests of type XMLHttpRequest inadvertently.
359 if ((contentType & typeMap.WEBSOCKET && contentType != typeMap.WEBSOCKET &&
360 !(contentType & typeMap.WEBRTC &&
361 contentType & typeMap.XMLHTTPREQUEST)) ||
362 (contentType & typeMap.WEBRTC && contentType != typeMap.WEBRTC &&
363 !(contentType & typeMap.WEBSOCKET &&
364 contentType & typeMap.XMLHTTPREQUEST)))
365 {
366 if (contentType & typeMap.WEBSOCKET)
367 {
368 convertFilterAddRules(rules, filter, action, withResourceTypes,
369 exceptionDomains, typeMap.WEBSOCKET);
370 }
371
372 if (contentType & typeMap.WEBRTC)
373 {
374 convertFilterAddRules(rules, filter, action, withResourceTypes,
375 exceptionDomains, typeMap.WEBRTC);
376 }
377
298 contentType &= ~(typeMap.WEBSOCKET | typeMap.WEBRTC); 378 contentType &= ~(typeMap.WEBSOCKET | typeMap.WEBRTC);
379
380 if (!contentType)
381 return;
299 } 382 }
300 383
301 let urlSchemes = getURLSchemes(contentType); 384 let urlSchemes = getURLSchemes(contentType);
302 let parsed = parseFilterRegexpSource(filter.regexpSource, urlSchemes[0]); 385 let parsed = parseFilterRegexpSource(filter.regexpSource, urlSchemes[0]);
303 386
304 // For the special case of $document whitelisting filters with just a domain 387 // For the special case of $document whitelisting filters with just a domain
305 // we can generate an equivalent blocking rule exception using if-domain. 388 // we can generate an equivalent blocking rule exception using if-domain.
306 if (filter instanceof filterClasses.WhitelistFilter && 389 if (filter instanceof filterClasses.WhitelistFilter &&
307 contentType & typeMap.DOCUMENT && 390 contentType & typeMap.DOCUMENT &&
308 parsed.justHostname) 391 parsed.justHostname)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 let included = []; 445 let included = [];
363 let excluded = []; 446 let excluded = [];
364 447
365 parseDomains(filter.domains, included, excluded); 448 parseDomains(filter.domains, included, excluded);
366 449
367 if (exceptionDomains) 450 if (exceptionDomains)
368 excluded = excluded.concat(exceptionDomains); 451 excluded = excluded.concat(exceptionDomains);
369 452
370 if (withResourceTypes) 453 if (withResourceTypes)
371 { 454 {
372 trigger["resource-type"] = getResourceTypes(contentType); 455 let resourceTypes = getResourceTypes(contentType);
373 456
374 if (trigger["resource-type"].length == 0) 457 // Content blocker rules can't differentiate between sub-document requests
458 // (iframes) and top-level document requests. To avoid too many false
459 // positives, we prevent rules with no hostname part from blocking document
460 // requests.
461 //
462 // Once Safari 11 becomes our minimum supported version, we could change
463 // our approach here to use the new "unless-top-url" property instead.
464 if (filter instanceof filterClasses.BlockingFilter && !parsed.hostname)
465 resourceTypes = resourceTypes.filter(type => type != "document");
466
467 if (resourceTypes.length == 0)
375 return; 468 return;
469
470 trigger["resource-type"] = resourceTypes;
376 } 471 }
377 472
378 if (filter.thirdParty != null) 473 if (filter.thirdParty != null)
379 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; 474 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"];
475
476 let addTopLevelException = false;
380 477
381 if (included.length > 0) 478 if (included.length > 0)
382 { 479 {
383 trigger["if-domain"] = []; 480 trigger["if-domain"] = [];
384 481
385 for (let name of included) 482 for (let name of included)
386 { 483 {
387 // If this is a blocking filter or an element hiding filter, add the 484 // If this is a blocking filter or an element hiding filter, add the
388 // subdomain wildcard only if no subdomains have been excluded. 485 // subdomain wildcard only if no subdomains have been excluded.
389 let notSubdomains = null; 486 let notSubdomains = null;
(...skipping 10 matching lines...) Expand all
400 else 497 else
401 { 498 {
402 trigger["if-domain"].push("*" + name); 499 trigger["if-domain"].push("*" + name);
403 } 500 }
404 } 501 }
405 } 502 }
406 else if (excluded.length > 0) 503 else if (excluded.length > 0)
407 { 504 {
408 trigger["unless-domain"] = excluded.map(name => "*" + name); 505 trigger["unless-domain"] = excluded.map(name => "*" + name);
409 } 506 }
507 else if (filter instanceof filterClasses.BlockingFilter &&
508 filter.contentType & typeMap.SUBDOCUMENT && parsed.hostname)
509 {
510 // Rules with a hostname part are still allowed to block document requests,
511 // but we add an exception for top-level documents.
512 //
513 // Note that we can only do this if there's no "unless-domain" property for
514 // now. This also only works in Safari 11 onwards, while older versions
515 // simply ignore this property. Once Safari 11 becomes our minimum
516 // supported version, we can merge "unless-domain" into "unless-top-url".
517 addTopLevelException = true;
518 excludeTopURLFromTrigger(trigger);
519 }
410 520
411 rules.push({trigger: trigger, action: {type: action}}); 521 rules.push({trigger: trigger, action: {type: action}});
412 522
413 if (needAltRules) 523 if (needAltRules)
414 { 524 {
415 // Generate additional rules for any alternative URL schemes. 525 // Generate additional rules for any alternative URL schemes.
416 for (let altRule of makeRuleCopies(trigger, {type: action}, urlSchemes)) 526 for (let altRule of makeRuleCopies(trigger, {type: action}, urlSchemes))
527 {
528 if (addTopLevelException)
529 excludeTopURLFromTrigger(altRule.trigger);
530
417 rules.push(altRule); 531 rules.push(altRule);
418 } 532 }
419 } 533 }
420
421 function hasNonASCI(obj)
422 {
423 if (typeof obj == "string")
424 {
425 if (/[^\x00-\x7F]/.test(obj))
426 return true;
427 }
428
429 if (typeof obj == "object")
430 {
431 if (obj instanceof Array)
432 for (let item of obj)
433 if (hasNonASCI(item))
434 return true;
435
436 let names = Object.getOwnPropertyNames(obj);
437 for (let name of names)
438 if (hasNonASCI(obj[name]))
439 return true;
440 }
441
442 return false;
443 } 534 }
444 535
445 function convertIDSelectorsToAttributeSelectors(selector) 536 function convertIDSelectorsToAttributeSelectors(selector)
446 { 537 {
447 // First we figure out where all the IDs are 538 // First we figure out where all the IDs are
448 let sep = ""; 539 let sep = "";
449 let start = null; 540 let start = null;
450 let positions = []; 541 let positions = [];
451 for (let i = 0; i < selector.length; i++) 542 for (let i = 0; i < selector.length; i++)
452 { 543 {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 { 577 {
487 newSelector.push(selector.substring(i, pos.start)); 578 newSelector.push(selector.substring(i, pos.start));
488 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); 579 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']');
489 i = pos.end; 580 i = pos.end;
490 } 581 }
491 newSelector.push(selector.substring(i)); 582 newSelector.push(selector.substring(i));
492 583
493 return newSelector.join(""); 584 return newSelector.join("");
494 } 585 }
495 586
496 function addCSSRules(rules, selectors, matchDomain) 587 function addCSSRules(rules, selectors, matchDomain, exceptionDomains)
497 { 588 {
589 let unlessDomain = exceptionDomains.size > 0 ? [] : null;
590
591 exceptionDomains.forEach(name => unlessDomain.push("*" + name));
592
498 while (selectors.length) 593 while (selectors.length)
499 { 594 {
500 let selector = selectors.splice(0, selectorLimit).join(", "); 595 let selector = selectors.splice(0, selectorLimit).join(", ");
501 596
502 // As of Safari 9.0 element IDs are matched as lowercase. We work around 597 // As of Safari 9.0 element IDs are matched as lowercase. We work around
503 // this by converting to the attribute format [id="elementID"] 598 // this by converting to the attribute format [id="elementID"]
504 selector = convertIDSelectorsToAttributeSelectors(selector); 599 selector = convertIDSelectorsToAttributeSelectors(selector);
505 600
506 rules.push({ 601 let rule = {
507 trigger: {"url-filter": matchDomain, 602 trigger: {"url-filter": matchDomain,
508 "url-filter-is-case-sensitive": true}, 603 "url-filter-is-case-sensitive": true},
509 action: {type: "css-display-none", 604 action: {type: "css-display-none",
510 selector: selector} 605 selector: selector}
511 }); 606 };
607
608 if (unlessDomain)
609 rule.trigger["unless-domain"] = unlessDomain;
610
611 rules.push(rule);
512 } 612 }
513 } 613 }
514 614
515 let ContentBlockerList = 615 let ContentBlockerList =
516 /** 616 /**
517 * Create a new Adblock Plus filter to content blocker list converter 617 * Create a new Adblock Plus filter to content blocker list converter
518 * 618 *
519 * @constructor 619 * @constructor
520 */ 620 */
521 exports.ContentBlockerList = function () 621 exports.ContentBlockerList = function ()
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 { 698 {
599 for (let matchDomain of result.matchDomains) 699 for (let matchDomain of result.matchDomains)
600 { 700 {
601 let group = groupedElemhideFilters.get(matchDomain) || []; 701 let group = groupedElemhideFilters.get(matchDomain) || [];
602 group.push(result.selector); 702 group.push(result.selector);
603 groupedElemhideFilters.set(matchDomain, group); 703 groupedElemhideFilters.set(matchDomain, group);
604 } 704 }
605 } 705 }
606 } 706 }
607 707
608 addCSSRules(rules, genericSelectors, "^https?://"); 708 // Separate out the element hiding exceptions that have only a hostname part
609 709 // from the rest. This allows us to implement a workaround for issue #5345
610 // Right after the generic element hiding filters, add the exceptions that 710 // (WebKit bug #167423), but as a bonus it also reduces the number of
611 // should apply only to those filters. 711 // generated rules. The downside is that the exception will only apply to the
612 for (let filter of this.generichideExceptions) 712 // top-level document, not to iframes. We have to live with this until the
613 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); 713 // WebKit bug is fixed in all supported versions of Safari.
714 // https://bugs.webkit.org/show_bug.cgi?id=167423
715 //
716 // Note that as a result of this workaround we end up with a huge rule set in
717 // terms of the amount of memory used. This can cause Node.js to throw
718 // "JavaScript heap out of memory". To avoid this, call Node.js with
719 // --max_old_space_size=4096
720 let elemhideExceptionDomains = extractFilterDomains(this.elemhideExceptions);
721
722 let genericSelectorExceptionDomains =
723 extractFilterDomains(this.generichideExceptions);
724 elemhideExceptionDomains.forEach(name =>
725 {
726 genericSelectorExceptionDomains.add(name);
727 });
728
729 addCSSRules(rules, genericSelectors, "^https?://",
730 genericSelectorExceptionDomains);
614 731
615 groupedElemhideFilters.forEach((selectors, matchDomain) => 732 groupedElemhideFilters.forEach((selectors, matchDomain) =>
616 { 733 {
617 addCSSRules(rules, selectors, matchDomain); 734 addCSSRules(rules, selectors, matchDomain, elemhideExceptionDomains);
618 }); 735 });
619
620 for (let filter of this.elemhideExceptions)
621 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
622 736
623 let requestFilterExceptionDomains = []; 737 let requestFilterExceptionDomains = [];
624 for (let filter of this.genericblockExceptions) 738 for (let filter of this.genericblockExceptions)
625 { 739 {
626 let parsed = parseFilterRegexpSource(filter.regexpSource); 740 let parsed = parseFilterRegexpSource(filter.regexpSource);
627 if (parsed.hostname) 741 if (parsed.hostname)
628 requestFilterExceptionDomains.push(parsed.hostname); 742 requestFilterExceptionDomains.push(parsed.hostname);
629 } 743 }
630 744
631 for (let filter of this.requestFilters) 745 for (let filter of this.requestFilters)
632 { 746 {
633 convertFilterAddRules(rules, filter, "block", true, 747 convertFilterAddRules(rules, filter, "block", true,
634 requestFilterExceptionDomains); 748 requestFilterExceptionDomains);
635 } 749 }
636 750
637 for (let filter of this.requestExceptions) 751 for (let filter of this.requestExceptions)
638 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); 752 convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
639 753
640 return rules.filter(rule => !hasNonASCI(rule)); 754 return rules;
641 }; 755 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld