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