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 LONG_DOMAINS_THRESHOLD = null; |
| 23 let optimizeContentFilterText = null; |
| 24 let optimizeRegExpFilterText = null; |
| 25 |
| 26 function randomName() |
| 27 { |
| 28 let str = Math.random().toString(36).substring(2); |
| 29 return str.substring(0, Math.ceil(Math.random() * str.length)); |
| 30 } |
| 31 |
| 32 function randomDomain() |
| 33 { |
| 34 // Create a domain with between one and four parts to cover all the usual |
| 35 // cases. |
| 36 let domain = randomName(); |
| 37 for (let i = 0; i < Math.floor(Math.random() * 4); i++) |
| 38 domain += "." + randomName(); |
| 39 return domain; |
| 40 } |
| 41 |
| 42 exports.setUp = function(callback) |
| 43 { |
| 44 let sandboxedRequire = createSandbox({ |
| 45 extraExports: { |
| 46 filterText: ["LONG_DOMAINS_THRESHOLD"] |
| 47 } |
| 48 }); |
| 49 ( |
| 50 {LONG_DOMAINS_THRESHOLD, |
| 51 optimizeContentFilterText, |
| 52 optimizeRegExpFilterText} = sandboxedRequire("../lib/filterText") |
| 53 ); |
| 54 |
| 55 callback(); |
| 56 }; |
| 57 |
| 58 exports.testOptimizeContentFilterText = function(test) |
| 59 { |
| 60 let domains = randomDomain(); |
| 61 while (domains.length < LONG_DOMAINS_THRESHOLD) |
| 62 domains += "," + randomDomain(); |
| 63 |
| 64 let filterParts = [ |
| 65 [domains, null, "." + randomName()], |
| 66 [domains, "@", "." + randomName()], |
| 67 [domains, "?", `.${randomName()}:-abp-contains(${randomName()})`], |
| 68 [domains, "$", |
| 69 `${randomName()} '${randomName()} ${randomName()}' ${randomName()}`] |
| 70 ]; |
| 71 |
| 72 let filters = filterParts.map( |
| 73 ([domains_, type, selector]) => |
| 74 [domains_ + "#" + (type || "") + "#" + selector, |
| 75 domains_, type, selector] |
| 76 ); |
| 77 |
| 78 // We just have to make sure that the parts returned are the same as the |
| 79 // parts passed in to the function. |
| 80 for (let filter of filters) |
| 81 test.deepEqual(optimizeContentFilterText(...filter), filter); |
| 82 |
| 83 test.done(); |
| 84 }; |
| 85 |
| 86 exports.testOptimizeRegExpFilterText = function(test) |
| 87 { |
| 88 let domains = randomDomain(); |
| 89 while (domains.length < LONG_DOMAINS_THRESHOLD) |
| 90 domains += "|" + randomDomain(); |
| 91 |
| 92 let filterParts = [ |
| 93 ["", randomName(), domains], |
| 94 ["@@", randomName(), domains] |
| 95 ]; |
| 96 |
| 97 let filters = filterParts.map( |
| 98 ([marker, pattern, domains_]) => [marker + pattern + "$domain=" + domains, |
| 99 pattern, domains, null, null, null] |
| 100 ); |
| 101 |
| 102 for (let filter of filters) |
| 103 test.deepEqual(optimizeRegExpFilterText(...filter), filter); |
| 104 |
| 105 test.done(); |
| 106 }; |
OLD | NEW |