encode-trie.js 3.1 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTrie = exports.encodeHTMLTrieRe = exports.getCodePoint = void 0;
var entities_json_1 = __importDefault(require("./maps/entities.json"));
function isHighSurrugate(c) {
    return (c & 64512 /* Mask */) === 55296 /* High */;
}
// For compatibility with node < 4, we wrap `codePointAt`
exports.getCodePoint = 
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
String.prototype.codePointAt != null
    ? function (str, index) { return str.codePointAt(index); }
    : // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
        function (c, index) {
            return isHighSurrugate(c.charCodeAt(index))
                ? (c.charCodeAt(index) - 55296 /* High */) * 0x400 +
                    c.charCodeAt(index + 1) -
                    0xdc00 +
                    0x10000
                : c.charCodeAt(index);
        };
var htmlTrie = getTrie(entities_json_1.default);
function encodeHTMLTrieRe(regExp, str) {
    var _a;
    var ret = "";
    var lastIdx = 0;
    var match;
    while ((match = regExp.exec(str)) !== null) {
        var i = match.index;
        var char = str.charCodeAt(i);
        var next = htmlTrie.get(char);
        if (next) {
            if (next.next != null && i + 1 < str.length) {
                var value = (_a = next.next.get(str.charCodeAt(i + 1))) === null || _a === void 0 ? void 0 : _a.value;
                if (value != null) {
                    ret += str.substring(lastIdx, i) + value;
                    regExp.lastIndex += 1;
                    lastIdx = i + 2;
                    continue;
                }
            }
            ret += str.substring(lastIdx, i) + next.value;
            lastIdx = i + 1;
        }
        else {
            ret += str.substring(lastIdx, i) + "&#x" + exports.getCodePoint(str, i).toString(16) + ";";
            // Increase by 1 if we have a surrogate pair
            lastIdx = regExp.lastIndex += Number(isHighSurrugate(char));
        }
    }
    return ret + str.substr(lastIdx);
}
exports.encodeHTMLTrieRe = encodeHTMLTrieRe;
function getTrie(map) {
    var _a, _b, _c, _d;
    var trie = new Map();
    for (var _i = 0, _e = Object.keys(map); _i < _e.length; _i++) {
        var value = _e[_i];
        var key = map[value];
        // Resolve the key
        var lastMap = trie;
        for (var i = 0; i < key.length - 1; i++) {
            var char = key.charCodeAt(i);
            var next = (_a = lastMap.get(char)) !== null && _a !== void 0 ? _a : {};
            lastMap.set(char, next);
            lastMap = (_b = next.next) !== null && _b !== void 0 ? _b : (next.next = new Map());
        }
        var val = (_c = lastMap.get(key.charCodeAt(key.length - 1))) !== null && _c !== void 0 ? _c : {};
        (_d = val.value) !== null && _d !== void 0 ? _d : (val.value = "&" + value + ";");
        lastMap.set(key.charCodeAt(key.length - 1), val);
    }
    return trie;
}
exports.getTrie = getTrie;