OLD | NEW |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 23 matching lines...) Expand all Loading... |
34 | 34 |
35 function normalizeSelectors(selectors) | 35 function normalizeSelectors(selectors) |
36 { | 36 { |
37 // getSelectorsForDomain is currently allowed to return duplicate selectors | 37 // getSelectorsForDomain is currently allowed to return duplicate selectors |
38 // for performance reasons, so we need to remove duplicates here. | 38 // for performance reasons, so we need to remove duplicates here. |
39 return selectors.sort().filter((selector, index, selectors) => | 39 return selectors.sort().filter((selector, index, selectors) => |
40 { | 40 { |
41 return index == 0 || selector != selectors[index - 1]; | 41 return index == 0 || selector != selectors[index - 1]; |
42 }); | 42 }); |
43 } | 43 } |
44 function selectorsEqual(domain, expectedSelectors, specificOnly) | 44 function testResult(domain, expectedSelectors, criteria) |
45 { | 45 { |
| 46 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); |
| 47 |
| 48 // Test without filter keys |
46 test.deepEqual( | 49 test.deepEqual( |
47 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)), | 50 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, criteria)), |
48 normalizeSelectors(expectedSelectors) | 51 normalizedExpectedSelectors |
49 ); | 52 ); |
| 53 |
| 54 // With filter keys |
| 55 let [selectors, filterKeys] = ElemHide.getSelectorsForDomain(domain, criteri
a, |
| 56 true); |
| 57 test.deepEqual(filterKeys.map(k => ElemHide.getFilterByKey(k).selector), |
| 58 selectors); |
| 59 test.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors); |
50 } | 60 } |
51 | 61 |
52 selectorsEqual("", []); | 62 testResult("", []); |
53 | 63 |
54 addFilter("~foo.example.com,example.com##foo"); | 64 addFilter("~foo.example.com,example.com##foo"); |
55 selectorsEqual("barfoo.example.com", ["foo"]); | 65 testResult("barfoo.example.com", ["foo"]); |
56 selectorsEqual("bar.foo.example.com", []); | 66 testResult("bar.foo.example.com", []); |
57 selectorsEqual("foo.example.com", []); | 67 testResult("foo.example.com", []); |
58 selectorsEqual("example.com", ["foo"]); | 68 testResult("example.com", ["foo"]); |
59 selectorsEqual("com", []); | 69 testResult("com", []); |
60 selectorsEqual("", []); | 70 testResult("", []); |
61 | 71 |
62 addFilter("foo.example.com##turnip"); | 72 addFilter("foo.example.com##turnip"); |
63 selectorsEqual("foo.example.com", ["turnip"]); | 73 testResult("foo.example.com", ["turnip"]); |
64 selectorsEqual("example.com", ["foo"]); | 74 testResult("example.com", ["foo"]); |
65 selectorsEqual("com", []); | 75 testResult("com", []); |
66 selectorsEqual("", []); | 76 testResult("", []); |
67 | 77 |
68 addFilter("example.com#@#foo"); | 78 addFilter("example.com#@#foo"); |
69 selectorsEqual("foo.example.com", ["turnip"]); | 79 testResult("foo.example.com", ["turnip"]); |
70 selectorsEqual("example.com", []); | 80 testResult("example.com", []); |
71 selectorsEqual("com", []); | 81 testResult("com", []); |
72 selectorsEqual("", []); | 82 testResult("", []); |
73 | 83 |
74 addFilter("com##bar"); | 84 addFilter("com##bar"); |
75 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 85 testResult("foo.example.com", ["turnip", "bar"]); |
76 selectorsEqual("example.com", ["bar"]); | 86 testResult("example.com", ["bar"]); |
77 selectorsEqual("com", ["bar"]); | 87 testResult("com", ["bar"]); |
78 selectorsEqual("", []); | 88 testResult("", []); |
79 | 89 |
80 addFilter("example.com#@#bar"); | 90 addFilter("example.com#@#bar"); |
81 selectorsEqual("foo.example.com", ["turnip"]); | 91 testResult("foo.example.com", ["turnip"]); |
82 selectorsEqual("example.com", []); | 92 testResult("example.com", []); |
83 selectorsEqual("com", ["bar"]); | 93 testResult("com", ["bar"]); |
84 selectorsEqual("", []); | 94 testResult("", []); |
85 | 95 |
86 removeFilter("example.com#@#foo"); | 96 removeFilter("example.com#@#foo"); |
87 selectorsEqual("foo.example.com", ["turnip"]); | 97 testResult("foo.example.com", ["turnip"]); |
88 selectorsEqual("example.com", ["foo"]); | 98 testResult("example.com", ["foo"]); |
89 selectorsEqual("com", ["bar"]); | 99 testResult("com", ["bar"]); |
90 selectorsEqual("", []); | 100 testResult("", []); |
91 | 101 |
92 removeFilter("example.com#@#bar"); | 102 removeFilter("example.com#@#bar"); |
93 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 103 testResult("foo.example.com", ["turnip", "bar"]); |
94 selectorsEqual("example.com", ["foo", "bar"]); | 104 testResult("example.com", ["foo", "bar"]); |
95 selectorsEqual("com", ["bar"]); | 105 testResult("com", ["bar"]); |
96 selectorsEqual("", []); | 106 testResult("", []); |
97 | 107 |
98 addFilter("##generic"); | 108 addFilter("##generic"); |
99 selectorsEqual("foo.example.com", ["turnip", "bar", "generic"]); | 109 testResult("foo.example.com", ["turnip", "bar", "generic"]); |
100 selectorsEqual("example.com", ["foo", "bar", "generic"]); | 110 testResult("example.com", ["foo", "bar", "generic"]); |
101 selectorsEqual("com", ["bar", "generic"]); | 111 testResult("com", ["bar", "generic"]); |
102 selectorsEqual("", ["generic"]); | 112 testResult("", ["generic"]); |
103 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 113 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY); |
104 selectorsEqual("example.com", ["foo", "bar"], true); | 114 testResult("example.com", ["foo", "bar"], ElemHide.SPECIFIC_ONLY); |
105 selectorsEqual("com", ["bar"], true); | 115 testResult("com", ["bar"], ElemHide.SPECIFIC_ONLY); |
106 selectorsEqual("", [], true); | 116 testResult("", [], ElemHide.SPECIFIC_ONLY); |
107 removeFilter("##generic"); | 117 removeFilter("##generic"); |
108 | 118 |
109 addFilter("~adblockplus.org##example"); | 119 addFilter("~adblockplus.org##example"); |
110 selectorsEqual("adblockplus.org", []); | 120 testResult("adblockplus.org", []); |
111 selectorsEqual("", ["example"]); | 121 testResult("", ["example"]); |
112 selectorsEqual("foo.example.com", ["turnip", "bar", "example"]); | 122 testResult("foo.example.com", ["turnip", "bar", "example"]); |
113 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 123 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY); |
114 removeFilter("~adblockplus.org##example"); | 124 removeFilter("~adblockplus.org##example"); |
115 | 125 |
116 removeFilter("~foo.example.com,example.com##foo"); | 126 removeFilter("~foo.example.com,example.com##foo"); |
117 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 127 testResult("foo.example.com", ["turnip", "bar"]); |
118 selectorsEqual("example.com", ["bar"]); | 128 testResult("example.com", ["bar"]); |
119 selectorsEqual("com", ["bar"]); | 129 testResult("com", ["bar"]); |
120 selectorsEqual("", []); | 130 testResult("", []); |
121 | 131 |
122 removeFilter("com##bar"); | 132 removeFilter("com##bar"); |
123 selectorsEqual("foo.example.com", ["turnip"]); | 133 testResult("foo.example.com", ["turnip"]); |
124 selectorsEqual("example.com", []); | 134 testResult("example.com", []); |
125 selectorsEqual("com", []); | 135 testResult("com", []); |
126 selectorsEqual("", []); | 136 testResult("", []); |
127 | 137 |
128 removeFilter("foo.example.com##turnip"); | 138 removeFilter("foo.example.com##turnip"); |
129 selectorsEqual("foo.example.com", []); | 139 testResult("foo.example.com", []); |
130 selectorsEqual("example.com", []); | 140 testResult("example.com", []); |
131 selectorsEqual("com", []); | 141 testResult("com", []); |
132 selectorsEqual("", []); | 142 testResult("", []); |
133 | 143 |
134 addFilter("example.com##dupe"); | 144 addFilter("example.com##dupe"); |
135 addFilter("example.com##dupe"); | 145 addFilter("example.com##dupe"); |
136 selectorsEqual("example.com", ["dupe"]); | 146 testResult("example.com", ["dupe"]); |
137 removeFilter("example.com##dupe"); | 147 removeFilter("example.com##dupe"); |
138 selectorsEqual("example.com", []); | 148 testResult("example.com", []); |
139 removeFilter("example.com##dupe"); | 149 removeFilter("example.com##dupe"); |
140 | 150 |
141 addFilter("~foo.example.com,example.com##foo"); | 151 addFilter("~foo.example.com,example.com##foo"); |
142 | 152 |
143 addFilter("##foo"); | 153 addFilter("##foo"); |
144 selectorsEqual("foo.example.com", ["foo"]); | 154 testResult("foo.example.com", ["foo"]); |
145 selectorsEqual("example.com", ["foo"]); | 155 testResult("example.com", ["foo"]); |
146 selectorsEqual("com", ["foo"]); | 156 testResult("com", ["foo"]); |
147 selectorsEqual("", ["foo"]); | 157 testResult("", ["foo"]); |
148 removeFilter("##foo"); | 158 removeFilter("##foo"); |
149 | 159 |
150 addFilter("example.org##foo"); | 160 addFilter("example.org##foo"); |
151 selectorsEqual("foo.example.com", []); | 161 testResult("foo.example.com", []); |
152 selectorsEqual("example.com", ["foo"]); | 162 testResult("example.com", ["foo"]); |
153 selectorsEqual("com", []); | 163 testResult("com", []); |
154 selectorsEqual("", []); | 164 testResult("", []); |
155 removeFilter("example.org##foo"); | 165 removeFilter("example.org##foo"); |
156 | 166 |
157 addFilter("~example.com##foo"); | 167 addFilter("~example.com##foo"); |
158 selectorsEqual("foo.example.com", []); | 168 testResult("foo.example.com", []); |
159 selectorsEqual("example.com", ["foo"]); | 169 testResult("example.com", ["foo"]); |
160 selectorsEqual("com", ["foo"]); | 170 testResult("com", ["foo"]); |
161 selectorsEqual("", ["foo"]); | 171 testResult("", ["foo"]); |
162 removeFilter("example.org##foo"); | 172 removeFilter("~example.com##foo"); |
| 173 |
| 174 removeFilter("~foo.example.com,example.com##foo"); |
| 175 |
| 176 // Test criteria |
| 177 addFilter("##hello"); |
| 178 addFilter("~example.com##world"); |
| 179 addFilter("foo.com##specific"); |
| 180 testResult("foo.com", ["specific"], ElemHide.SPECIFIC_ONLY); |
| 181 testResult("foo.com", ["specific", "world"], ElemHide.NO_UNCONDITIONAL); |
| 182 testResult("foo.com", ["hello", "specific", "world"], ElemHide.ALL_MATCHING); |
| 183 testResult("foo.com", ["hello", "specific", "world"]); |
| 184 removeFilter("foo.com##specific"); |
| 185 removeFilter("~example.com##world"); |
| 186 removeFilter("##hello"); |
| 187 testResult("foo.com", []); |
| 188 |
| 189 addFilter("##hello"); |
| 190 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 191 testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 192 testResult("foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 193 testResult("foo.com", ["hello"]); |
| 194 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 195 testResult("bar.com", [], ElemHide.NO_UNCONDITIONAL); |
| 196 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 197 testResult("bar.com", ["hello"]); |
| 198 addFilter("foo.com#@#hello"); |
| 199 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 200 testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 201 testResult("foo.com", [], ElemHide.ALL_MATCHING); |
| 202 testResult("foo.com", []); |
| 203 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 204 testResult("bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 205 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 206 testResult("bar.com", ["hello"]); |
| 207 removeFilter("foo.com#@#hello"); |
| 208 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 209 // Note: We don't take care to track conditional selectors which became |
| 210 // unconditional when a filter was removed. This was too expensive. |
| 211 //testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 212 testResult("foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 213 testResult("foo.com", ["hello"]); |
| 214 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 215 testResult("bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 216 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 217 testResult("bar.com", ["hello"]); |
| 218 removeFilter("##hello"); |
| 219 testResult("foo.com", []); |
| 220 testResult("bar.com", []); |
| 221 |
| 222 addFilter("##hello"); |
| 223 addFilter("foo.com##hello"); |
| 224 testResult("foo.com", ["hello"]); |
| 225 removeFilter("foo.com##hello"); |
| 226 testResult("foo.com", ["hello"]); |
| 227 removeFilter("##hello"); |
| 228 testResult("foo.com", []); |
| 229 |
| 230 addFilter("##hello"); |
| 231 addFilter("foo.com##hello"); |
| 232 testResult("foo.com", ["hello"]); |
| 233 removeFilter("##hello"); |
| 234 testResult("foo.com", ["hello"]); |
| 235 removeFilter("foo.com##hello"); |
| 236 testResult("foo.com", []); |
| 237 |
| 238 // Advanced filter keys test |
| 239 testResult("", []); |
| 240 addFilter("##dupe"); |
| 241 addFilter(",,##dupe"); |
| 242 addFilter(",,,##dupe"); |
| 243 addFilter("foo.com##dupe"); |
| 244 testResult("", ["dupe"]); |
| 245 removeFilter(",,,##dupe"); |
| 246 testResult("", ["dupe"]); |
| 247 removeFilter("foo.com##dupe"); |
| 248 testResult("", ["dupe"]); |
| 249 removeFilter(",,##dupe"); |
| 250 testResult("", ["dupe"]); |
| 251 removeFilter("##dupe"); |
| 252 testResult("", []); |
163 | 253 |
164 test.done(); | 254 test.done(); |
165 }; | 255 }; |
OLD | NEW |