LEFT | RIGHT |
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 Loading... |
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 } | 78 } |
79 }; | 79 }; |
80 | 80 |
81 exports.Snippets = Snippets; | 81 exports.Snippets = Snippets; |
82 | 82 |
83 /** | 83 /** |
84 * Parses a script and returns a list of all its commands and their arguments | 84 * Parses a script and returns a list of all its commands and their arguments |
85 * @param {string} script | 85 * @param {string} script |
86 * @return {Array.<string[]>} | 86 * @return {Array.<string[]>} |
87 */ | 87 */ |
88 function parseScript(script) | 88 function parseScript(script) |
89 { | 89 { |
90 const singleCharacterEscapes = new Map([ | |
91 ["n", "\n"], ["r", "\r"], ["t", "\t"] | |
92 ]); | |
93 | |
94 let tree = []; | 90 let tree = []; |
95 | 91 |
96 let escape = false; | 92 let escape = false; |
97 let literal = false; | 93 let withinQuotes = false; |
98 | 94 |
99 let unicodeEscape = null; | 95 let unicodeEscape = null; |
100 | 96 |
101 let call = []; | 97 let call = []; |
102 let argument = ""; | 98 let argument = ""; |
103 | 99 |
104 for (let character of [...script.trim(), ";"]) | 100 for (let character of script.trim() + ";") |
105 { | 101 { |
106 if (unicodeEscape != null) | 102 if (unicodeEscape != null) |
107 { | 103 { |
108 unicodeEscape += character; | 104 unicodeEscape += character; |
109 | 105 |
110 if (unicodeEscape.length == 4) | 106 if (unicodeEscape.length == 4) |
111 { | 107 { |
112 let codePoint = parseInt(unicodeEscape, 16); | 108 let codePoint = parseInt(unicodeEscape, 16); |
113 if (!isNaN(codePoint)) | 109 if (!isNaN(codePoint)) |
114 argument += String.fromCodePoint(codePoint); | 110 argument += String.fromCodePoint(codePoint); |
115 | 111 |
116 unicodeEscape = null; | 112 unicodeEscape = null; |
117 } | 113 } |
118 } | 114 } |
119 else if (escape) | 115 else if (escape) |
120 { | 116 { |
121 escape = false; | 117 escape = false; |
122 | 118 |
123 if (character == "u") | 119 if (character == "u") |
124 unicodeEscape = ""; | 120 unicodeEscape = ""; |
125 else | 121 else |
126 argument += singleCharacterEscapes.get(character) || character; | 122 argument += singleCharacterEscapes.get(character) || character; |
127 } | 123 } |
128 else if (character == "\\") | 124 else if (character == "\\") |
129 { | 125 { |
130 escape = true; | 126 escape = true; |
131 } | 127 } |
132 else if (character == "'") | 128 else if (character == "'") |
133 { | 129 { |
134 literal = !literal; | 130 withinQuotes = !withinQuotes; |
135 } | 131 } |
136 else if (literal || character != ";" && !/\s/u.test(character)) | 132 else if (withinQuotes || character != ";" && !/\s/u.test(character)) |
137 { | 133 { |
138 argument += character; | 134 argument += character; |
139 } | 135 } |
140 else | 136 else |
141 { | 137 { |
142 if (argument) | 138 if (argument) |
143 { | 139 { |
144 call.push(argument); | 140 call.push(argument); |
145 argument = ""; | 141 argument = ""; |
146 } | 142 } |
147 | 143 |
148 if (character == ";" && call.length > 0) | 144 if (character == ";" && call.length > 0) |
149 { | 145 { |
150 tree.push(call); | 146 tree.push(call); |
151 call = []; | 147 call = []; |
152 } | 148 } |
153 } | 149 } |
154 } | 150 } |
155 | 151 |
156 return tree; | 152 return tree; |
157 } | 153 } |
158 | 154 |
159 exports.parseScript = parseScript; | 155 exports.parseScript = parseScript; |
LEFT | RIGHT |