1 | import htmlTrie from "./generated/encode-html.js";
|
---|
2 | import { xmlReplacer, getCodePoint } from "./escape.js";
|
---|
3 | const 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. `ü`) will be used.
|
---|
14 | */
|
---|
15 | export 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. `ü`) will be used.
|
---|
25 | */
|
---|
26 | export function encodeNonAsciiHTML(data) {
|
---|
27 | return encodeHTMLTrieRe(xmlReplacer, data);
|
---|
28 | }
|
---|
29 | function 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 |
---|