Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 "use strict"; | |
19 | |
20 const {createSandbox} = require("./_common"); | |
21 | |
22 let ElemHide = null; | |
23 let Filter = null; | |
24 | |
25 exports.setUp = function(callback) | |
26 { | |
27 let sandboxedRequire = createSandbox(); | |
28 ( | |
29 {ElemHide} = sandboxedRequire("../lib/elemHide"), | |
30 {Filter} = sandboxedRequire("../lib/filterClasses") | |
31 ); | |
32 | |
33 callback(); | |
34 }; | |
35 | |
36 function normalizeSelectors(selectorList) | |
37 { | |
38 let selectors; | |
39 | |
40 if (Array.isArray(selectorList)) | |
41 selectors = selectorList; | |
42 else | |
43 { | |
44 selectors = []; | |
45 for (let i = 0; i < selectorList.selectorCount; i++) | |
46 selectors.push(selectorList.selectorAt(i)); | |
47 } | |
48 | |
49 // getSelectorsForDomain is currently allowed to return duplicate selectors | |
50 // for performance reasons, so we need to remove duplicates here. | |
51 return selectors.sort().filter((selector, index, sortedSelectors) => | |
52 { | |
53 return index == 0 || selector != sortedSelectors[index - 1]; | |
54 }); | |
55 } | |
56 | |
57 function testResult(test, elemHide, domain, expectedSelectors, criteria) | |
58 { | |
59 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); | |
60 | |
61 test.deepEqual( | |
62 normalizeSelectors(elemHide.getSelectorsForDomain(domain, criteria)), | |
63 normalizedExpectedSelectors | |
64 ); | |
65 } | |
66 | |
67 exports.testGetSelectorsForDomain = function(test) | |
68 { | |
69 let elemHide = ElemHide.create(); | |
70 | |
71 let addFilter = filterText => elemHide.add(Filter.fromText(filterText)); | |
72 let removeFilter = filterText => elemHide.remove(Filter.fromText(filterText)); | |
73 | |
74 testResult(test, elemHide, "", []); | |
75 | |
76 addFilter("~foo.example.com,example.com##foo"); | |
77 testResult(test, elemHide, "barfoo.example.com", ["foo"]); | |
78 testResult(test, elemHide, "bar.foo.example.com", []); | |
79 testResult(test, elemHide, "foo.example.com", []); | |
80 testResult(test, elemHide, "example.com", ["foo"]); | |
81 testResult(test, elemHide, "com", []); | |
82 testResult(test, elemHide, "", []); | |
83 | |
84 addFilter("foo.example.com##turnip"); | |
85 testResult(test, elemHide, "foo.example.com", ["turnip"]); | |
86 testResult(test, elemHide, "example.com", ["foo"]); | |
87 testResult(test, elemHide, "com", []); | |
88 testResult(test, elemHide, "", []); | |
89 | |
90 addFilter("example.com#@#foo"); | |
91 testResult(test, elemHide, "foo.example.com", ["turnip"]); | |
92 testResult(test, elemHide, "example.com", []); | |
93 testResult(test, elemHide, "com", []); | |
94 testResult(test, elemHide, "", []); | |
95 | |
96 addFilter("com##bar"); | |
97 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); | |
98 testResult(test, elemHide, "example.com", ["bar"]); | |
99 testResult(test, elemHide, "com", ["bar"]); | |
100 testResult(test, elemHide, "", []); | |
101 | |
102 addFilter("example.com#@#bar"); | |
103 testResult(test, elemHide, "foo.example.com", ["turnip"]); | |
104 testResult(test, elemHide, "example.com", []); | |
105 testResult(test, elemHide, "com", ["bar"]); | |
106 testResult(test, elemHide, "", []); | |
107 | |
108 removeFilter("example.com#@#foo"); | |
109 testResult(test, elemHide, "foo.example.com", ["turnip"]); | |
110 testResult(test, elemHide, "example.com", ["foo"]); | |
111 testResult(test, elemHide, "com", ["bar"]); | |
112 testResult(test, elemHide, "", []); | |
113 | |
114 removeFilter("example.com#@#bar"); | |
115 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); | |
116 testResult(test, elemHide, "example.com", ["foo", "bar"]); | |
117 testResult(test, elemHide, "com", ["bar"]); | |
118 testResult(test, elemHide, "", []); | |
119 | |
120 addFilter("##generic"); | |
121 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "generic"]); | |
122 testResult(test, elemHide, "example.com", ["foo", "bar", "generic"]); | |
123 testResult(test, elemHide, "com", ["bar", "generic"]); | |
124 testResult(test, elemHide, "", ["generic"]); | |
125 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SPEC IFIC_ONLY); | |
126 testResult(test, elemHide, "example.com", ["foo", "bar"], ElemHide.SPECIFIC_ON LY); | |
127 testResult(test, elemHide, "com", ["bar"], ElemHide.SPECIFIC_ONLY); | |
128 testResult(test, elemHide, "", [], ElemHide.SPECIFIC_ONLY); | |
129 removeFilter("##generic"); | |
130 | |
131 addFilter("~adblockplus.org##example"); | |
132 testResult(test, elemHide, "adblockplus.org", []); | |
133 testResult(test, elemHide, "", ["example"]); | |
134 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "example"]); | |
135 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SPEC IFIC_ONLY); | |
136 removeFilter("~adblockplus.org##example"); | |
137 | |
138 removeFilter("~foo.example.com,example.com##foo"); | |
139 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); | |
140 testResult(test, elemHide, "example.com", ["bar"]); | |
141 testResult(test, elemHide, "com", ["bar"]); | |
142 testResult(test, elemHide, "", []); | |
143 | |
144 removeFilter("com##bar"); | |
145 testResult(test, elemHide, "foo.example.com", ["turnip"]); | |
146 testResult(test, elemHide, "example.com", []); | |
147 testResult(test, elemHide, "com", []); | |
148 testResult(test, elemHide, "", []); | |
149 | |
150 removeFilter("foo.example.com##turnip"); | |
151 testResult(test, elemHide, "foo.example.com", []); | |
152 testResult(test, elemHide, "example.com", []); | |
153 testResult(test, elemHide, "com", []); | |
154 testResult(test, elemHide, "", []); | |
155 | |
156 addFilter("example.com##dupe"); | |
157 addFilter("example.com##dupe"); | |
158 testResult(test, elemHide, "example.com", ["dupe"]); | |
159 removeFilter("example.com##dupe"); | |
160 testResult(test, elemHide, "example.com", []); | |
161 removeFilter("example.com##dupe"); | |
162 | |
163 addFilter("~foo.example.com,example.com##foo"); | |
164 | |
165 addFilter("##foo"); | |
166 testResult(test, elemHide, "foo.example.com", ["foo"]); | |
167 testResult(test, elemHide, "example.com", ["foo"]); | |
168 testResult(test, elemHide, "com", ["foo"]); | |
169 testResult(test, elemHide, "", ["foo"]); | |
170 removeFilter("##foo"); | |
171 | |
172 addFilter("example.org##foo"); | |
173 testResult(test, elemHide, "foo.example.com", []); | |
174 testResult(test, elemHide, "example.com", ["foo"]); | |
175 testResult(test, elemHide, "com", []); | |
176 testResult(test, elemHide, "", []); | |
177 removeFilter("example.org##foo"); | |
178 | |
179 addFilter("~example.com##foo"); | |
180 testResult(test, elemHide, "foo.example.com", []); | |
181 testResult(test, elemHide, "example.com", ["foo"]); | |
182 testResult(test, elemHide, "com", ["foo"]); | |
183 testResult(test, elemHide, "", ["foo"]); | |
184 removeFilter("~example.com##foo"); | |
185 | |
186 removeFilter("~foo.example.com,example.com##foo"); | |
187 | |
188 // Test criteria | |
189 addFilter("##hello"); | |
190 addFilter("~example.com##world"); | |
191 addFilter("foo.com##specific"); | |
192 testResult(test, elemHide, "foo.com", ["specific"], ElemHide.SPECIFIC_ONLY); | |
193 testResult(test, elemHide, "foo.com", ["specific", "world"], ElemHide.NO_UNCON DITIONAL); | |
194 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"], ElemHide .ALL_MATCHING); | |
195 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"]); | |
196 removeFilter("foo.com##specific"); | |
197 removeFilter("~example.com##world"); | |
198 removeFilter("##hello"); | |
199 testResult(test, elemHide, "foo.com", []); | |
200 | |
201 addFilter("##hello"); | |
202 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); | |
203 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); | |
204 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); | |
205 testResult(test, elemHide, "foo.com", ["hello"]); | |
206 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); | |
207 testResult(test, elemHide, "bar.com", [], ElemHide.NO_UNCONDITIONAL); | |
208 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); | |
209 testResult(test, elemHide, "bar.com", ["hello"]); | |
210 addFilter("foo.com#@#hello"); | |
211 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); | |
212 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); | |
213 testResult(test, elemHide, "foo.com", [], ElemHide.ALL_MATCHING); | |
214 testResult(test, elemHide, "foo.com", []); | |
215 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); | |
216 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); | |
217 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); | |
218 testResult(test, elemHide, "bar.com", ["hello"]); | |
219 removeFilter("foo.com#@#hello"); | |
220 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); | |
221 // Note: We don't take care to track conditional selectors which became | |
222 // unconditional when a filter was removed. This was too expensive. | |
223 // testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); | |
224 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); | |
225 testResult(test, elemHide, "foo.com", ["hello"]); | |
226 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); | |
227 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); | |
228 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); | |
229 testResult(test, elemHide, "bar.com", ["hello"]); | |
230 removeFilter("##hello"); | |
231 testResult(test, elemHide, "foo.com", []); | |
232 testResult(test, elemHide, "bar.com", []); | |
233 | |
234 addFilter("##hello"); | |
235 addFilter("foo.com##hello"); | |
236 testResult(test, elemHide, "foo.com", ["hello"]); | |
237 removeFilter("foo.com##hello"); | |
238 testResult(test, elemHide, "foo.com", ["hello"]); | |
239 removeFilter("##hello"); | |
240 testResult(test, elemHide, "foo.com", []); | |
241 | |
242 addFilter("##hello"); | |
243 addFilter("foo.com##hello"); | |
244 testResult(test, elemHide, "foo.com", ["hello"]); | |
245 removeFilter("##hello"); | |
246 testResult(test, elemHide, "foo.com", ["hello"]); | |
247 removeFilter("foo.com##hello"); | |
248 testResult(test, elemHide, "foo.com", []); | |
249 | |
250 elemHide.delete(); | |
251 test.done(); | |
252 }; | |
253 | |
254 exports.testZeroFilterKey = function(test) | |
255 { | |
256 let elemHide = ElemHide.create(); | |
257 | |
258 elemHide.add(Filter.fromText("##test")); | |
sergei
2018/01/22 15:40:07
there is a memory leak, Filter.fromText should be
hub
2018/01/22 16:49:17
This is the kind of changes that are self containe
| |
259 elemHide.add(Filter.fromText("foo.com#@#test")); | |
260 testResult(test, elemHide, "foo.com", []); | |
261 testResult(test, elemHide, "bar.com", ["test"]); | |
262 | |
263 elemHide.delete(); | |
264 test.done(); | |
265 }; | |
OLD | NEW |