| 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 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 let tree = []; | 90 let tree = []; |
| 91 | 91 |
| 92 // Whether the next character should be treated as an escape sequence. | |
|
kzar
2018/07/12 10:30:37
I don't these new comments add much, and they are
Manish Jethani
2018/07/12 10:57:49
Well I'm glad you think so, because I don't like t
| |
| 93 let escape = false; | 92 let escape = false; |
| 94 | |
| 95 // Whether we are within a quoted sequence. | |
| 96 let withinQuotes = false; | 93 let withinQuotes = false; |
| 97 | 94 |
| 98 // The Unicode code point following a "\u" | |
| 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 // The terminating semicolon for the last command is optional; we add one | |
| 105 // here to make our loop consistent. | |
| 106 for (let character of script.trim() + ";") | 100 for (let character of script.trim() + ";") |
| 107 { | 101 { |
| 108 if (unicodeEscape != null) | 102 if (unicodeEscape != null) |
| 109 { | 103 { |
| 110 unicodeEscape += character; | 104 unicodeEscape += character; |
| 111 | 105 |
| 112 if (unicodeEscape.length == 4) | 106 if (unicodeEscape.length == 4) |
| 113 { | 107 { |
| 114 // Consider the Unicode escape only if it parses as a valid code point. | |
| 115 let codePoint = parseInt(unicodeEscape, 16); | 108 let codePoint = parseInt(unicodeEscape, 16); |
| 116 if (!isNaN(codePoint)) | 109 if (!isNaN(codePoint)) |
| 117 argument += String.fromCodePoint(codePoint); | 110 argument += String.fromCodePoint(codePoint); |
| 118 | 111 |
| 119 unicodeEscape = null; | 112 unicodeEscape = null; |
| 120 } | 113 } |
| 121 } | 114 } |
| 122 else if (escape) | 115 else if (escape) |
| 123 { | 116 { |
| 124 escape = false; | 117 escape = false; |
| 125 | 118 |
| 126 if (character == "u") | 119 if (character == "u") |
| 127 unicodeEscape = ""; | 120 unicodeEscape = ""; |
| 128 else | 121 else |
| 129 argument += singleCharacterEscapes.get(character) || character; | 122 argument += singleCharacterEscapes.get(character) || character; |
| 130 } | 123 } |
| 131 else if (character == "\\") | 124 else if (character == "\\") |
| 132 { | 125 { |
| 133 escape = true; | 126 escape = true; |
| 134 } | 127 } |
| 135 else if (character == "'") | 128 else if (character == "'") |
| 136 { | 129 { |
| 137 withinQuotes = !withinQuotes; | 130 withinQuotes = !withinQuotes; |
|
kzar
2018/07/12 10:30:37
Nice, I think this variable name is an improvement
| |
| 138 } | 131 } |
| 139 // Normally a semicolon or a whitespace character marks the end of an | |
| 140 // argument, but within quotes these characters are taken literally and | |
| 141 // included in the argument. | |
| 142 else if (withinQuotes || character != ";" && !/\s/u.test(character)) | 132 else if (withinQuotes || character != ";" && !/\s/u.test(character)) |
| 143 { | 133 { |
| 144 argument += character; | 134 argument += character; |
| 145 } | 135 } |
| 146 else | 136 else |
| 147 { | 137 { |
| 148 if (argument) | 138 if (argument) |
| 149 { | 139 { |
| 150 call.push(argument); | 140 call.push(argument); |
| 151 argument = ""; | 141 argument = ""; |
| 152 } | 142 } |
| 153 | 143 |
| 154 if (character == ";" && call.length > 0) | 144 if (character == ";" && call.length > 0) |
| 155 { | 145 { |
| 156 tree.push(call); | 146 tree.push(call); |
| 157 call = []; | 147 call = []; |
| 158 } | 148 } |
| 159 } | 149 } |
| 160 } | 150 } |
| 161 | 151 |
| 162 return tree; | 152 return tree; |
| 163 } | 153 } |
| 164 | 154 |
| 165 exports.parseScript = parseScript; | 155 exports.parseScript = parseScript; |
| LEFT | RIGHT |