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: Support Unicode escape sequences Created April 26, 2018, 4:16 p.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') | test/snippets.js » ('J')
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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 !ElemHide.getException(filter, domain)) 72 !ElemHide.getException(filter, domain))
73 { 73 {
74 result.push(filter); 74 result.push(filter);
75 } 75 }
76 } 76 }
77 return result; 77 return result;
78 } 78 }
79 }; 79 };
80 80
81 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 const singleCharacterEscapes = new Map([
91 ["n", "\n"], ["r", "\r"], ["t", "\t"]
92 ]);
93
94 let tree = [];
95
96 let escape = false;
97 let literal = false;
98
99 let unicodeEscape = null;
100
101 let call = [];
102 let argument = "";
103
104 for (let character of [...script.trim(), ";"])
105 {
106 if (unicodeEscape != null)
107 {
108 unicodeEscape += character;
109
110 if (unicodeEscape.length == 4)
111 {
112 let codePoint = parseInt(unicodeEscape, 16);
113 if (!isNaN(codePoint))
114 argument += String.fromCodePoint(codePoint);
115
116 unicodeEscape = null;
117 }
118 }
119 else if (escape)
120 {
121 escape = false;
122
123 if (character == "u")
124 unicodeEscape = "";
125 else
126 argument += singleCharacterEscapes.get(character) || character;
127 }
128 else if (character == "\\")
129 {
130 escape = true;
131 }
132 else if (character == "'")
133 {
134 literal = !literal;
135 }
136 else if (literal || character != ";" && !/\s/u.test(character))
137 {
138 argument += character;
139 }
140 else
141 {
142 if (argument)
143 {
144 call.push(argument);
145 argument = "";
146 }
147
148 if (character == ";" && call.length > 0)
149 {
150 tree.push(call);
151 call = [];
152 }
153 }
154 }
155
156 return tree;
157 }
158
159 exports.parseScript = parseScript;
OLDNEW
« no previous file with comments | « no previous file | test/snippets.js » ('j') | test/snippets.js » ('J')

Powered by Google App Engine
This is Rietveld