WXL
3 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { getCodePoint, XML_BITSET_VALUE } from "./escape.js";
import { htmlTrie } from "./generated/encode-html.js";
/**
 * We store the characters to consider as a compact bitset for fast lookups.
 */
const HTML_BITSET = /* #__PURE__ */ new Uint32Array([
    5632, // Bits for 09,0A,0C
    4227923966, // 32..63 -> 21-2D (minus space), 2E,2F,3A-3F
    4160749569, // 64..95 -> 40, 5B-5F
    939524097, // 96..127-> 60, 7B-7D
]);
const XML_BITSET = /* #__PURE__ */ new Uint32Array([0, XML_BITSET_VALUE, 0, 0]);
/**
 * Encodes all characters in the input using HTML entities. This includes
 * characters that are valid ASCII characters in HTML documents, such as `#`.
 *
 * To get a more compact output, consider using the `encodeNonAsciiHTML`
 * function, which will only encode characters that are not valid in HTML
 * documents, as well as non-ASCII characters.
 *
 * If a character has no equivalent entity, a numeric hexadecimal reference
 * (eg. `ü`) will be used.
 */
export function encodeHTML(input) {
    return encodeHTMLTrieRe(HTML_BITSET, input);
}
/**
 * Encodes all non-ASCII characters, as well as characters not valid in HTML
 * documents using HTML entities. This function will not encode characters that
 * are valid in HTML documents, such as `#`.
 *
 * If a character has no equivalent entity, a numeric hexadecimal reference
 * (eg. `ü`) will be used.
 */
export function encodeNonAsciiHTML(input) {
    return encodeHTMLTrieRe(XML_BITSET, input);
}
function encodeHTMLTrieRe(bitset, input) {
    let out;
    let last = 0; // Start of the next untouched slice.
    const { length } = input;
    for (let index = 0; index < length; index++) {
        const char = input.charCodeAt(index);
        // Skip ASCII characters that don't need encoding
        if (char < 0x80 && !((bitset[char >>> 5] >>> char) & 1)) {
            continue;
        }
        if (out === undefined)
            out = input.substring(0, index);
        else if (last !== index)
            out += input.substring(last, index);
        let node = htmlTrie.get(char);
        if (typeof node === "object") {
            if (index + 1 < length) {
                const nextChar = input.charCodeAt(index + 1);
                const value = typeof node.next === "number"
                    ? node.next === nextChar
                        ? node.nextValue
                        : undefined
                    : node.next.get(nextChar);
                if (value !== undefined) {
                    out += value;
                    index++;
                    last = index + 1;
                    continue;
                }
            }
            node = node.value;
        }
        if (node === undefined) {
            const cp = getCodePoint(input, index);
            out += `&#x${cp.toString(16)};`;
            if (cp !== char)
                index++;
            last = index + 1;
        }
        else {
            out += node;
            last = index + 1;
        }
    }
    if (out === undefined)
        return input;
    if (last < length)
        out += input.substr(last);
    return out;
}
//# sourceMappingURL=encode.js.map