* Parse the provided css text and inside strings (meaning, inside pairs of unescaped single or * double quotes) replace specific characters with their respective placeholders as indicated * by the `ESCAPE_IN_STRING_MAP` map. * * For example convert the text * `animation: "my-anim:at\"ion" 1s;`
(input: string)
| 1178 | * @returns the css text with specific characters in strings replaced by placeholders. |
| 1179 | **/ |
| 1180 | function escapeInStrings(input: string): string { |
| 1181 | let result = input; |
| 1182 | let currentQuoteChar: string | null = null; |
| 1183 | for (let i = 0; i < result.length; i++) { |
| 1184 | const char = result[i]; |
| 1185 | if (char === '\\') { |
| 1186 | i++; |
| 1187 | } else { |
| 1188 | if (currentQuoteChar !== null) { |
| 1189 | // index i is inside a quoted sub-string |
| 1190 | if (char === currentQuoteChar) { |
| 1191 | currentQuoteChar = null; |
| 1192 | } else { |
| 1193 | const placeholder: string | undefined = ESCAPE_IN_STRING_MAP[char]; |
| 1194 | if (placeholder) { |
| 1195 | result = `${result.substr(0, i)}${placeholder}${result.substr(i + 1)}`; |
| 1196 | i += placeholder.length - 1; |
| 1197 | } |
| 1198 | } |
| 1199 | } else if (char === "'" || char === '"') { |
| 1200 | currentQuoteChar = char; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | return result; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * Replace in a string all occurrences of keys in the `ESCAPE_IN_STRING_MAP` map with their |