source: node_modules/entities/lib/escape.js

Last change on this file was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.escapeText = exports.escapeAttribute = exports.escapeUTF8 = exports.escape = exports.encodeXML = exports.getCodePoint = exports.xmlReplacer = void 0;
4exports.xmlReplacer = /["&'<>$\x80-\uFFFF]/g;
5var xmlCodeMap = new Map([
6 [34, "&quot;"],
7 [38, "&amp;"],
8 [39, "&apos;"],
9 [60, "&lt;"],
10 [62, "&gt;"],
11]);
12// For compatibility with node < 4, we wrap `codePointAt`
13exports.getCodePoint =
14// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15String.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. `&#xfc;`) will be used.
32 */
33function 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}
53exports.encodeXML = encodeXML;
54/**
55 * Encodes all non-ASCII characters, as well as characters not valid in XML
56 * documents using numeric hexadecimal reference (eg. `&#xfc;`).
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 */
63exports.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 */
74function 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 */
98exports.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 */
105exports.escapeAttribute = getEscaper(/["&\u00A0]/g, new Map([
106 [34, "&quot;"],
107 [38, "&amp;"],
108 [160, "&nbsp;"],
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 */
116exports.escapeText = getEscaper(/[&<>\u00A0]/g, new Map([
117 [38, "&amp;"],
118 [60, "&lt;"],
119 [62, "&gt;"],
120 [160, "&nbsp;"],
121]));
122//# sourceMappingURL=escape.js.map
Note: See TracBrowser for help on using the repository browser.