| Left: | ||
| Right: |
| 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 {Filter} = require("./filterClasses"); | 24 const {Filter} = require("./filterClasses"); |
| 25 | |
| 26 const singleCharacterEscapes = new Map([ | |
| 27 ["n", "\n"], ["r", "\r"], ["t", "\t"] | |
| 28 ]); | |
| 25 | 29 |
| 26 let filters = new Set(); | 30 let filters = new Set(); |
| 27 | 31 |
| 28 /** | 32 /** |
| 29 * Container for snippet filters | 33 * Container for snippet filters |
| 30 * @class | 34 * @class |
| 31 */ | 35 */ |
| 32 let Snippets = { | 36 let Snippets = { |
| 33 /** | 37 /** |
| 34 * Removes all known filters | 38 * Removes all known filters |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 } | 78 } |
| 75 }; | 79 }; |
| 76 | 80 |
| 77 exports.Snippets = Snippets; | 81 exports.Snippets = Snippets; |
| 78 | 82 |
| 79 /** | 83 /** |
| 80 * 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 |
| 81 * @param {string} script | 85 * @param {string} script |
| 82 * @return {Array.<string[]>} | 86 * @return {Array.<string[]>} |
| 83 */ | 87 */ |
| 84 function parseScript(script) | 88 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 { | 89 { |
| 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 = []; | 90 let tree = []; |
| 91 | 91 |
| 92 let escape = false; | 92 let escape = false; |
| 93 let literal = false; | 93 let withinQuotes = false; |
| 94 | 94 |
| 95 let unicodeEscape = null; | 95 let unicodeEscape = null; |
| 96 | 96 |
| 97 let call = []; | 97 let call = []; |
| 98 let argument = ""; | 98 let argument = ""; |
| 99 | 99 |
| 100 for (let character of [...script.trim(), ";"]) | 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 { | 101 { |
| 102 if (unicodeEscape != null) | 102 if (unicodeEscape != null) |
| 103 { | 103 { |
| 104 unicodeEscape += character; | 104 unicodeEscape += character; |
| 105 | 105 |
| 106 if (unicodeEscape.length == 4) | 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 { | 107 { |
| 108 let codePoint = parseInt(unicodeEscape, 16); | 108 let codePoint = parseInt(unicodeEscape, 16); |
| 109 if (!isNaN(codePoint)) | 109 if (!isNaN(codePoint)) |
| 110 argument += String.fromCodePoint(codePoint); | 110 argument += String.fromCodePoint(codePoint); |
| 111 | 111 |
| 112 unicodeEscape = null; | 112 unicodeEscape = null; |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 else if (escape) | 115 else if (escape) |
| 116 { | 116 { |
| 117 escape = false; | 117 escape = false; |
| 118 | 118 |
| 119 if (character == "u") | 119 if (character == "u") |
| 120 unicodeEscape = ""; | 120 unicodeEscape = ""; |
| 121 else | 121 else |
| 122 argument += singleCharacterEscapes.get(character) || character; | 122 argument += singleCharacterEscapes.get(character) || character; |
| 123 } | 123 } |
| 124 else if (character == "\\") | 124 else if (character == "\\") |
| 125 { | 125 { |
| 126 escape = true; | 126 escape = true; |
| 127 } | 127 } |
| 128 else if (character == "'") | 128 else if (character == "'") |
| 129 { | 129 { |
| 130 literal = !literal; | 130 withinQuotes = !withinQuotes; |
| 131 } | 131 } |
| 132 else if (literal || character != ";" && !/\s/u.test(character)) | 132 else if (withinQuotes || 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 { | 133 { |
| 134 argument += character; | 134 argument += character; |
| 135 } | 135 } |
| 136 else | 136 else |
| 137 { | 137 { |
| 138 if (argument) | 138 if (argument) |
| 139 { | 139 { |
| 140 call.push(argument); | 140 call.push(argument); |
| 141 argument = ""; | 141 argument = ""; |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (character == ";" && call.length > 0) | 144 if (character == ";" && call.length > 0) |
| 145 { | 145 { |
| 146 tree.push(call); | 146 tree.push(call); |
| 147 call = []; | 147 call = []; |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 return tree; | 152 return tree; |
| 153 } | 153 } |
| 154 | 154 |
| 155 exports.parseScript = parseScript; | 155 exports.parseScript = parseScript; |
| LEFT | RIGHT |