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-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 SnippetFilter = null; | |
23 let Snippets = null; | 22 let Snippets = null; |
24 let parseScript = null; | 23 let parseScript = null; |
25 let ElemHide = null; | |
26 let Filter = null; | 24 let Filter = null; |
27 | 25 |
28 exports.setUp = function(callback) | 26 exports.setUp = function(callback) |
29 { | 27 { |
30 let sandboxedRequire = createSandbox(); | 28 let sandboxedRequire = createSandbox(); |
31 ( | 29 ( |
32 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 30 {Filter} = sandboxedRequire("../lib/filterClasses"), |
33 {ElemHide} = sandboxedRequire("../lib/elemHide"), | |
34 {Snippets, parseScript} = sandboxedRequire("../lib/snippets") | 31 {Snippets, parseScript} = sandboxedRequire("../lib/snippets") |
35 ); | 32 ); |
36 | 33 |
37 callback(); | 34 callback(); |
38 }; | 35 }; |
39 | 36 |
40 exports.testDomainRestrictions = function(test) | 37 exports.testDomainRestrictions = function(test) |
41 { | 38 { |
42 function testSelectorMatches(description, filters, domain, expectedMatches) | 39 function testScriptMatches(description, filters, domain, expectedMatches) |
43 { | 40 { |
44 for (let filter of filters) | 41 for (let filter of filters) |
45 { | 42 Snippets.add(Filter.fromText(filter)); |
46 filter = Filter.fromText(filter); | |
47 if (filter instanceof SnippetFilter) | |
48 Snippets.add(filter); | |
49 else | |
50 ElemHide.add(filter); | |
51 } | |
52 | 43 |
53 let matches = Snippets.getScriptsForDomain(domain) | 44 let matches = Snippets.getScriptsForDomain(domain); |
54 .map(filter => filter.text); | |
55 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 45 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
56 | 46 |
57 Snippets.clear(); | 47 Snippets.clear(); |
58 ElemHide.clear(); | |
59 } | 48 } |
60 | 49 |
61 testSelectorMatches( | 50 testScriptMatches( |
62 "Ignore selectors with exceptions", | |
63 [ | |
64 "example.com#$#foo", | |
65 "example.com#$#bar", | |
66 "example.com#@#foo" | |
67 ], | |
68 "example.com", | |
69 ["example.com#$#bar"] | |
70 ); | |
71 testSelectorMatches( | |
72 "Ignore filters that include parent domain but exclude subdomain", | 51 "Ignore filters that include parent domain but exclude subdomain", |
73 [ | 52 [ |
74 "~www.example.com,example.com#$#foo" | 53 "~www.example.com,example.com#$#foo" |
75 ], | 54 ], |
76 "www.example.com", | 55 "www.example.com", |
77 [] | 56 [] |
78 ); | 57 ); |
79 testSelectorMatches( | 58 testScriptMatches( |
80 "Ignore filters with parent domain if exception matches subdomain", | |
81 [ | |
82 "www.example.com#@#foo", | |
83 "example.com#$#foo" | |
84 ], | |
85 "www.example.com", | |
86 [] | |
87 ); | |
88 testSelectorMatches( | |
89 "Ignore filters for other subdomain", | 59 "Ignore filters for other subdomain", |
90 [ | 60 [ |
91 "www.example.com#$#foo", | 61 "www.example.com#$#foo", |
92 "other.example.com#$#foo" | 62 "other.example.com#$#foo" |
93 ], | 63 ], |
94 "other.example.com", | 64 "other.example.com", |
95 ["other.example.com#$#foo"] | 65 ["foo"] |
96 ); | 66 ); |
97 | 67 |
98 test.done(); | 68 test.done(); |
99 }; | 69 }; |
100 | 70 |
101 exports.testSnippetFiltersContainer = function(test) | 71 exports.testSnippetFiltersContainer = function(test) |
102 { | 72 { |
103 function compareRules(description, domain, expectedMatches) | 73 function compareRules(description, domain, expectedMatches) |
104 { | 74 { |
105 let result = Snippets.getScriptsForDomain(domain) | 75 let result = Snippets.getScriptsForDomain(domain); |
106 .map(filter => filter.text); | 76 expectedMatches = expectedMatches.map(filter => filter.script); |
107 expectedMatches = expectedMatches.map(filter => filter.text); | |
108 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 77 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
109 } | 78 } |
110 | 79 |
111 let domainFilter = Filter.fromText("example.com#$#filter1"); | 80 let domainFilter = Filter.fromText("example.com#$#filter1"); |
112 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 81 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); |
113 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 82 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); |
114 | 83 |
115 Snippets.add(domainFilter); | 84 Snippets.add(domainFilter); |
116 Snippets.add(subdomainFilter); | 85 Snippets.add(subdomainFilter); |
117 Snippets.add(otherDomainFilter); | 86 Snippets.add(otherDomainFilter); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 [["foo", "Hello world"]]); | 127 [["foo", "Hello world"]]); |
159 checkParsedScript("Script with argument containing a quoted escaped quote", | 128 checkParsedScript("Script with argument containing a quoted escaped quote", |
160 "foo 'Hello \\'world\\''", | 129 "foo 'Hello \\'world\\''", |
161 [["foo", "Hello 'world'"]]); | 130 [["foo", "Hello 'world'"]]); |
162 checkParsedScript("Script with argument containing an escaped semicolon", | 131 checkParsedScript("Script with argument containing an escaped semicolon", |
163 "foo TL\\;DR", | 132 "foo TL\\;DR", |
164 [["foo", "TL;DR"]]); | 133 [["foo", "TL;DR"]]); |
165 checkParsedScript("Script with argument containing a quoted semicolon", | 134 checkParsedScript("Script with argument containing a quoted semicolon", |
166 "foo 'TL;DR'", | 135 "foo 'TL;DR'", |
167 [["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 ]); |
168 checkParsedScript("Script with multiple commands", "foo; bar", | 146 checkParsedScript("Script with multiple commands", "foo; bar", |
169 [["foo"], ["bar"]]); | 147 [["foo"], ["bar"]]); |
170 checkParsedScript("Script with multiple commands and multiple arguments each", | 148 checkParsedScript("Script with multiple commands and multiple arguments each", |
171 "foo 1 Hello; bar world! #", | 149 "foo 1 Hello; bar world! #", |
172 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); | 150 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); |
173 checkParsedScript( | 151 checkParsedScript("Script with multiple commands and multiple " + |
174 "Script with multiple commands and multiple " + | 152 "escaped and quoted arguments each", |
175 "escaped and quoted arguments each", | 153 "foo 1 'Hello, \\'Tommy\\'!' ;" + |
176 "foo 1 'Hello, \\'Tommy\\'!' ;" + | 154 "bar Hi!\\ How\\ are\\ you? http://example.com", [ |
177 "bar Hi!\\ How\\ are\\ you? http://example.com", | 155 ["foo", "1", "Hello, 'Tommy'!"], |
178 [["foo", "1", "Hello, 'Tommy'!"], | 156 ["bar", "Hi! How are you?", "http://example.com"] |
179 ["bar", "Hi! How are you?", "http://example.com"]] | 157 ]); |
180 ); | |
181 checkParsedScript("Script with command names containing " + | 158 checkParsedScript("Script with command names containing " + |
182 "whitespace (spaces, tabs, newlines, etc.), " + | 159 "whitespace (spaces, tabs, newlines, etc.), " + |
183 "quotes, and semicolons", | 160 "quotes, and semicolons", |
184 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 161 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", |
185 [["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"]]); |
186 checkParsedScript("Script containing Unicode composite characters", | 163 checkParsedScript("Script containing Unicode composite characters", |
187 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 164 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", |
188 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 165 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); |
189 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 166 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", |
190 [["foo"], ["bar", "1"]]); | 167 [["foo"], ["bar", "1"]]); |
191 | 168 |
192 test.done(); | 169 test.done(); |
193 }; | 170 }; |
LEFT | RIGHT |