Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/snippets.js

Issue 29761612: Issue 6538, 6781 - Implement script compilation for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Move compileScript to top level Created April 26, 2018, 2:34 p.m.
Right Patch Set: Rebase Created May 23, 2018, 4:18 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 /** 20 /**
21 * @fileOverview Snippets implementation. 21 * @fileOverview Snippets implementation.
22 */ 22 */
23 23
24 const {ElemHide} = require("./elemHide");
25 const {Filter} = require("./filterClasses"); 24 const {Filter} = require("./filterClasses");
26 25
27 let filters = new Set(); 26 let filters = new Set();
28 27
29 /** 28 /**
30 * Container for snippet filters 29 * Container for snippet filters
31 * @class 30 * @class
32 */ 31 */
33 let Snippets = { 32 let Snippets = {
34 /** 33 /**
(...skipping 18 matching lines...) Expand all
53 * @param {SnippetFilter} filter 52 * @param {SnippetFilter} filter
54 */ 53 */
55 remove(filter) 54 remove(filter)
56 { 55 {
57 filters.delete(filter.text); 56 filters.delete(filter.text);
58 }, 57 },
59 58
60 /** 59 /**
61 * Returns a list of all scripts active on a particular domain 60 * Returns a list of all scripts active on a particular domain
62 * @param {string} domain 61 * @param {string} domain
63 * @return {SnippetFilter[]} 62 * @return {string[]}
64 */ 63 */
65 getScriptsForDomain(domain) 64 getScriptsForDomain(domain)
66 { 65 {
67 let result = []; 66 let result = [];
68 for (let text of filters) 67 for (let text of filters)
69 { 68 {
70 let filter = Filter.fromText(text); 69 let filter = Filter.fromText(text);
71 if (filter.isActiveOnDomain(domain) && 70 if (filter.isActiveOnDomain(domain))
72 !ElemHide.getException(filter, domain)) 71 result.push(filter.script);
73 {
74 result.push(filter);
75 }
76 } 72 }
77 return result; 73 return result;
78 } 74 }
79 }; 75 };
80 76
81 exports.Snippets = Snippets; 77 exports.Snippets = Snippets;
82 78
83 /** 79 /**
84 * Parses a script and returns a list of all its commands and their arguments 80 * Parses a script and returns a list of all its commands and their arguments
85 * @param {string} script 81 * @param {string} script
86 * @return {Array.<string[]>} 82 * @return {Array.<string[]>}
87 */ 83 */
88 function parseScript(script) 84 function parseScript(script)
89 { 85 {
86 const singleCharacterEscapes = new Map([
87 ["n", "\n"], ["r", "\r"], ["t", "\t"]
88 ]);
89
90 let tree = []; 90 let tree = [];
91 91
92 let escape = false; 92 let escape = false;
93 let literal = false; 93 let literal = false;
94
95 let unicodeEscape = null;
94 96
95 let call = []; 97 let call = [];
96 let argument = ""; 98 let argument = "";
97 99
98 for (let character of [...script.trim(), ";"]) 100 for (let character of [...script.trim(), ";"])
99 { 101 {
100 if (escape) 102 if (unicodeEscape != null)
103 {
104 unicodeEscape += character;
105
106 if (unicodeEscape.length == 4)
107 {
108 let codePoint = parseInt(unicodeEscape, 16);
109 if (!isNaN(codePoint))
110 argument += String.fromCodePoint(codePoint);
111
112 unicodeEscape = null;
113 }
114 }
115 else if (escape)
101 { 116 {
102 escape = false; 117 escape = false;
103 118
104 argument += character; 119 if (character == "u")
120 unicodeEscape = "";
121 else
122 argument += singleCharacterEscapes.get(character) || character;
105 } 123 }
106 else if (character == "\\") 124 else if (character == "\\")
107 { 125 {
108 escape = true; 126 escape = true;
109 } 127 }
110 else if (character == "'") 128 else if (character == "'")
111 { 129 {
112 literal = !literal; 130 literal = !literal;
113 } 131 }
114 else if (literal || character != ";" && !/\s/u.test(character)) 132 else if (literal || character != ";" && !/\s/u.test(character))
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 { 164 {
147 return ` 165 return `
148 "use strict"; 166 "use strict";
149 { 167 {
150 const libraries = ${JSON.stringify(libraries)}; 168 const libraries = ${JSON.stringify(libraries)};
151 169
152 const script = ${JSON.stringify(parseScript(script))}; 170 const script = ${JSON.stringify(parseScript(script))};
153 171
154 let imports = Object.create(null); 172 let imports = Object.create(null);
155 for (let library of libraries) 173 for (let library of libraries)
156 new Function("exports", library)(imports); 174 new Function("exports", library)(imports);
kzar 2018/07/10 14:35:15 I wonder why we take include all libraries, instea
kzar 2018/07/10 14:35:15 If I got it right, `compileScript` has a string re
Manish Jethani 2018/07/12 09:48:31 The idea is that there will be a local library, wh
Manish Jethani 2018/07/12 09:48:32 libraries is an array of strings. The only way to
kzar 2018/07/12 10:21:43 I still don't see the point of stringifying it, ju
Manish Jethani 2018/07/12 11:05:29 The JSON.stringify is what converts your first sni
kzar 2018/07/12 11:19:55 It's really not, but nevermind.
Manish Jethani 2018/07/12 11:35:16 OK, perhaps I am misunderstanding. If you can rewr
Sebastian Noack 2018/07/16 19:14:53 So we are generating code that generates code just
Manish Jethani 2018/07/17 19:57:15 The library looks like this: https://github.com/a
157 175
158 for (let [name, ...args] of script) 176 for (let [name, ...args] of script)
159 { 177 {
160 if (Object.prototype.hasOwnProperty.call(imports, name)) 178 if (Object.prototype.hasOwnProperty.call(imports, name))
kzar 2018/07/10 14:35:15 Why not `name in imports`, you created the `import
Manish Jethani 2018/07/12 09:48:31 It's just safer this way. Right now it's a null pr
kzar 2018/07/12 10:21:43 Alright, I disagree but won't insist.
Manish Jethani 2018/07/12 11:05:29 Acknowledged.
161 { 179 {
162 let value = imports[name]; 180 let value = imports[name];
163 if (typeof value == "function") 181 if (typeof value == "function")
kzar 2018/07/10 14:35:15 Could this check ever fail?
Manish Jethani 2018/07/12 09:48:31 Yes, a library could export a "constant" for whate
kzar 2018/07/12 10:21:43 Acknowledged.
164 value(...args); 182 value(...args);
165 } 183 }
166 } 184 }
167 } 185 }
168 `; 186 `;
169 } 187 }
170 188
171 exports.compileScript = compileScript; 189 exports.compileScript = compileScript;
LEFTRIGHT
« no previous file | test/snippets.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld