| 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 /* eslint no-new-func: "off" */ | 
|  | 19 | 
| 18 "use strict"; | 20 "use strict"; | 
| 19 | 21 | 
| 20 const {createSandbox} = require("./_common"); | 22 const {createSandbox} = require("./_common"); | 
| 21 | 23 | 
| 22 let SnippetFilter = null; | 24 let SnippetFilter = null; | 
| 23 let Snippets = null; | 25 let Snippets = null; | 
| 24 let parseScript = null; | 26 let parseScript = null; | 
|  | 27 let compileScript = null; | 
| 25 let ElemHide = null; | 28 let ElemHide = null; | 
| 26 let Filter = null; | 29 let Filter = null; | 
| 27 | 30 | 
| 28 exports.setUp = function(callback) | 31 exports.setUp = function(callback) | 
| 29 { | 32 { | 
| 30   let sandboxedRequire = createSandbox(); | 33   let sandboxedRequire = createSandbox(); | 
| 31   ( | 34   ( | 
| 32     {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 35     {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 
| 33     {ElemHide} = sandboxedRequire("../lib/elemHide"), | 36     {ElemHide} = sandboxedRequire("../lib/elemHide"), | 
| 34     {Snippets, parseScript} = sandboxedRequire("../lib/snippets") | 37     {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets") | 
| 35   ); | 38   ); | 
| 36 | 39 | 
| 37   callback(); | 40   callback(); | 
| 38 }; | 41 }; | 
| 39 | 42 | 
| 40 exports.testDomainRestrictions = function(test) | 43 exports.testDomainRestrictions = function(test) | 
| 41 { | 44 { | 
| 42   function testSelectorMatches(description, filters, domain, expectedMatches) | 45   function testSelectorMatches(description, filters, domain, expectedMatches) | 
| 43   { | 46   { | 
| 44     for (let filter of filters) | 47     for (let filter of filters) | 
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 184                     "fo\\'\\ \\ \\\t\\\n\\;o 1 2 3; 'b a  r' 1 2", | 187                     "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"]]); | 188                     [["fo'  \t\n;o", "1", "2", "3"], ["b a  r", "1", "2"]]); | 
| 186   checkParsedScript("Script containing Unicode composite characters", | 189   checkParsedScript("Script containing Unicode composite characters", | 
| 187                     "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 190                     "f\ud83d\ude42\ud83d\ude42 b\ud83d\ude02r", | 
| 188                     [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 191                     [["f\ud83d\ude42\ud83d\ude42", "b\ud83d\ude02r"]]); | 
| 189   checkParsedScript("Script with no-op commands", "foo; ;;; ;  ; bar 1", | 192   checkParsedScript("Script with no-op commands", "foo; ;;; ;  ; bar 1", | 
| 190                     [["foo"], ["bar", "1"]]); | 193                     [["foo"], ["bar", "1"]]); | 
| 191 | 194 | 
| 192   test.done(); | 195   test.done(); | 
| 193 }; | 196 }; | 
|  | 197 | 
|  | 198 exports.testScriptCompilation = function(test) | 
|  | 199 { | 
|  | 200   let libraries = [ | 
|  | 201     ` | 
|  | 202       let foo = 0; | 
|  | 203 | 
|  | 204       exports.setFoo = function(value) | 
|  | 205       { | 
|  | 206         foo = value; | 
|  | 207       }; | 
|  | 208 | 
|  | 209       exports.assertFoo = function(expected) | 
|  | 210       { | 
|  | 211         if (foo != expected) | 
|  | 212           throw new Error("Value mismatch"); | 
|  | 213       }; | 
|  | 214     ` | 
|  | 215   ]; | 
|  | 216 | 
|  | 217   let template = ` | 
|  | 218     "use strict"; | 
|  | 219     { | 
|  | 220       const libraries = ${JSON.stringify(libraries)}; | 
|  | 221 | 
|  | 222       const script = {{{script}}}; | 
|  | 223 | 
|  | 224       let imports = Object.create(null); | 
|  | 225       for (let library of libraries) | 
|  | 226         new Function("exports", library)(imports); | 
|  | 227 | 
|  | 228       for (let [name, ...args] of script) | 
|  | 229       { | 
|  | 230         if (Object.prototype.hasOwnProperty.call(imports, name)) | 
|  | 231         { | 
|  | 232           let value = imports[name]; | 
|  | 233           if (typeof value == "function") | 
|  | 234             value(...args); | 
|  | 235         } | 
|  | 236       } | 
|  | 237     } | 
|  | 238   `; | 
|  | 239 | 
|  | 240   function verifyExecutable(script) | 
|  | 241   { | 
|  | 242     let actual = compileScript(script, libraries); | 
|  | 243     let expected = template.replace("{{{script}}}", | 
|  | 244                                     JSON.stringify(parseScript(script))); | 
|  | 245 | 
|  | 246     test.equal(expected, actual); | 
|  | 247   } | 
|  | 248 | 
|  | 249   verifyExecutable("hello 'How are you?'"); | 
|  | 250 | 
|  | 251   // Test script execution. | 
|  | 252   new Function(compileScript("setFoo 123; assertFoo 123", libraries))(); | 
|  | 253 | 
|  | 254   // Override setFoo in a second library, without overriding assertFoo. A | 
|  | 255   // 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 | 
|  | 257   // 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. | 
|  | 259   new Function( | 
|  | 260     compileScript("setFoo 456; assertFoo 0", [ | 
|  | 261       ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | 
|  | 262     ]) | 
|  | 263   )(); | 
|  | 264 | 
|  | 265   test.done(); | 
|  | 266 }; | 
| OLD | NEW | 
|---|