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 single character escape sequences Created April 26, 2018, 3:34 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') | 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 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 call = [];
100 let argument = "";
101
102 for (let character of [...script.trim(), ";"])
103 {
104 if (escape)
105 {
106 escape = false;
107
108 argument += singleCharacterEscapes.get(character) || character;
109 }
110 else if (character == "\\")
111 {
112 escape = true;
113 }
114 else if (character == "'")
115 {
116 literal = !literal;
117 }
118 else if (literal || character != ";" && !/\s/u.test(character))
119 {
120 argument += character;
121 }
122 else
123 {
124 if (argument)
125 {
126 call.push(argument);
127 argument = "";
128 }
129
130 if (character == ";" && call.length > 0)
131 {
132 tree.push(call);
133 call = [];
134 }
135 }
136 }
137
138 return tree;
139 }
140
141 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