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