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

Delta Between Two Patch Sets: lib/snippets.js

Issue 29761597: Issue 6538, 6781 - Implement script parsing for snippets (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created April 25, 2018, 5:25 p.m.
Right Patch Set: Revert Patch Set 8 Created July 12, 2018, 10:56 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') | no next file with change/comment »
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");
25
26 const singleCharacterEscapes = new Map([
27 ["n", "\n"], ["r", "\r"], ["t", "\t"]
28 ]);
26 29
27 let filters = new Set(); 30 let filters = new Set();
28 31
29 /** 32 /**
30 * Container for snippet filters 33 * Container for snippet filters
31 * @class 34 * @class
32 */ 35 */
33 let Snippets = { 36 let Snippets = {
34 /** 37 /**
35 * Removes all known filters 38 * Removes all known filters
(...skipping 17 matching lines...) Expand all
53 * @param {SnippetFilter} filter 56 * @param {SnippetFilter} filter
54 */ 57 */
55 remove(filter) 58 remove(filter)
56 { 59 {
57 filters.delete(filter.text); 60 filters.delete(filter.text);
58 }, 61 },
59 62
60 /** 63 /**
61 * Returns a list of all scripts active on a particular domain 64 * Returns a list of all scripts active on a particular domain
62 * @param {string} domain 65 * @param {string} domain
63 * @return {SnippetFilter[]} 66 * @return {string[]}
64 */ 67 */
65 getScriptsForDomain(domain) 68 getScriptsForDomain(domain)
66 { 69 {
67 let result = []; 70 let result = [];
68 for (let text of filters) 71 for (let text of filters)
69 { 72 {
70 let filter = Filter.fromText(text); 73 let filter = Filter.fromText(text);
71 if (filter.isActiveOnDomain(domain) && 74 if (filter.isActiveOnDomain(domain))
72 !ElemHide.getException(filter, domain)) 75 result.push(filter.script);
73 {
74 result.push(filter);
75 }
76 } 76 }
77 return result; 77 return result;
78 },
79
80 /**
81 * Parses a script and returns a list of all its commands and their arguments
82 * @param {string} script
83 * @return {Array.<string[]>}
Manish Jethani 2018/04/26 13:25:17 I had to use Array.<string[]> instead of string[][
84 */
85 parseScript(script)
86 {
87 let tree = [];
88
89 let escape = false;
90 let literal = false;
91
92 let call = [];
93 let argument = "";
94
95 for (let character of [...script.trim(), ";"])
Manish Jethani 2018/04/26 13:25:17 The parsing here is as per the specification in Tr
96 {
97 if (escape)
98 {
99 escape = false;
100
101 argument += character;
102 }
103 else if (character == "\\")
104 {
105 escape = true;
106 }
107 else if (character == "'")
108 {
109 literal = !literal;
110 }
111 else if (literal || character != ";" && !/\s/u.test(character))
112 {
113 argument += character;
114 }
115 else
116 {
117 if (argument)
118 {
119 call.push(argument);
120 argument = "";
121 }
122
123 if (character == ";" && call.length > 0)
124 {
125 tree.push(call);
126 call = [];
127 }
128 }
129 }
130
131 return tree;
132 } 78 }
133 }; 79 };
134 80
135 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 let escape = false;
93 let withinQuotes = false;
94
95 let unicodeEscape = null;
96
97 let call = [];
98 let argument = "";
99
100 for (let character of script.trim() + ";")
101 {
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)
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 withinQuotes = !withinQuotes;
131 }
132 else if (withinQuotes || character != ";" && !/\s/u.test(character))
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;
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