Nikolay Vitkov | 1037198 | 2025-02-03 17:08:07 | [diff] [blame] | 1 | "use strict"; |
| 2 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | exports.escapeText = exports.escapeAttribute = exports.escapeUTF8 = exports.escape = exports.encodeXML = exports.getCodePoint = exports.xmlReplacer = void 0; |
| 4 | exports.xmlReplacer = /["&'<>$\x80-\uFFFF]/g; |
| 5 | var xmlCodeMap = new Map([ |
| 6 | [34, """], |
| 7 | [38, "&"], |
| 8 | [39, "'"], |
| 9 | [60, "<"], |
| 10 | [62, ">"], |
| 11 | ]); |
| 12 | // For compatibility with node < 4, we wrap `codePointAt` |
| 13 | exports.getCodePoint = |
| 14 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 15 | String.prototype.codePointAt != null |
| 16 | ? function (str, index) { return str.codePointAt(index); } |
| 17 | : // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae |
| 18 | function (c, index) { |
| 19 | return (c.charCodeAt(index) & 0xfc00) === 0xd800 |
| 20 | ? (c.charCodeAt(index) - 0xd800) * 0x400 + |
| 21 | c.charCodeAt(index + 1) - |
| 22 | 0xdc00 + |
| 23 | 0x10000 |
| 24 | : c.charCodeAt(index); |
| 25 | }; |
| 26 | /** |
| 27 | * Encodes all non-ASCII characters, as well as characters not valid in XML |
| 28 | * documents using XML entities. |
| 29 | * |
| 30 | * If a character has no equivalent entity, a |
| 31 | * numeric hexadecimal reference (eg. `ü`) will be used. |
| 32 | */ |
| 33 | function encodeXML(str) { |
| 34 | var ret = ""; |
| 35 | var lastIdx = 0; |
| 36 | var match; |
| 37 | while ((match = exports.xmlReplacer.exec(str)) !== null) { |
| 38 | var i = match.index; |
| 39 | var char = str.charCodeAt(i); |
| 40 | var next = xmlCodeMap.get(char); |
| 41 | if (next !== undefined) { |
| 42 | ret += str.substring(lastIdx, i) + next; |
| 43 | lastIdx = i + 1; |
| 44 | } |
| 45 | else { |
| 46 | ret += "".concat(str.substring(lastIdx, i), "&#x").concat((0, exports.getCodePoint)(str, i).toString(16), ";"); |
| 47 | // Increase by 1 if we have a surrogate pair |
| 48 | lastIdx = exports.xmlReplacer.lastIndex += Number((char & 0xfc00) === 0xd800); |
| 49 | } |
| 50 | } |
| 51 | return ret + str.substr(lastIdx); |
| 52 | } |
| 53 | exports.encodeXML = encodeXML; |
| 54 | /** |
| 55 | * Encodes all non-ASCII characters, as well as characters not valid in XML |
| 56 | * documents using numeric hexadecimal reference (eg. `ü`). |
| 57 | * |
| 58 | * Have a look at `escapeUTF8` if you want a more concise output at the expense |
| 59 | * of reduced transportability. |
| 60 | * |
| 61 | * @param data String to escape. |
| 62 | */ |
| 63 | exports.escape = encodeXML; |
| 64 | /** |
| 65 | * Creates a function that escapes all characters matched by the given regular |
| 66 | * expression using the given map of characters to escape to their entities. |
| 67 | * |
| 68 | * @param regex Regular expression to match characters to escape. |
| 69 | * @param map Map of characters to escape to their entities. |
| 70 | * |
| 71 | * @returns Function that escapes all characters matched by the given regular |
| 72 | * expression using the given map of characters to escape to their entities. |
| 73 | */ |
| 74 | function getEscaper(regex, map) { |
| 75 | return function escape(data) { |
| 76 | var match; |
| 77 | var lastIdx = 0; |
| 78 | var result = ""; |
| 79 | while ((match = regex.exec(data))) { |
| 80 | if (lastIdx !== match.index) { |
| 81 | result += data.substring(lastIdx, match.index); |
| 82 | } |
| 83 | // We know that this character will be in the map. |
| 84 | result += map.get(match[0].charCodeAt(0)); |
| 85 | // Every match will be of length 1 |
| 86 | lastIdx = match.index + 1; |
| 87 | } |
| 88 | return result + data.substring(lastIdx); |
| 89 | }; |
| 90 | } |
| 91 | /** |
| 92 | * Encodes all characters not valid in XML documents using XML entities. |
| 93 | * |
| 94 | * Note that the output will be character-set dependent. |
| 95 | * |
| 96 | * @param data String to escape. |
| 97 | */ |
| 98 | exports.escapeUTF8 = getEscaper(/[&<>'"]/g, xmlCodeMap); |
| 99 | /** |
| 100 | * Encodes all characters that have to be escaped in HTML attributes, |
| 101 | * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. |
| 102 | * |
| 103 | * @param data String to escape. |
| 104 | */ |
| 105 | exports.escapeAttribute = getEscaper(/["&\u00A0]/g, new Map([ |
| 106 | [34, """], |
| 107 | [38, "&"], |
| 108 | [160, " "], |
| 109 | ])); |
| 110 | /** |
| 111 | * Encodes all characters that have to be escaped in HTML text, |
| 112 | * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. |
| 113 | * |
| 114 | * @param data String to escape. |
| 115 | */ |
| 116 | exports.escapeText = getEscaper(/[&<>\u00A0]/g, new Map([ |
| 117 | [38, "&"], |
| 118 | [60, "<"], |
| 119 | [62, ">"], |
| 120 | [160, " "], |
| 121 | ])); |
| 122 | //# sourceMappingURL=escape.js.map |