|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +export function yamlEscapeKeyIfNeeded(str: string): string { |
| 18 | + if (!yamlStringNeedsQuotes(str)) |
| 19 | + return str; |
| 20 | + return `'` + str.replace(/'/g, `''`) + `'`; |
| 21 | +} |
| 22 | + |
| 23 | +export function yamlEscapeValueIfNeeded(str: string): string { |
| 24 | + if (!yamlStringNeedsQuotes(str)) |
| 25 | + return str; |
| 26 | + return '"' + str.replace(/[\\"\x00-\x1f\x7f-\x9f]/g, c => { |
| 27 | + switch (c) { |
| 28 | + case '\\': |
| 29 | + return '\\\\'; |
| 30 | + case '"': |
| 31 | + return '\\"'; |
| 32 | + case '\b': |
| 33 | + return '\\b'; |
| 34 | + case '\f': |
| 35 | + return '\\f'; |
| 36 | + case '\n': |
| 37 | + return '\\n'; |
| 38 | + case '\r': |
| 39 | + return '\\r'; |
| 40 | + case '\t': |
| 41 | + return '\\t'; |
| 42 | + default: |
| 43 | + const code = c.charCodeAt(0); |
| 44 | + return '\\x' + code.toString(16).padStart(2, '0'); |
| 45 | + } |
| 46 | + }) + '"'; |
| 47 | +} |
| 48 | + |
| 49 | +function yamlStringNeedsQuotes(str: string): boolean { |
| 50 | + if (str.length === 0) |
| 51 | + return true; |
| 52 | + |
| 53 | + // Strings with leading or trailing whitespace need quotes |
| 54 | + if (/^\s|\s$/.test(str)) |
| 55 | + return true; |
| 56 | + |
| 57 | + // Strings containing control characters need quotes |
| 58 | + if (/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]/.test(str)) |
| 59 | + return true; |
| 60 | + |
| 61 | + // Strings starting with '-' need quotes |
| 62 | + if (/^-/.test(str)) |
| 63 | + return true; |
| 64 | + |
| 65 | + // Strings containing ':' or '\n' followed by a space or at the end need quotes |
| 66 | + if (/[\n:](\s|$)/.test(str)) |
| 67 | + return true; |
| 68 | + |
| 69 | + // Strings containing '#' preceded by a space need quotes (comment indicator) |
| 70 | + if (/\s#/.test(str)) |
| 71 | + return true; |
| 72 | + |
| 73 | + // Strings that contain line breaks need quotes |
| 74 | + if (/[\n\r]/.test(str)) |
| 75 | + return true; |
| 76 | + |
| 77 | + // Strings starting with indicator characters or quotes need quotes |
| 78 | + if (/^[&*\],?!>|@"'#%]/.test(str)) |
| 79 | + return true; |
| 80 | + |
| 81 | + // Strings containing special characters that could cause ambiguity |
| 82 | + if (/[{}`]/.test(str)) |
| 83 | + return true; |
| 84 | + |
| 85 | + // YAML array starts with [ |
| 86 | + if (/^\[/.test(str)) |
| 87 | + return true; |
| 88 | + |
| 89 | + // Non-string types recognized by YAML |
| 90 | + if (!isNaN(Number(str)) || ['y', 'n', 'yes', 'no', 'true', 'false', 'on', 'off', 'null'].includes(str.toLowerCase())) |
| 91 | + return true; |
| 92 | + |
| 93 | + return false; |
| 94 | +} |
0 commit comments