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