source: node_modules/entities/lib/esm/encode.js@ 7deb3e2

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

Initial commit

  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[57e58a3]1import htmlTrie from "./generated/encode-html.js";
2import { xmlReplacer, getCodePoint } from "./escape.js";
3const htmlReplacer = /[\t\n!-,./:-@[-`\f{-}$\x80-\uFFFF]/g;
4/**
5 * Encodes all characters in the input using HTML entities. This includes
6 * characters that are valid ASCII characters in HTML documents, such as `#`.
7 *
8 * To get a more compact output, consider using the `encodeNonAsciiHTML`
9 * function, which will only encode characters that are not valid in HTML
10 * documents, as well as non-ASCII characters.
11 *
12 * If a character has no equivalent entity, a numeric hexadecimal reference
13 * (eg. `&#xfc;`) will be used.
14 */
15export function encodeHTML(data) {
16 return encodeHTMLTrieRe(htmlReplacer, data);
17}
18/**
19 * Encodes all non-ASCII characters, as well as characters not valid in HTML
20 * documents using HTML entities. This function will not encode characters that
21 * are valid in HTML documents, such as `#`.
22 *
23 * If a character has no equivalent entity, a numeric hexadecimal reference
24 * (eg. `&#xfc;`) will be used.
25 */
26export function encodeNonAsciiHTML(data) {
27 return encodeHTMLTrieRe(xmlReplacer, data);
28}
29function encodeHTMLTrieRe(regExp, str) {
30 let ret = "";
31 let lastIdx = 0;
32 let match;
33 while ((match = regExp.exec(str)) !== null) {
34 const i = match.index;
35 ret += str.substring(lastIdx, i);
36 const char = str.charCodeAt(i);
37 let next = htmlTrie.get(char);
38 if (typeof next === "object") {
39 // We are in a branch. Try to match the next char.
40 if (i + 1 < str.length) {
41 const nextChar = str.charCodeAt(i + 1);
42 const value = typeof next.n === "number"
43 ? next.n === nextChar
44 ? next.o
45 : undefined
46 : next.n.get(nextChar);
47 if (value !== undefined) {
48 ret += value;
49 lastIdx = regExp.lastIndex += 1;
50 continue;
51 }
52 }
53 next = next.v;
54 }
55 // We might have a tree node without a value; skip and use a numeric entity.
56 if (next !== undefined) {
57 ret += next;
58 lastIdx = i + 1;
59 }
60 else {
61 const cp = getCodePoint(str, i);
62 ret += `&#x${cp.toString(16)};`;
63 // Increase by 1 if we have a surrogate pair
64 lastIdx = regExp.lastIndex += Number(cp !== char);
65 }
66 }
67 return ret + str.substr(lastIdx);
68}
69//# sourceMappingURL=encode.js.map
Note: See TracBrowser for help on using the repository browser.