 Issue 29761612:
  Issue 6538, 6781 - Implement script compilation for snippets  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/
    
  
    Issue 29761612:
  Issue 6538, 6781 - Implement script compilation for snippets  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore/| 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 /* eslint no-new-func: "off" */ | 18 /* eslint no-new-func: "off" */ | 
| 19 | 19 | 
| 20 "use strict"; | 20 "use strict"; | 
| 21 | 21 | 
| 22 const {createSandbox} = require("./_common"); | 22 const {createSandbox} = require("./_common"); | 
| 23 | 23 | 
| 24 let SnippetFilter = null; | |
| 25 let Snippets = null; | 24 let Snippets = null; | 
| 26 let parseScript = null; | 25 let parseScript = null; | 
| 27 let compileScript = null; | 26 let compileScript = null; | 
| 28 let ElemHide = null; | |
| 29 let Filter = null; | 27 let Filter = null; | 
| 30 | 28 | 
| 31 exports.setUp = function(callback) | 29 exports.setUp = function(callback) | 
| 32 { | 30 { | 
| 33 let sandboxedRequire = createSandbox(); | 31 let sandboxedRequire = createSandbox(); | 
| 34 ( | 32 ( | 
| 35 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 33 {Filter} = sandboxedRequire("../lib/filterClasses"), | 
| 36 {ElemHide} = sandboxedRequire("../lib/elemHide"), | |
| 37 {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets") | 34 {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets") | 
| 38 ); | 35 ); | 
| 39 | 36 | 
| 40 callback(); | 37 callback(); | 
| 41 }; | 38 }; | 
| 42 | 39 | 
| 43 exports.testDomainRestrictions = function(test) | 40 exports.testDomainRestrictions = function(test) | 
| 44 { | 41 { | 
| 45 function testSelectorMatches(description, filters, domain, expectedMatches) | 42 function testScriptMatches(description, filters, domain, expectedMatches) | 
| 46 { | 43 { | 
| 47 for (let filter of filters) | 44 for (let filter of filters) | 
| 48 { | 45 Snippets.add(Filter.fromText(filter)); | 
| 49 filter = Filter.fromText(filter); | 46 | 
| 50 if (filter instanceof SnippetFilter) | 47 let matches = Snippets.getScriptsForDomain(domain); | 
| 51 Snippets.add(filter); | |
| 52 else | |
| 53 ElemHide.add(filter); | |
| 54 } | |
| 55 | |
| 56 let matches = Snippets.getScriptsForDomain(domain) | |
| 57 .map(filter => filter.text); | |
| 58 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 48 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 
| 59 | 49 | 
| 60 Snippets.clear(); | 50 Snippets.clear(); | 
| 61 ElemHide.clear(); | 51 } | 
| 62 } | 52 | 
| 63 | 53 testScriptMatches( | 
| 64 testSelectorMatches( | |
| 65 "Ignore selectors with exceptions", | |
| 66 [ | |
| 67 "example.com#$#foo", | |
| 68 "example.com#$#bar", | |
| 69 "example.com#@#foo" | |
| 70 ], | |
| 71 "example.com", | |
| 72 ["example.com#$#bar"] | |
| 73 ); | |
| 74 testSelectorMatches( | |
| 75 "Ignore filters that include parent domain but exclude subdomain", | 54 "Ignore filters that include parent domain but exclude subdomain", | 
| 76 [ | 55 [ | 
| 77 "~www.example.com,example.com#$#foo" | 56 "~www.example.com,example.com#$#foo" | 
| 78 ], | 57 ], | 
| 79 "www.example.com", | 58 "www.example.com", | 
| 80 [] | 59 [] | 
| 81 ); | 60 ); | 
| 82 testSelectorMatches( | 61 testScriptMatches( | 
| 83 "Ignore filters with parent domain if exception matches subdomain", | |
| 84 [ | |
| 85 "www.example.com#@#foo", | |
| 86 "example.com#$#foo" | |
| 87 ], | |
| 88 "www.example.com", | |
| 89 [] | |
| 90 ); | |
| 91 testSelectorMatches( | |
| 92 "Ignore filters for other subdomain", | 62 "Ignore filters for other subdomain", | 
| 93 [ | 63 [ | 
| 94 "www.example.com#$#foo", | 64 "www.example.com#$#foo", | 
| 95 "other.example.com#$#foo" | 65 "other.example.com#$#foo" | 
| 96 ], | 66 ], | 
| 97 "other.example.com", | 67 "other.example.com", | 
| 98 ["other.example.com#$#foo"] | 68 ["foo"] | 
| 99 ); | 69 ); | 
| 100 | 70 | 
| 101 test.done(); | 71 test.done(); | 
| 102 }; | 72 }; | 
| 103 | 73 | 
| 104 exports.testSnippetFiltersContainer = function(test) | 74 exports.testSnippetFiltersContainer = function(test) | 
| 105 { | 75 { | 
| 106 function compareRules(description, domain, expectedMatches) | 76 function compareRules(description, domain, expectedMatches) | 
| 107 { | 77 { | 
| 108 let result = Snippets.getScriptsForDomain(domain) | 78 let result = Snippets.getScriptsForDomain(domain); | 
| 109 .map(filter => filter.text); | 79 expectedMatches = expectedMatches.map(filter => filter.script); | 
| 110 expectedMatches = expectedMatches.map(filter => filter.text); | |
| 111 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 80 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 
| 112 } | 81 } | 
| 113 | 82 | 
| 114 let domainFilter = Filter.fromText("example.com#$#filter1"); | 83 let domainFilter = Filter.fromText("example.com#$#filter1"); | 
| 115 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 84 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 
| 116 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 85 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 
| 117 | 86 | 
| 118 Snippets.add(domainFilter); | 87 Snippets.add(domainFilter); | 
| 119 Snippets.add(subdomainFilter); | 88 Snippets.add(subdomainFilter); | 
| 120 Snippets.add(otherDomainFilter); | 89 Snippets.add(otherDomainFilter); | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 [["foo", "Hello world"]]); | 130 [["foo", "Hello world"]]); | 
| 162 checkParsedScript("Script with argument containing a quoted escaped quote", | 131 checkParsedScript("Script with argument containing a quoted escaped quote", | 
| 163 "foo 'Hello \\'world\\''", | 132 "foo 'Hello \\'world\\''", | 
| 164 [["foo", "Hello 'world'"]]); | 133 [["foo", "Hello 'world'"]]); | 
| 165 checkParsedScript("Script with argument containing an escaped semicolon", | 134 checkParsedScript("Script with argument containing an escaped semicolon", | 
| 166 "foo TL\\;DR", | 135 "foo TL\\;DR", | 
| 167 [["foo", "TL;DR"]]); | 136 [["foo", "TL;DR"]]); | 
| 168 checkParsedScript("Script with argument containing a quoted semicolon", | 137 checkParsedScript("Script with argument containing a quoted semicolon", | 
| 169 "foo 'TL;DR'", | 138 "foo 'TL;DR'", | 
| 170 [["foo", "TL;DR"]]); | 139 [["foo", "TL;DR"]]); | 
| 140 checkParsedScript("Script with argument containing single character " + | |
| 141 "escape sequences", | |
| 142 "foo yin\\tyang\\n", | |
| 143 [["foo", "yin\tyang\n"]]); | |
| 144 checkParsedScript("Script with argument containing Unicode escape sequences", | |
| 145 "foo \\u0062\\ud83d\\ude42r " + | |
| 146 "'l\\ud83d\\ude02mbd\\ud83d\\ude02'", [ | |
| 147 ["foo", "b\ud83d\ude42r", "l\ud83d\ude02mbd\ud83d\ude02"] | |
| 148 ]); | |
| 171 checkParsedScript("Script with multiple commands", "foo; bar", | 149 checkParsedScript("Script with multiple commands", "foo; bar", | 
| 172 [["foo"], ["bar"]]); | 150 [["foo"], ["bar"]]); | 
| 173 checkParsedScript("Script with multiple commands and multiple arguments each", | 151 checkParsedScript("Script with multiple commands and multiple arguments each", | 
| 174 "foo 1 Hello; bar world! #", | 152 "foo 1 Hello; bar world! #", | 
| 175 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); | 153 [["foo", "1", "Hello"], ["bar", "world!", "#"]]); | 
| 176 checkParsedScript( | 154 checkParsedScript("Script with multiple commands and multiple " + | 
| 177 "Script with multiple commands and multiple " + | 155 "escaped and quoted arguments each", | 
| 178 "escaped and quoted arguments each", | 156 "foo 1 'Hello, \\'Tommy\\'!' ;" + | 
| 179 "foo 1 'Hello, \\'Tommy\\'!' ;" + | 157 "bar Hi!\\ How\\ are\\ you? http://example.com", [ | 
| 180 "bar Hi!\\ How\\ are\\ you? http://example.com", | 158 ["foo", "1", "Hello, 'Tommy'!"], | 
| 181 [["foo", "1", "Hello, 'Tommy'!"], | 159 ["bar", "Hi! How are you?", "http://example.com"] | 
| 182 ["bar", "Hi! How are you?", "http://example.com"]] | 160 ]); | 
| 183 ); | |
| 184 checkParsedScript("Script with command names containing " + | 161 checkParsedScript("Script with command names containing " + | 
| 185 "whitespace (spaces, tabs, newlines, etc.), " + | 162 "whitespace (spaces, tabs, newlines, etc.), " + | 
| 186 "quotes, and semicolons", | 163 "quotes, and semicolons", | 
| 187 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 164 "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a r' 1 2", | 
| 188 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); | 165 [["fo' \t\n;o", "1", "2", "3"], ["b a r", "1", "2"]]); | 
| 189 checkParsedScript("Script containing Unicode composite characters", | 166 checkParsedScript("Script containing Unicode composite characters", | 
| 190 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 167 "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 
| 191 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 168 [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 
| 192 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 169 checkParsedScript("Script with no-op commands", "foo; ;;; ; ; bar 1", | 
| 193 [["foo"], ["bar", "1"]]); | 170 [["foo"], ["bar", "1"]]); | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 } | 213 } | 
| 237 } | 214 } | 
| 238 `; | 215 `; | 
| 239 | 216 | 
| 240 function verifyExecutable(script) | 217 function verifyExecutable(script) | 
| 241 { | 218 { | 
| 242 let actual = compileScript(script, libraries); | 219 let actual = compileScript(script, libraries); | 
| 243 let expected = template.replace("{{{script}}}", | 220 let expected = template.replace("{{{script}}}", | 
| 244 JSON.stringify(parseScript(script))); | 221 JSON.stringify(parseScript(script))); | 
| 245 | 222 | 
| 246 test.equal(expected, actual); | 223 test.equal(expected, actual); | 
| 
Sebastian Noack
2018/07/16 16:37:35
Instead of copying the boilerplate over to the tes
 
Manish Jethani
2018/07/17 19:57:15
Let me try something.
Yeah I agree that the curre
 
Manish Jethani
2018/07/17 20:10:26
Wait, this is what the next test does! It tests th
 | |
| 247 } | 224 } | 
| 248 | 225 | 
| 249 verifyExecutable("hello 'How are you?'"); | 226 verifyExecutable("hello 'How are you?'"); | 
| 250 | 227 | 
| 251 // Test script execution. | 228 // Test script execution. | 
| 252 new Function(compileScript("setFoo 123; assertFoo 123", libraries))(); | 229 new Function(compileScript("setFoo 123; assertFoo 123", libraries))(); | 
| 253 | 230 | 
| 254 // Override setFoo in a second library, without overriding assertFoo. A | 231 // Override setFoo in a second library, without overriding assertFoo. A | 
| 255 // couple of things to note here: (1) each library has its own variables; | 232 // couple of things to note here: (1) each library has its own variables; | 
| 256 // (2) script execution is stateless, i.e. the values are not retained | 233 // (2) script execution is stateless, i.e. the values are not retained | 
| 257 // between executions. In the example below, assertFoo does not find 456 but | 234 // between executions. In the example below, assertFoo does not find 456 but | 
| 258 // it doesn't find 123 either. It's the initial value 0. | 235 // it doesn't find 123 either. It's the initial value 0. | 
| 259 new Function( | 236 new Function( | 
| 260 compileScript("setFoo 456; assertFoo 0", [ | 237 compileScript("setFoo 456; assertFoo 0", [ | 
| 261 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | 238 ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | 
| 262 ]) | 239 ]) | 
| 263 )(); | 240 )(); | 
| 264 | 241 | 
| 265 test.done(); | 242 test.done(); | 
| 266 }; | 243 }; | 
| LEFT | RIGHT |