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

Delta Between Two Patch Sets: test/filterClasses.js

Issue 29760704: Issue 6592 - Implement $rewrite filter option (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: More review changes Created May 3, 2018, 6:39 p.m.
Right Patch Set: Just inject URL into the sandbox globals. Created May 17, 2018, 12:50 p.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
« lib/filterClasses.js ('K') | « test/_common.js ('k') | no next file » | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 } 101 }
102 else if (filter instanceof ElemHideBase) 102 else if (filter instanceof ElemHideBase)
103 { 103 {
104 if (filter instanceof ElemHideFilter) 104 if (filter instanceof ElemHideFilter)
105 result.push("type=elemhide"); 105 result.push("type=elemhide");
106 else if (filter instanceof ElemHideException) 106 else if (filter instanceof ElemHideException)
107 result.push("type=elemhideexception"); 107 result.push("type=elemhideexception");
108 else if (filter instanceof ElemHideEmulationFilter) 108 else if (filter instanceof ElemHideEmulationFilter)
109 result.push("type=elemhideemulation"); 109 result.push("type=elemhideemulation");
110 110
111 result.push("selectorDomain=" + (filter.selectorDomain || "")); 111 result.push("selectorDomains=" + (filter.selectorDomains || ""));
112 result.push("selector=" + filter.selector); 112 result.push("selector=" + filter.selector);
113 } 113 }
114 } 114 }
115 return result; 115 return result;
116 } 116 }
117 117
118 function addDefaults(expected) 118 function addDefaults(expected)
119 { 119 {
120 let type = null; 120 let type = null;
121 let hasProperty = {}; 121 let hasProperty = {};
(...skipping 30 matching lines...) Expand all
152 } 152 }
153 if (type == "filterlist") 153 if (type == "filterlist")
154 { 154 {
155 addProperty("collapse", "null"); 155 addProperty("collapse", "null");
156 addProperty("csp", "null"); 156 addProperty("csp", "null");
157 addProperty("rewrite", "null"); 157 addProperty("rewrite", "null");
158 } 158 }
159 if (type == "elemhide" || type == "elemhideexception" || 159 if (type == "elemhide" || type == "elemhideexception" ||
160 type == "elemhideemulation") 160 type == "elemhideemulation")
161 { 161 {
162 addProperty("selectorDomain", ""); 162 addProperty("selectorDomains", "");
163 addProperty("domains", ""); 163 addProperty("domains", "");
164 } 164 }
165 } 165 }
166 166
167 function compareFilter(test, text, expected, postInit) 167 function compareFilter(test, text, expected, postInit)
168 { 168 {
169 addDefaults(expected); 169 addDefaults(expected);
170 170
171 let filter = Filter.fromText(text); 171 let filter = Filter.fromText(text);
172 if (postInit) 172 if (postInit)
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp "]); 324 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp "]);
325 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp =foo,csp=ReFeRReR", "reason=filter_invalid_csp"]); 325 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp =foo,csp=ReFeRReR", "reason=filter_invalid_csp"]);
326 326
327 test.done(); 327 test.done();
328 }; 328 };
329 329
330 exports.testElementHidingRules = function(test) 330 exports.testElementHidingRules = function(test)
331 { 331 {
332 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]); 332 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]);
333 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]); 333 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
334 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "selectorDo main=foo", "selector=ddd", "domains=FOO"]); 334 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "selectorDo mains=foo", "selector=ddd", "domains=FOO"]);
335 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "se lectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 335 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "se lectorDomains=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
336 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", " selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 336 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", " selectorDomains=foo", "selector=ddd", "domains=FOO|~BAR"]);
337 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar# #ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); 337 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar# #ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
338 338
339 test.done(); 339 test.done();
340 }; 340 };
341 341
342 exports.testElementHidingExceptions = function(test) 342 exports.testElementHidingExceptions = function(test)
343 { 343 {
344 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selec tor=ddd"]); 344 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selec tor=ddd"]);
345 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]); 345 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
346 compareFilter(test, "foo#@#ddd", ["type=elemhideexception", "text=foo#@#ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO"]); 346 compareFilter(test, "foo#@#ddd", ["type=elemhideexception", "text=foo#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=FOO"]);
347 compareFilter(test, "foo,bar#@#ddd", ["type=elemhideexception", "text=foo,bar# @#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 347 compareFilter(test, "foo,bar#@#ddd", ["type=elemhideexception", "text=foo,bar# @#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
348 compareFilter(test, "foo,~bar#@#ddd", ["type=elemhideexception", "text=foo,~ba r#@#ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 348 compareFilter(test, "foo,~bar#@#ddd", ["type=elemhideexception", "text=foo,~ba r#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=FOO|~BAR"]);
349 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo ,~baz,bar#@#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BA Z"]); 349 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo ,~baz,bar#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=BAR|FOO|~B AZ"]);
350 350
351 test.done(); 351 test.done();
352 }; 352 };
353 353
354 exports.testElemHideEmulationFilters = function(test) 354 exports.testElemHideEmulationFilters = function(test)
355 { 355 {
356 // Check valid domain combinations 356 // Check valid domain combinations
357 compareFilter(test, "foo.com#?#:-abp-properties(abc)", ["type=elemhideemulatio n", "text=foo.com#?#:-abp-properties(abc)", "selectorDomain=foo.com", "selector= :-abp-properties(abc)", "domains=FOO.COM"]); 357 compareFilter(test, "foo.com#?#:-abp-properties(abc)", ["type=elemhideemulatio n", "text=foo.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector =:-abp-properties(abc)", "domains=FOO.COM"]);
358 compareFilter(test, "foo.com,~bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=foo.com,~bar.com#?#:-abp-properties(abc)", "selectorDomain=fo o.com", "selector=:-abp-properties(abc)", "domains=FOO.COM|~BAR.COM"]); 358 compareFilter(test, "foo.com,~bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=foo.com,~bar.com#?#:-abp-properties(abc)", "selectorDomains=f oo.com", "selector=:-abp-properties(abc)", "domains=FOO.COM|~BAR.COM"]);
359 compareFilter(test, "foo.com,~bar#?#:-abp-properties(abc)", ["type=elemhideemu lation", "text=foo.com,~bar#?#:-abp-properties(abc)", "selectorDomain=foo.com", "selector=:-abp-properties(abc)", "domains=FOO.COM|~BAR"]); 359 compareFilter(test, "foo.com,~bar#?#:-abp-properties(abc)", ["type=elemhideemu lation", "text=foo.com,~bar#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=FOO.COM|~BAR"]);
360 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomain=ba r.com", "selector=:-abp-properties(abc)", "domains=BAR.COM|~FOO.COM"]); 360 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomains=b ar.com", "selector=:-abp-properties(abc)", "domains=BAR.COM|~FOO.COM"]);
361 361
362 // Check some special cases 362 // Check some special cases
363 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]); 363 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]);
364 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com# ?#abc", "selectorDomain=foo.com", "selector=abc", "domains=FOO.COM"]); 364 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com# ?#abc", "selectorDomains=foo.com", "selector=abc", "domains=FOO.COM"]);
365 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "selectorDomain=foo.com", "selector=:-abp-fo obar(abc)", "domains=FOO.COM"]); 365 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "selectorDomains=foo.com", "selector=:-abp-f oobar(abc)", "domains=FOO.COM"]);
366 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomain=foo. com", "selector=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); 366 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=foo .com", "selector=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]);
367 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*) )", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag e: url(data:*))", "selectorDomain=foo.com", "selector=:-abp-properties(|backgrou nd-image: url(data:*))", "domains=FOO.COM"]); 367 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*) )", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag e: url(data:*))", "selectorDomains=foo.com", "selector=:-abp-properties(|backgro und-image: url(data:*))", "domains=FOO.COM"]);
368 368
369 // Check conversion of legacy filters 369 // Check conversion of legacy filters
370 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com#?#:-abp-properties(abc)", "selectorDomain=foo.com", "selector =:-abp-properties(abc)", "domains=FOO.COM"]); 370 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selecto r=:-abp-properties(abc)", "domains=FOO.COM"]);
371 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex t("foo.com#?#:-abp-properties(abc)")); 371 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex t("foo.com#?#:-abp-properties(abc)"));
372 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); 372 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomains=foo.com", "sele ctor=[-abp-properties='abc']", "domains=FOO.COM"]);
373 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat ion", "text=foo.com#?#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); 373 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat ion", "text=foo.com#?#[-abp-properties='abc']", "selectorDomains=foo.com", "sele ctor=[-abp-properties='abc']", "domains=FOO.COM"]);
374 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomain=foo .com", "selector=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); 374 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=fo o.com", "selector=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]);
375 375
376 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037) . 376 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037) .
377 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide" , "text=foo.com##[-abp-properties-bogus='abc']", "selectorDomain=foo.com", "sele ctor=[-abp-properties-bogus='abc']", "domains=FOO.COM"]); 377 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide" , "text=foo.com##[-abp-properties-bogus='abc']", "selectorDomains=foo.com", "sel ector=[-abp-properties-bogus='abc']", "domains=FOO.COM"]);
378 378
379 test.done(); 379 test.done();
380 }; 380 };
381 381
382 exports.testEmptyElemHideDomains = function(test) 382 exports.testEmptyElemHideDomains = function(test)
383 { 383 {
384 let emptyDomainFilters = [ 384 let emptyDomainFilters = [
385 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 385 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
386 ",foo.com##selector", "foo.com,~##selector", 386 ",foo.com##selector", "foo.com,~##selector",
387 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 387 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
388 ]; 388 ];
389 389
390 for (let filterText of emptyDomainFilters) 390 for (let filterText of emptyDomainFilters)
391 { 391 {
392 let filter = Filter.fromText(filterText); 392 let filter = Filter.fromText(filterText);
393 test.ok(filter instanceof InvalidFilter); 393 test.ok(filter instanceof InvalidFilter);
394 test.equal(filter.reason, "filter_invalid_domain"); 394 test.equal(filter.reason, "filter_invalid_domain");
395 } 395 }
396 396
397 test.done(); 397 test.done();
398 }; 398 };
399 399
400 exports.testElemHideRulesWithBraces = function(test) 400 exports.testElemHideRulesWithBraces = function(test)
401 { 401 {
402 compareFilter( 402 compareFilter(
403 test, "###foo{color: red}", [ 403 test, "###foo{color: red}", [
404 "type=elemhide", 404 "type=elemhide",
405 "text=###foo{color: red}", 405 "text=###foo{color: red}",
406 "selectorDomain=", 406 "selectorDomains=",
407 "selector=#foo\\7B color: red\\7D ", 407 "selector=#foo\\7B color: red\\7D ",
408 "domains=" 408 "domains="
409 ] 409 ]
410 ); 410 );
411 compareFilter( 411 compareFilter(
412 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [ 412 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [
413 "type=elemhideemulation", 413 "type=elemhideemulation",
414 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", 414 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
415 "selectorDomain=foo.com", 415 "selectorDomains=foo.com",
416 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", 416 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)",
417 "domains=FOO.COM" 417 "domains=FOO.COM"
418 ] 418 ]
419 ); 419 );
420 test.done(); 420 test.done();
421 }; 421 };
422 422
423 exports.testFilterNormalization = function(test) 423 exports.testFilterNormalization = function(test)
424 { 424 {
425 // Line breaks etc 425 // Line breaks etc
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 let filter = Filter.fromText(text); 513 let filter = Filter.fromText(text);
514 514
515 test.equal(filter.rewrite, "$1"); 515 test.equal(filter.rewrite, "$1");
516 // no rewrite occured: didn't match. 516 // no rewrite occured: didn't match.
517 test.equal(filter.rewriteUrl("foo"), "foo"); 517 test.equal(filter.rewriteUrl("foo"), "foo");
518 // rewrite occured: matched. 518 // rewrite occured: matched.
519 test.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"), 519 test.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"),
520 "http://content.server/file/foo.txt"); 520 "http://content.server/file/foo.txt");
521 521
522 // checking for same origin. 522 // checking for same origin.
523 let rewriteDiffOrigin = "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite= foo.com$1"; 523 let rewriteDiffOrigin =
524 "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite=foo.com$1";
524 let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin); 525 let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin);
525 526
526 // no rewrite occured because of a different origin. 527 // no rewrite occured because of a different origin.
527 test.equal( 528 test.equal(
528 filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"), 529 filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"),
529 "http://content.server/file/foo.txt?bar"); 530 "http://content.server/file/foo.txt?bar"
530 531 );
531 test.done(); 532
532 }; 533 // relative path.
534 let rewriteRelative = "/(\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1/disable";
535 let filterRelative = Filter.fromText(rewriteRelative);
536
537 test.equal(
538 filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"),
539 "http://content.server/file/foo.txt/disable"
540 );
541 test.equal(
542 filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"),
543 "http://example.com/file/foo.txt/disable"
544 );
545
546 test.done();
547 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld