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

Side by Side Diff: lib/snippets.js

Issue 29761597: Issue 6538, 6781 - Implement script parsing for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add some comments Created July 12, 2018, 8:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {Filter} = require("./filterClasses"); 24 const {Filter} = require("./filterClasses");
25 25
26 const singleCharacterEscapes = new Map([
27 ["n", "\n"], ["r", "\r"], ["t", "\t"]
28 ]);
29
26 let filters = new Set(); 30 let filters = new Set();
27 31
28 /** 32 /**
29 * Container for snippet filters 33 * Container for snippet filters
30 * @class 34 * @class
31 */ 35 */
32 let Snippets = { 36 let Snippets = {
33 /** 37 /**
34 * Removes all known filters 38 * Removes all known filters
35 */ 39 */
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 { 72 {
69 let filter = Filter.fromText(text); 73 let filter = Filter.fromText(text);
70 if (filter.isActiveOnDomain(domain)) 74 if (filter.isActiveOnDomain(domain))
71 result.push(filter.script); 75 result.push(filter.script);
72 } 76 }
73 return result; 77 return result;
74 } 78 }
75 }; 79 };
76 80
77 exports.Snippets = Snippets; 81 exports.Snippets = Snippets;
82
83 /**
84 * Parses a script and returns a list of all its commands and their arguments
85 * @param {string} script
86 * @return {Array.<string[]>}
87 */
88 function parseScript(script)
89 {
90 let tree = [];
91
92 // Whether the next character should be treated as an escape sequence.
kzar 2018/07/12 10:30:37 I don't these new comments add much, and they are
Manish Jethani 2018/07/12 10:57:49 Well I'm glad you think so, because I don't like t
93 let escape = false;
94
95 // Whether we are within a quoted sequence.
96 let withinQuotes = false;
97
98 // The Unicode code point following a "\u"
99 let unicodeEscape = null;
100
101 let call = [];
102 let argument = "";
103
104 // The terminating semicolon for the last command is optional; we add one
105 // here to make our loop consistent.
106 for (let character of script.trim() + ";")
107 {
108 if (unicodeEscape != null)
109 {
110 unicodeEscape += character;
111
112 if (unicodeEscape.length == 4)
113 {
114 // Consider the Unicode escape only if it parses as a valid code point.
115 let codePoint = parseInt(unicodeEscape, 16);
116 if (!isNaN(codePoint))
117 argument += String.fromCodePoint(codePoint);
118
119 unicodeEscape = null;
120 }
121 }
122 else if (escape)
123 {
124 escape = false;
125
126 if (character == "u")
127 unicodeEscape = "";
128 else
129 argument += singleCharacterEscapes.get(character) || character;
130 }
131 else if (character == "\\")
132 {
133 escape = true;
134 }
135 else if (character == "'")
136 {
137 withinQuotes = !withinQuotes;
kzar 2018/07/12 10:30:37 Nice, I think this variable name is an improvement
138 }
139 // Normally a semicolon or a whitespace character marks the end of an
140 // argument, but within quotes these characters are taken literally and
141 // included in the argument.
142 else if (withinQuotes || character != ";" && !/\s/u.test(character))
143 {
144 argument += character;
145 }
146 else
147 {
148 if (argument)
149 {
150 call.push(argument);
151 argument = "";
152 }
153
154 if (character == ";" && call.length > 0)
155 {
156 tree.push(call);
157 call = [];
158 }
159 }
160 }
161
162 return tree;
163 }
164
165 exports.parseScript = parseScript;
OLDNEW
« no previous file with comments | « no previous file | test/snippets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld