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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 const {createSandbox} = require("./_common"); | 20 const {createSandbox} = require("./_common"); |
21 | 21 |
22 let Snippets = null; | 22 let Snippets = null; |
| 23 let parseScript = null; |
23 let Filter = null; | 24 let Filter = null; |
24 | 25 |
25 exports.setUp = function(callback) | 26 exports.setUp = function(callback) |
26 { | 27 { |
27 let sandboxedRequire = createSandbox(); | 28 let sandboxedRequire = createSandbox(); |
28 ( | 29 ( |
29 {Filter} = sandboxedRequire("../lib/filterClasses"), | 30 {Filter} = sandboxedRequire("../lib/filterClasses"), |
30 {Snippets} = sandboxedRequire("../lib/snippets") | 31 {Snippets, parseScript} = sandboxedRequire("../lib/snippets") |
31 ); | 32 ); |
32 | 33 |
33 callback(); | 34 callback(); |
34 }; | 35 }; |
35 | 36 |
36 exports.testDomainRestrictions = function(test) | 37 exports.testDomainRestrictions = function(test) |
37 { | 38 { |
38 function testScriptMatches(description, filters, domain, expectedMatches) | 39 function testScriptMatches(description, filters, domain, expectedMatches) |
39 { | 40 { |
40 for (let filter of filters) | 41 for (let filter of filters) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 99 |
99 Snippets.clear(); | 100 Snippets.clear(); |
100 compareRules( | 101 compareRules( |
101 "Return no filters after clearing", | 102 "Return no filters after clearing", |
102 "www.example.com", | 103 "www.example.com", |
103 [] | 104 [] |
104 ); | 105 ); |
105 | 106 |
106 test.done(); | 107 test.done(); |
107 }; | 108 }; |
| 109 |
| 110 exports.testScriptParsing = function(test) |
| 111 { |
| 112 function checkParsedScript(description, script, expectedTree) |
| 113 { |
| 114 let tree = parseScript(script); |
| 115 test.deepEqual(tree, expectedTree, description); |
| 116 } |
| 117 |
| 118 checkParsedScript("Script with no arguments", "foo", [["foo"]]); |
| 119 checkParsedScript("Script with one argument", "foo 1", [["foo", "1"]]); |
| 120 checkParsedScript("Script with two arguments", "foo 1 Hello", |
| 121 [["foo", "1", "Hello"]]); |
| 122 checkParsedScript("Script with argument containing an escaped space", |
| 123 "foo Hello\\ world", |
| 124 [["foo", "Hello world"]]); |
| 125 checkParsedScript("Script with argument containing a quoted space", |
| 126 "foo 'Hello world'", |
| 127 [["foo", "Hello world"]]); |
| 128 checkParsedScript("Script with argument containing a quoted escaped quote", |
| 129 "foo 'Hello \\'world\\''", |
| 130 [["foo", "Hello 'world'"]]); |
| 131 checkParsedScript("Script with argument containing an escaped semicolon", |
| 132 "foo TL\\;DR", |
| 133 [["foo", "TL;DR"]]); |
| 134 checkParsedScript("Script with argument containing a quoted semicolon", |
| 135 "foo 'TL;DR'", |
| 136 [["foo", "TL;DR"]]); |
| 137 checkParsedScript("Script with argument containing single character " + |
| 138 "escape sequences", |
| 139 "foo yin\\tyang\\n", |
| 140 [["foo", "yin\tyang\n"]]); |
| 141 checkParsedScript("Script with argument containing Unicode escape sequences", |
| 142 "foo \\u0062\\ud83d\\ude42r " + |
| 143 "'l\\ud83d\\ude02mbd\\ud83d\\ude02'", [ |
| 144 ["foo", "b\ud83d\ude42r", "l\ud83d\ude02mbd\ud83d\ude02"] |
| 145 ]); |
| 146 checkParsedScript("Script with multiple commands", "foo; bar", |
| 147 [["foo"], ["bar"]]); |
| 148 checkParsedScript("Script with multiple commands and multiple arguments each", |
| 149 "foo 1 Hello; bar world! #", |
| 150 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); |
| 151 checkParsedScript("Script with multiple commands and multiple " + |
| 152 "escaped and quoted arguments each", |
| 153 "foo 1 'Hello, \\'Tommy\\'!' ;" + |
| 154 "bar Hi!\\ How\\ are\\ you? http://example.com", [ |
| 155 ["foo", "1", "Hello, 'Tommy'!"], |
| 156 ["bar", "Hi! How are you?", "http://example.com"] |
| 157 ]); |
| 158 checkParsedScript("Script with command names containing " + |
| 159 "whitespace (spaces, tabs, newlines, etc.), " + |
| 160 "quotes, and semicolons", |
| 161 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", |
| 162 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); |
| 163 checkParsedScript("Script containing Unicode composite characters", |
| 164 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", |
| 165 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); |
| 166 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", |
| 167 [["foo"], ["bar", "1"]]); |
| 168 |
| 169 test.done(); |
| 170 }; |
OLD | NEW |