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: Rebase Created May 23, 2018, 4:12 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
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 { 68 {
69 let filter = Filter.fromText(text); 69 let filter = Filter.fromText(text);
70 if (filter.isActiveOnDomain(domain)) 70 if (filter.isActiveOnDomain(domain))
71 result.push(filter.script); 71 result.push(filter.script);
72 } 72 }
73 return result; 73 return result;
74 } 74 }
75 }; 75 };
76 76
77 exports.Snippets = Snippets; 77 exports.Snippets = Snippets;
78
79 /**
80 * Parses a script and returns a list of all its commands and their arguments
81 * @param {string} script
82 * @return {Array.<string[]>}
83 */
84 function parseScript(script)
kzar 2018/07/10 13:55:20 I found this terminology confusing, "script" is th
Manish Jethani 2018/07/11 19:04:41 The argument to this function is the SnippetFilter
kzar 2018/07/12 10:30:36 Fair enough.
85 {
86 const singleCharacterEscapes = new Map([
kzar 2018/07/10 13:55:20 Might be better to declare this once at the top of
Manish Jethani 2018/07/11 19:04:41 Done.
87 ["n", "\n"], ["r", "\r"], ["t", "\t"]
88 ]);
89
90 let tree = [];
91
92 let escape = false;
93 let literal = false;
94
95 let unicodeEscape = null;
96
97 let call = [];
98 let argument = "";
99
100 for (let character of [...script.trim(), ";"])
kzar 2018/07/10 13:55:20 Why not just do `script = script.trim() + ";";` ab
Manish Jethani 2018/07/11 19:04:41 Done.
101 {
102 if (unicodeEscape != null)
103 {
104 unicodeEscape += character;
105
106 if (unicodeEscape.length == 4)
kzar 2018/07/10 13:55:20 What if unicodeEscape never reaches this length? I
Manish Jethani 2018/07/11 19:04:41 Yes, they would be lost. If it's not at the end o
kzar 2018/07/12 10:30:36 Acknowledged.
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)
116 {
117 escape = false;
118
119 if (character == "u")
120 unicodeEscape = "";
121 else
122 argument += singleCharacterEscapes.get(character) || character;
123 }
124 else if (character == "\\")
125 {
126 escape = true;
127 }
128 else if (character == "'")
129 {
130 literal = !literal;
131 }
132 else if (literal || character != ";" && !/\s/u.test(character))
kzar 2018/07/10 13:55:20 So the literal flag has no effect on escaped chara
Manish Jethani 2018/07/11 19:04:41 No, mainly because we need a way to escape the sin
kzar 2018/07/12 10:30:36 Acknowledged.
133 {
134 argument += character;
135 }
136 else
137 {
138 if (argument)
139 {
140 call.push(argument);
141 argument = "";
142 }
143
144 if (character == ";" && call.length > 0)
145 {
146 tree.push(call);
147 call = [];
148 }
149 }
150 }
151
152 return tree;
153 }
154
155 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