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

Side by Side Diff: test/elemHide.js

Issue 29886555: Issue 6957 - Add generateStyleSheetForDomain function to ElemHide module (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Sept. 20, 2018, 12:22 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
« lib/elemHide.js ('K') | « lib/elemHide.js ('k') | no next file » | 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-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 29 matching lines...) Expand all
40 {Filter} = sandboxedRequire("../lib/filterClasses") 40 {Filter} = sandboxedRequire("../lib/filterClasses")
41 ); 41 );
42 42
43 callback(); 43 callback();
44 }; 44 };
45 45
46 function normalizeSelectors(selectors) 46 function normalizeSelectors(selectors)
47 { 47 {
48 // getSelectorsForDomain is currently allowed to return duplicate selectors 48 // getSelectorsForDomain is currently allowed to return duplicate selectors
49 // for performance reasons, so we need to remove duplicates here. 49 // for performance reasons, so we need to remove duplicates here.
50 return selectors.sort().filter((selector, index, sortedSelectors) => 50 return selectors.slice().sort().filter((selector, index, sortedSelectors) =>
Manish Jethani 2018/09/20 12:27:24 Somewhat unrelated, but we should make a copy and
Jon Sonesen 2018/09/20 20:10:20 Acknowledged. Just curious, since slice returns a
Manish Jethani 2018/09/21 10:53:05 The selectors themselves are immutable string obje
Jon Sonesen 2018/09/23 18:01:59 Yeah I figured since it is test code the importanc
51 { 51 {
52 return index == 0 || selector != sortedSelectors[index - 1]; 52 return index == 0 || selector != sortedSelectors[index - 1];
53 }); 53 });
54 } 54 }
55 55
56 function testResult(test, domain, expectedSelectors, specificOnly) 56 function testResult(test, domain, expectedSelectors, specificOnly)
57 { 57 {
58 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); 58 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors);
59 59
60 test.deepEqual( 60 test.deepEqual(
61 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)), 61 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)),
62 normalizedExpectedSelectors 62 normalizedExpectedSelectors
63 ); 63 );
64
65 let {code, selectors} =
66 ElemHide.generateStyleSheetForDomain(domain, specificOnly);
67
68 test.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors);
69
70 // Make sure each expected selector is in the actual CSS code.
71 for (let selector of normalizedExpectedSelectors)
72 {
73 test.ok(code.includes(selector + ", ") ||
74 code.includes(selector + " {display: none !important;}\n"));
75 }
Jon Sonesen 2018/09/20 20:10:20 I am sort of confused by the set literal `{code, s
Manish Jethani 2018/09/21 10:53:05 This is called a destructuring assignment [1] [1]
Jon Sonesen 2018/09/23 18:01:59 I do think the convention is helpful tbh so I was
64 } 76 }
65 77
66 exports.testGetSelectorsForDomain = function(test) 78 exports.testGetSelectorsForDomain = function(test)
67 { 79 {
68 let addFilter = filterText => ElemHide.add(Filter.fromText(filterText)); 80 let addFilter = filterText => ElemHide.add(Filter.fromText(filterText));
69 let removeFilter = filterText => ElemHide.remove(Filter.fromText(filterText)); 81 let removeFilter = filterText => ElemHide.remove(Filter.fromText(filterText));
70 let addException = 82 let addException =
71 filterText => ElemHideExceptions.add(Filter.fromText(filterText)); 83 filterText => ElemHideExceptions.add(Filter.fromText(filterText));
72 let removeException = 84 let removeException =
73 filterText => ElemHideExceptions.remove(Filter.fromText(filterText)); 85 filterText => ElemHideExceptions.remove(Filter.fromText(filterText));
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 303
292 let selectors = new Array(50000).map((element, index) => ".s" + index); 304 let selectors = new Array(50000).map((element, index) => ".s" + index);
293 305
294 test.equal((createStyleSheet(selectors).match(/\n/g) || []).length, 306 test.equal((createStyleSheet(selectors).match(/\n/g) || []).length,
295 Math.ceil(50000 / selectorGroupSize), 307 Math.ceil(50000 / selectorGroupSize),
296 "Style sheet should be split up into rules with at most " + 308 "Style sheet should be split up into rules with at most " +
297 selectorGroupSize + " selectors each"); 309 selectorGroupSize + " selectors each");
298 310
299 test.done(); 311 test.done();
300 }; 312 };
OLDNEW
« lib/elemHide.js ('K') | « lib/elemHide.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld