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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 Filter = require("filterClasses").Filter; | 20 let Filter = require("filterClasses").Filter; |
21 let ContentBlockerList = require("../lib/abp2blocklist.js").ContentBlockerList; | 21 let ContentBlockerList = require("../lib/abp2blocklist.js").ContentBlockerList; |
22 | 22 |
23 function testRules(test, filters, expected, transformFunction) | 23 function testRules(test, filters, expected, transformFunction, |
| 24 {merge = false, exhaustiveMerge = false} = {}) |
24 { | 25 { |
25 let blockerList = new ContentBlockerList(); | 26 let blockerList = new ContentBlockerList(); |
26 for (let filter of filters) | 27 for (let filter of filters) |
27 blockerList.addFilter(Filter.fromText(filter)); | 28 blockerList.addFilter(Filter.fromText(filter)); |
28 | 29 |
29 let rules = blockerList.generateRules(); | 30 let rules = blockerList.generateRules({merge, exhaustiveMerge}); |
30 if (transformFunction) | 31 if (transformFunction) |
31 rules = transformFunction(rules); | 32 rules = transformFunction(rules); |
32 | 33 |
33 test.deepEqual(rules, expected); | 34 test.deepEqual(rules, expected); |
34 } | 35 } |
35 | 36 |
36 exports.generateRules = { | 37 exports.generateRules = { |
37 testElementHiding: function(test) | 38 testElementHiding: function(test) |
38 { | 39 { |
39 testRules(test, ["##.whatever"], [ | 40 testRules(test, ["##.whatever"], [ |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 }, | 225 }, |
225 | 226 |
226 testUnicode: function(test) | 227 testUnicode: function(test) |
227 { | 228 { |
228 testRules(test, ["$domain=🐈.cat"], ["xn--zn8h.cat", "www.xn--zn8h.cat"], | 229 testRules(test, ["$domain=🐈.cat"], ["xn--zn8h.cat", "www.xn--zn8h.cat"], |
229 rules => rules[0]["trigger"]["if-domain"]); | 230 rules => rules[0]["trigger"]["if-domain"]); |
230 testRules(test, ["🐈$domain=🐈.cat"], []); | 231 testRules(test, ["🐈$domain=🐈.cat"], []); |
231 testRules(test, ["###🐈"], []); | 232 testRules(test, ["###🐈"], []); |
232 | 233 |
233 test.done(); | 234 test.done(); |
| 235 }, |
| 236 |
| 237 testMerging: function(test) |
| 238 { |
| 239 // Single character substitutions, deletions, and insertions. |
| 240 testRules(test, ["/ads", "/adv"], ["^https?://.*/ad[sv]"], |
| 241 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 242 {merge: true}); |
| 243 testRules(test, ["/ad", "/ads"], ["^https?://.*/ads?"], |
| 244 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 245 {merge: true}); |
| 246 testRules(test, ["/ads", "/ad"], ["^https?://.*/ads?"], |
| 247 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 248 {merge: true}); |
| 249 testRules(test, ["/ads", "/adv", "/ad"], ["^https?://.*/ad[sv]?"], |
| 250 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 251 {merge: true}); |
| 252 testRules(test, ["/ad", "/ads", "/adv"], ["^https?://.*/ad[sv]?"], |
| 253 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 254 {merge: true}); |
| 255 testRules(test, ["/ads", "/ad", "/adv"], ["^https?://.*/ad[sv]?"], |
| 256 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 257 {merge: true}); |
| 258 testRules(test, ["/a", "/ad", "/ads", "/adv"], |
| 259 ["^https?://.*/a", "^https?://.*/ad[sv]?"], |
| 260 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 261 {merge: true}); |
| 262 testRules(test, ["/ad", "/a", "/ads", "/adv"], |
| 263 ["^https?://.*/a", "^https?://.*/ad[sv]?"], |
| 264 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 265 {merge: true}); |
| 266 testRules(test, ["/ads", "/adv", "/ad", "/a"], |
| 267 ["^https?://.*/ad[sv]?", "^https?://.*/a"], |
| 268 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 269 {merge: true}); |
| 270 testRules(test, ["/ads", "/adv", "/a", "/ad"], |
| 271 ["^https?://.*/ad[sv]?", "^https?://.*/a"], |
| 272 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 273 {merge: true}); |
| 274 testRules(test, ["/ad", "/a", "/ads", "/adv", "/adx"], |
| 275 ["^https?://.*/a", "^https?://.*/ad[svx]?"], |
| 276 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 277 {merge: true}); |
| 278 testRules(test, ["/ads", "/a", "/ad", "/adv", "/adx"], |
| 279 ["^https?://.*/ad[svx]?", "^https?://.*/a"], |
| 280 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 281 {merge: true}); |
| 282 |
| 283 // Multiple character deletions and insertions. |
| 284 testRules(test, ["/ad", "/adxsi"], |
| 285 ["^https?://.*/ad(xsi)?"], |
| 286 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 287 {merge: true}); |
| 288 testRules(test, ["/adxsi", "/xsi"], |
| 289 ["^https?://.*/(ad)?xsi"], |
| 290 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 291 {merge: true}); |
| 292 testRules(test, ["/adxsi", "/ai"], |
| 293 ["^https?://.*/a(dxs)?i"], |
| 294 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 295 {merge: true}); |
| 296 |
| 297 // Both single and multiple character edits combined. |
| 298 testRules(test, ["/ad", "/adxsi", "/xsi", "/axsi", "/bxsi"], |
| 299 ["^https?://.*/ad(xsi)?", "^https?://.*/[ab]?xsi"], |
| 300 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 301 {merge: true}); |
| 302 |
| 303 testRules(test, ["/ads", "/a", "/ad", "/adv", "/adx", "/adxs"], |
| 304 ["^https?://.*/ad[svx]?", "^https?://.*/a(dxs)?"], |
| 305 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 306 {merge: true}); |
| 307 testRules(test, ["/adxs", "/a", "/ad", "/ads", "/adv", "/adx"], |
| 308 ["^https?://.*/a(dxs)?", "^https?://.*/ad[svx]?"], |
| 309 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 310 {merge: true}); |
| 311 testRules(test, ["/adxs", "/a", "/ads", "/ad", "/adv", "/adx"], |
| 312 ["^https?://.*/a(dxs)?", "^https?://.*/ad[svx]?"], |
| 313 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 314 {merge: true}); |
| 315 testRules(test, ["/adxs", "/a", "/ads", "/adv", "/ad", "/adx"], |
| 316 ["^https?://.*/a(dxs)?", "^https?://.*/ad[svx]?"], |
| 317 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 318 {merge: true}); |
| 319 testRules(test, ["/adsxi", "/adxs", "/a", "/ads", "/adv", "/ad", "/adx"], |
| 320 ["^https?://.*/a(dsxi)?", "^https?://.*/adxs", |
| 321 "^https?://.*/ad[svx]?"], |
| 322 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 323 {merge: true}); |
| 324 testRules(test, ["/adxsi", "/adsxi", "/adxs", "/a", "/ads", "/adv", "/ad", |
| 325 "/adx"], |
| 326 ["^https?://.*/adxsi?", "^https?://.*/a(dsxi)?", |
| 327 "^https?://.*/ad[svx]?"], |
| 328 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 329 {merge: true}); |
| 330 |
| 331 // Given the 6 rules "ads", "bds", "adv", "bdv", "adx", and "bdx", we want |
| 332 // the 2 rules "ad[svx]" and "bd[svx]", not the 3 rules "[ab]ds", "[ab]dv", |
| 333 // and "[ab]dx" |
| 334 testRules(test, ["/ads", "/bds", "/adv", "/bdv", "/adx", "/bdx"], |
| 335 ["^https?://.*/ad[svx]", "^https?://.*/bd[svx]"], |
| 336 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 337 {merge: true}); |
| 338 testRules(test, ["/ads", "/bds", "/adv", "/bdv", "/bdx"], |
| 339 ["^https?://.*/ad[sv]", "^https?://.*/bd[svx]"], |
| 340 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 341 {merge: true}); |
| 342 |
| 343 // Make sure there's no merge where there are special characters in the |
| 344 // delta. |
| 345 testRules(test, ["/ads?", "/ads"], |
| 346 ["^https?://.*/ads\\?", "^https?://.*/ads"], |
| 347 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 348 {merge: true}); |
| 349 testRules(test, ["/ads?", "/ads-"], |
| 350 ["^https?://.*/ads\\?", "^https?://.*/ads-"], |
| 351 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 352 {merge: true}); |
| 353 testRules(test, ["/ads?-", "/ads-"], |
| 354 ["^https?://.*/ads\\?-", "^https?://.*/ads-"], |
| 355 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 356 {merge: true}); |
| 357 |
| 358 // In exhaustive merge, redundant rules should be discarded. |
| 359 testRules(test, ["/ad", "/ads", "/advertisement"], |
| 360 ["^https?://.*/ad"], |
| 361 rules => rules.map(rule => rule.trigger["url-filter"]), |
| 362 {merge: true, exhaustiveMerge: true}); |
| 363 |
| 364 test.done(); |
234 } | 365 } |
235 }; | 366 }; |
OLD | NEW |