entities
Advanced tools
Comparing version 3.0.1 to 4.0.0
@@ -0,2 +1,3 @@ | ||
export declare function replaceCodePoint(codePoint: number): number; | ||
export default function decodeCodePoint(codePoint: number): string; | ||
//# sourceMappingURL=decode_codepoint.d.ts.map |
"use strict"; | ||
// Adapted from https://github.com/mathiasbynens/he/blob/36afe179392226cf1b6ccdb16ebbb7a5a844d93a/src/he.js#L106-L134 | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.replaceCodePoint = void 0; | ||
var decodeMap = new Map([ | ||
@@ -47,9 +48,14 @@ [0, 65533], | ||
}; | ||
function decodeCodePoint(codePoint) { | ||
function replaceCodePoint(codePoint) { | ||
var _a; | ||
if ((codePoint >= 0xd800 && codePoint <= 0xdfff) || codePoint > 0x10ffff) { | ||
return "\uFFFD"; | ||
return 0xfffd; | ||
} | ||
return fromCodePoint((_a = decodeMap.get(codePoint)) !== null && _a !== void 0 ? _a : codePoint); | ||
return (_a = decodeMap.get(codePoint)) !== null && _a !== void 0 ? _a : codePoint; | ||
} | ||
exports.replaceCodePoint = replaceCodePoint; | ||
function decodeCodePoint(codePoint) { | ||
return fromCodePoint(replaceCodePoint(codePoint)); | ||
} | ||
exports.default = decodeCodePoint; | ||
//# sourceMappingURL=decode_codepoint.js.map |
@@ -1,11 +0,10 @@ | ||
import htmlDecodeTree from "./generated/decode-data-html"; | ||
import xmlDecodeTree from "./generated/decode-data-xml"; | ||
export { htmlDecodeTree, xmlDecodeTree }; | ||
import htmlDecodeTree from "./generated/decode-data-html.js"; | ||
import xmlDecodeTree from "./generated/decode-data-xml.js"; | ||
import decodeCodePoint from "./decode_codepoint.js"; | ||
export { htmlDecodeTree, xmlDecodeTree, decodeCodePoint }; | ||
export declare enum BinTrieFlags { | ||
HAS_VALUE = 32768, | ||
BRANCH_LENGTH = 32512, | ||
MULTI_BYTE = 128, | ||
VALUE_LENGTH = 49152, | ||
BRANCH_LENGTH = 16256, | ||
JUMP_TABLE = 127 | ||
} | ||
export declare const JUMP_OFFSET_BASE: number; | ||
export declare function determineBranch(decodeTree: Uint16Array, current: number, nodeIdx: number, char: number): number; | ||
@@ -12,0 +11,0 @@ export declare function decodeHTML(str: string): string; |
@@ -6,16 +6,15 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeXML = exports.decodeHTMLStrict = exports.decodeHTML = exports.determineBranch = exports.JUMP_OFFSET_BASE = exports.BinTrieFlags = exports.xmlDecodeTree = exports.htmlDecodeTree = void 0; | ||
var decode_data_html_1 = __importDefault(require("./generated/decode-data-html")); | ||
exports.htmlDecodeTree = decode_data_html_1.default; | ||
var decode_data_xml_1 = __importDefault(require("./generated/decode-data-xml")); | ||
exports.xmlDecodeTree = decode_data_xml_1.default; | ||
var decode_codepoint_1 = __importDefault(require("./decode_codepoint")); | ||
exports.decodeXML = exports.decodeHTMLStrict = exports.decodeHTML = exports.determineBranch = exports.BinTrieFlags = exports.decodeCodePoint = exports.xmlDecodeTree = exports.htmlDecodeTree = void 0; | ||
var decode_data_html_js_1 = __importDefault(require("./generated/decode-data-html.js")); | ||
exports.htmlDecodeTree = decode_data_html_js_1.default; | ||
var decode_data_xml_js_1 = __importDefault(require("./generated/decode-data-xml.js")); | ||
exports.xmlDecodeTree = decode_data_xml_js_1.default; | ||
var decode_codepoint_js_1 = __importDefault(require("./decode_codepoint.js")); | ||
exports.decodeCodePoint = decode_codepoint_js_1.default; | ||
var BinTrieFlags; | ||
(function (BinTrieFlags) { | ||
BinTrieFlags[BinTrieFlags["HAS_VALUE"] = 32768] = "HAS_VALUE"; | ||
BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 32512] = "BRANCH_LENGTH"; | ||
BinTrieFlags[BinTrieFlags["MULTI_BYTE"] = 128] = "MULTI_BYTE"; | ||
BinTrieFlags[BinTrieFlags["VALUE_LENGTH"] = 49152] = "VALUE_LENGTH"; | ||
BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 16256] = "BRANCH_LENGTH"; | ||
BinTrieFlags[BinTrieFlags["JUMP_TABLE"] = 127] = "JUMP_TABLE"; | ||
})(BinTrieFlags = exports.BinTrieFlags || (exports.BinTrieFlags = {})); | ||
exports.JUMP_OFFSET_BASE = 48 /* ZERO */ - 1; | ||
function getDecoder(decodeTree) { | ||
@@ -42,8 +41,8 @@ return function decodeHTMLBinary(str, strict) { | ||
} | ||
while (((cp = str.charCodeAt(++strIdx)) >= 48 /* ZERO */ && | ||
cp <= 57 /* NINE */) || | ||
do | ||
cp = str.charCodeAt(++strIdx); | ||
while ((cp >= 48 /* ZERO */ && cp <= 57 /* NINE */) || | ||
(base === 16 && | ||
(cp | 32 /* To_LOWER_BIT */) >= 97 /* LOWER_A */ && | ||
(cp | 32 /* To_LOWER_BIT */) <= 102 /* LOWER_F */)) | ||
; | ||
(cp | 32 /* To_LOWER_BIT */) <= 102 /* LOWER_F */)); | ||
if (start !== strIdx) { | ||
@@ -58,3 +57,3 @@ var entity = str.substring(start, strIdx); | ||
} | ||
ret += decode_codepoint_1.default(parsed); | ||
ret += (0, decode_codepoint_js_1.default)(parsed); | ||
lastIdx = strIdx; | ||
@@ -64,3 +63,3 @@ } | ||
} | ||
var result = null; | ||
var resultIdx = 0; | ||
var excess = 1; | ||
@@ -74,21 +73,25 @@ var treeIdx = 0; | ||
current = decodeTree[treeIdx]; | ||
var masked = current & BinTrieFlags.VALUE_LENGTH; | ||
// If the branch is a value, store it and continue | ||
if (current & BinTrieFlags.HAS_VALUE) { | ||
if (masked) { | ||
// If we have a legacy entity while parsing strictly, just skip the number of bytes | ||
if (strict && str.charCodeAt(strIdx) !== 59 /* SEMI */) { | ||
// No need to consider multi-byte values, as the legacy entity is always a single byte | ||
treeIdx += 1; | ||
} | ||
else { | ||
// If this is a surrogate pair, combine the higher bits from the node with the next byte | ||
result = | ||
current & BinTrieFlags.MULTI_BYTE | ||
? String.fromCharCode(decodeTree[++treeIdx], decodeTree[++treeIdx]) | ||
: String.fromCharCode(decodeTree[++treeIdx]); | ||
if (!strict || str.charCodeAt(strIdx) === 59 /* SEMI */) { | ||
resultIdx = treeIdx; | ||
excess = 0; | ||
} | ||
// The mask is the number of bytes of the value, including the current byte. | ||
var valueLength = (masked >> 14) - 1; | ||
if (valueLength === 0) | ||
break; | ||
treeIdx += valueLength; | ||
} | ||
} | ||
if (result != null) { | ||
ret += result; | ||
if (resultIdx !== 0) { | ||
var valueLength = (decodeTree[resultIdx] & BinTrieFlags.VALUE_LENGTH) >> 14; | ||
ret += | ||
valueLength === 1 | ||
? String.fromCharCode(decodeTree[resultIdx] & ~BinTrieFlags.VALUE_LENGTH) | ||
: valueLength === 2 | ||
? String.fromCharCode(decodeTree[resultIdx + 1]) | ||
: String.fromCharCode(decodeTree[resultIdx + 1], decodeTree[resultIdx + 2]); | ||
lastIdx = strIdx - excess + 1; | ||
@@ -101,15 +104,11 @@ } | ||
function determineBranch(decodeTree, current, nodeIdx, char) { | ||
if (current <= 128) { | ||
return char === current ? nodeIdx : -1; | ||
} | ||
var branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 8; | ||
var branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 7; | ||
var jumpOffset = current & BinTrieFlags.JUMP_TABLE; | ||
// Case 1: Single branch encoded in jump offset | ||
if (branchCount === 0) { | ||
return -1; | ||
return jumpOffset !== 0 && char === jumpOffset ? nodeIdx : -1; | ||
} | ||
if (branchCount === 1) { | ||
return char === decodeTree[nodeIdx] ? nodeIdx + 1 : -1; | ||
} | ||
var jumpOffset = current & BinTrieFlags.JUMP_TABLE; | ||
// Case 2: Multiple branches encoded in jump table | ||
if (jumpOffset) { | ||
var value = char - exports.JUMP_OFFSET_BASE - jumpOffset; | ||
var value = char - jumpOffset; | ||
return value < 0 || value > branchCount | ||
@@ -119,2 +118,3 @@ ? -1 | ||
} | ||
// Case 3: Multiple branches encoded in dictionary | ||
// Binary search for the character. | ||
@@ -139,4 +139,4 @@ var lo = nodeIdx; | ||
exports.determineBranch = determineBranch; | ||
var htmlDecoder = getDecoder(decode_data_html_1.default); | ||
var xmlDecoder = getDecoder(decode_data_xml_1.default); | ||
var htmlDecoder = getDecoder(decode_data_html_js_1.default); | ||
var xmlDecoder = getDecoder(decode_data_xml_js_1.default); | ||
function decodeHTML(str) { | ||
@@ -154,1 +154,2 @@ return htmlDecoder(str, false); | ||
exports.decodeXML = decodeXML; | ||
//# sourceMappingURL=decode.js.map |
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.getTrie = exports.encodeHTMLTrieRe = exports.getCodePoint = void 0; | ||
var entities_json_1 = __importDefault(require("./maps/entities.json")); | ||
var entities_encode_json_1 = __importDefault(require("./maps/entities-encode.json")); | ||
function isHighSurrugate(c) { | ||
@@ -26,3 +26,3 @@ return (c & 64512 /* Mask */) === 55296 /* High */; | ||
}; | ||
var htmlTrie = getTrie(entities_json_1.default); | ||
var htmlTrie = getTrie(entities_encode_json_1.default); | ||
function encodeHTMLTrieRe(regExp, str) { | ||
@@ -51,3 +51,3 @@ var _a; | ||
else { | ||
ret += str.substring(lastIdx, i) + "&#x" + exports.getCodePoint(str, i).toString(16) + ";"; | ||
ret += "".concat(str.substring(lastIdx, i), "&#x").concat((0, exports.getCodePoint)(str, i).toString(16), ";"); | ||
// Increase by 1 if we have a surrogate pair | ||
@@ -64,8 +64,8 @@ lastIdx = regExp.lastIndex += Number(isHighSurrugate(char)); | ||
for (var _i = 0, _e = Object.keys(map); _i < _e.length; _i++) { | ||
var value = _e[_i]; | ||
var key = map[value]; | ||
var decoded = _e[_i]; | ||
var entity = map[decoded]; | ||
// Resolve the key | ||
var lastMap = trie; | ||
for (var i = 0; i < key.length - 1; i++) { | ||
var char = key.charCodeAt(i); | ||
for (var i = 0; i < decoded.length - 1; i++) { | ||
var char = decoded.charCodeAt(i); | ||
var next = (_a = lastMap.get(char)) !== null && _a !== void 0 ? _a : {}; | ||
@@ -75,5 +75,5 @@ lastMap.set(char, next); | ||
} | ||
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); | ||
var val = (_c = lastMap.get(decoded.charCodeAt(decoded.length - 1))) !== null && _c !== void 0 ? _c : {}; | ||
(_d = val.value) !== null && _d !== void 0 ? _d : (val.value = "&".concat(entity, ";")); | ||
lastMap.set(decoded.charCodeAt(decoded.length - 1), val); | ||
} | ||
@@ -83,1 +83,2 @@ return trie; | ||
exports.getTrie = getTrie; | ||
//# sourceMappingURL=encode-trie.js.map |
@@ -45,3 +45,17 @@ /** | ||
*/ | ||
export declare function escapeUTF8(data: string): string; | ||
export declare const escapeUTF8: (data: string) => string; | ||
/** | ||
* Encodes all characters that have to be escaped in HTML attributes, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
* | ||
* @param data String to escape. | ||
*/ | ||
export declare const escapeAttribute: (data: string) => string; | ||
/** | ||
* Encodes all characters that have to be escaped in HTML text, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
* | ||
* @param data String to escape. | ||
*/ | ||
export declare const escapeText: (data: string) => string; | ||
//# sourceMappingURL=encode.d.ts.map |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.escapeUTF8 = exports.escape = exports.encodeNonAsciiHTML = exports.encodeHTML = exports.encodeXML = void 0; | ||
var xml_json_1 = __importDefault(require("./maps/xml.json")); | ||
var encode_trie_1 = require("./encode-trie"); | ||
var entities_json_1 = __importDefault(require("./maps/entities.json")); | ||
var htmlReplacer = getCharRegExp(entities_json_1.default, true); | ||
var xmlReplacer = getCharRegExp(xml_json_1.default, true); | ||
var xmlInvalidChars = getCharRegExp(xml_json_1.default, false); | ||
var xmlCodeMap = new Map(Object.keys(xml_json_1.default).map(function (k) { return [ | ||
xml_json_1.default[k].charCodeAt(0), | ||
"&" + k + ";", | ||
]; })); | ||
exports.escapeText = exports.escapeAttribute = exports.escapeUTF8 = exports.escape = exports.encodeNonAsciiHTML = exports.encodeHTML = exports.encodeXML = void 0; | ||
var encode_trie_js_1 = require("./encode-trie.js"); | ||
var htmlReplacer = /[\t\n!-,./:-@[-`\f{-}$\x80-\uFFFF]/g; | ||
var xmlReplacer = /["&'<>$\x80-\uFFFF]/g; | ||
var xmlCodeMap = new Map([ | ||
[34, """], | ||
[38, "&"], | ||
[39, "'"], | ||
[60, "<"], | ||
[62, ">"], | ||
]); | ||
/** | ||
@@ -37,3 +34,3 @@ * Encodes all non-ASCII characters, as well as characters not valid in XML | ||
else { | ||
ret += str.substring(lastIdx, i) + "&#x" + encode_trie_1.getCodePoint(str, i).toString(16) + ";"; | ||
ret += "".concat(str.substring(lastIdx, i), "&#x").concat((0, encode_trie_js_1.getCodePoint)(str, i).toString(16), ";"); | ||
// Increase by 1 if we have a surrogate pair | ||
@@ -57,3 +54,3 @@ lastIdx = xmlReplacer.lastIndex += Number((char & 65408) === 0xd800); | ||
function encodeHTML(data) { | ||
return encode_trie_1.encodeHTMLTrieRe(htmlReplacer, data); | ||
return (0, encode_trie_js_1.encodeHTMLTrieRe)(htmlReplacer, data); | ||
} | ||
@@ -69,29 +66,5 @@ exports.encodeHTML = encodeHTML; | ||
function encodeNonAsciiHTML(data) { | ||
return encode_trie_1.encodeHTMLTrieRe(xmlReplacer, data); | ||
return (0, encode_trie_js_1.encodeHTMLTrieRe)(xmlReplacer, data); | ||
} | ||
exports.encodeNonAsciiHTML = encodeNonAsciiHTML; | ||
function getCharRegExp(map, nonAscii) { | ||
// Collect the start characters of all entities | ||
var chars = Object.keys(map) | ||
.map(function (k) { return "\\" + map[k].charAt(0); }) | ||
.filter(function (v) { return !nonAscii || v.charCodeAt(1) < 128; }) | ||
.sort(function (a, b) { return a.charCodeAt(1) - b.charCodeAt(1); }) | ||
// Remove duplicates | ||
.filter(function (v, i, a) { return v !== a[i + 1]; }); | ||
// Add ranges to single characters. | ||
for (var start = 0; start < chars.length - 1; start++) { | ||
// Find the end of a run of characters | ||
var end = start; | ||
while (end < chars.length - 1 && | ||
chars[end].charCodeAt(1) + 1 === chars[end + 1].charCodeAt(1)) { | ||
end += 1; | ||
} | ||
var count = 1 + end - start; | ||
// We want to replace at least three characters | ||
if (count < 3) | ||
continue; | ||
chars.splice(start, count, chars[start] + "-" + chars[end]); | ||
} | ||
return new RegExp("[" + chars.join("") + (nonAscii ? "\\x80-\\uFFFF" : "") + "]", "g"); | ||
} | ||
/** | ||
@@ -107,2 +80,19 @@ * Encodes all non-ASCII characters, as well as characters not valid in XML | ||
exports.escape = encodeXML; | ||
function getEscaper(regex, map) { | ||
return function escape(data) { | ||
var match; | ||
var lastIdx = 0; | ||
var result = ""; | ||
while ((match = regex.exec(data))) { | ||
if (lastIdx !== match.index) { | ||
result += data.substring(lastIdx, match.index); | ||
} | ||
// We know that this chararcter will be in the map. | ||
result += map.get(match[0].charCodeAt(0)); | ||
// Every match will be of length 1 | ||
lastIdx = match.index + 1; | ||
} | ||
return result + data.substring(lastIdx); | ||
}; | ||
} | ||
/** | ||
@@ -115,17 +105,26 @@ * Encodes all characters not valid in XML documents using XML entities. | ||
*/ | ||
function escapeUTF8(data) { | ||
var match; | ||
var lastIdx = 0; | ||
var result = ""; | ||
while ((match = xmlInvalidChars.exec(data))) { | ||
if (lastIdx !== match.index) { | ||
result += data.substring(lastIdx, match.index); | ||
} | ||
// We know that this chararcter will be in `inverseXML` | ||
result += xmlCodeMap.get(match[0].charCodeAt(0)); | ||
// Every match will be of length 1 | ||
lastIdx = match.index + 1; | ||
} | ||
return result + data.substring(lastIdx); | ||
} | ||
exports.escapeUTF8 = escapeUTF8; | ||
exports.escapeUTF8 = getEscaper(/[&<>'"]/g, xmlCodeMap); | ||
/** | ||
* Encodes all characters that have to be escaped in HTML attributes, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
* | ||
* @param data String to escape. | ||
*/ | ||
exports.escapeAttribute = getEscaper(/["&\u00A0]/g, new Map([ | ||
[34, """], | ||
[38, "&"], | ||
[160, " "], | ||
])); | ||
/** | ||
* Encodes all characters that have to be escaped in HTML text, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
* | ||
* @param data String to escape. | ||
*/ | ||
exports.escapeText = getEscaper(/[&<>\u00A0]/g, new Map([ | ||
[38, "&"], | ||
[60, "<"], | ||
[62, ">"], | ||
[160, " "], | ||
])); | ||
//# sourceMappingURL=encode.js.map |
@@ -5,2 +5,3 @@ "use strict"; | ||
// prettier-ignore | ||
exports.default = new Uint16Array([1024, 97, 103, 108, 113, 9, 23, 27, 31, 1086, 15, 0, 0, 19, 112, 59, 32768, 38, 111, 115, 59, 32768, 39, 116, 59, 32768, 62, 116, 59, 32768, 60, 117, 111, 116, 59, 32768, 34]); | ||
exports.default = new Uint16Array([512, 97, 103, 108, 113, 9, 21, 24, 27, 621, 15, 0, 0, 18, 112, 59, 16422, 111, 115, 59, 16423, 116, 59, 16446, 116, 59, 16444, 117, 111, 116, 59, 16418]); | ||
//# sourceMappingURL=decode-data-xml.js.map |
@@ -31,8 +31,18 @@ /** The level of entities to support. */ | ||
*/ | ||
Extensive = 2 | ||
Extensive = 2, | ||
/** | ||
* Encode all characters that have to be escaped in HTML attributes, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
*/ | ||
Attribute = 3, | ||
/** | ||
* Encode all characters that have to be escaped in HTML text, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
*/ | ||
Text = 4 | ||
} | ||
interface DecodingOptions { | ||
export interface DecodingOptions { | ||
/** | ||
* The level of entities to support. | ||
* @default EntityLevel.XML | ||
* @default {@link EntityLevel.XML} | ||
*/ | ||
@@ -49,3 +59,3 @@ level?: EntityLevel; | ||
* | ||
* @default DecodingMode.Legacy | ||
* @default {@link DecodingMode.Legacy} | ||
*/ | ||
@@ -75,3 +85,3 @@ mode?: DecodingMode; | ||
* The level of entities to support. | ||
* @default EntityLevel.XML | ||
* @default {@link EntityLevel.XML} | ||
*/ | ||
@@ -81,3 +91,3 @@ level?: EntityLevel; | ||
* Output format. | ||
* @default EncodingMode.Extensive | ||
* @default {@link EncodingMode.Extensive} | ||
*/ | ||
@@ -93,4 +103,4 @@ mode?: EncodingMode; | ||
export declare function encode(data: string, options?: EncodingOptions | EntityLevel): string; | ||
export { encodeXML, encodeHTML, encodeNonAsciiHTML, escape, escapeUTF8, encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode"; | ||
export { decodeXML, decodeHTML, decodeHTMLStrict, decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode"; | ||
export { encodeXML, encodeHTML, encodeNonAsciiHTML, escape, escapeUTF8, escapeAttribute, escapeText, encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode.js"; | ||
export { decodeXML, decodeHTML, decodeHTMLStrict, decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode.js"; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeXMLStrict = exports.decodeHTML5Strict = exports.decodeHTML4Strict = exports.decodeHTML5 = exports.decodeHTML4 = exports.decodeHTMLStrict = exports.decodeHTML = exports.decodeXML = exports.encodeHTML5 = exports.encodeHTML4 = exports.escapeUTF8 = exports.escape = exports.encodeNonAsciiHTML = exports.encodeHTML = exports.encodeXML = exports.encode = exports.decodeStrict = exports.decode = exports.EncodingMode = exports.DecodingMode = exports.EntityLevel = void 0; | ||
var decode_1 = require("./decode"); | ||
var encode_1 = require("./encode"); | ||
exports.decodeXMLStrict = exports.decodeHTML5Strict = exports.decodeHTML4Strict = exports.decodeHTML5 = exports.decodeHTML4 = exports.decodeHTMLStrict = exports.decodeHTML = exports.decodeXML = exports.encodeHTML5 = exports.encodeHTML4 = exports.escapeText = exports.escapeAttribute = exports.escapeUTF8 = exports.escape = exports.encodeNonAsciiHTML = exports.encodeHTML = exports.encodeXML = exports.encode = exports.decodeStrict = exports.decode = exports.EncodingMode = exports.DecodingMode = exports.EntityLevel = void 0; | ||
var decode_js_1 = require("./decode.js"); | ||
var encode_js_1 = require("./encode.js"); | ||
/** The level of entities to support. */ | ||
@@ -40,2 +40,12 @@ var EntityLevel; | ||
EncodingMode[EncodingMode["Extensive"] = 2] = "Extensive"; | ||
/** | ||
* Encode all characters that have to be escaped in HTML attributes, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
*/ | ||
EncodingMode[EncodingMode["Attribute"] = 3] = "Attribute"; | ||
/** | ||
* Encode all characters that have to be escaped in HTML text, | ||
* following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}. | ||
*/ | ||
EncodingMode[EncodingMode["Text"] = 4] = "Text"; | ||
})(EncodingMode = exports.EncodingMode || (exports.EncodingMode = {})); | ||
@@ -53,7 +63,7 @@ /** | ||
if (opts.mode === DecodingMode.Strict) { | ||
return decode_1.decodeHTMLStrict(data); | ||
return (0, decode_js_1.decodeHTMLStrict)(data); | ||
} | ||
return decode_1.decodeHTML(data); | ||
return (0, decode_js_1.decodeHTML)(data); | ||
} | ||
return decode_1.decodeXML(data); | ||
return (0, decode_js_1.decodeXML)(data); | ||
} | ||
@@ -73,7 +83,7 @@ exports.decode = decode; | ||
if (opts.mode === DecodingMode.Legacy) { | ||
return decode_1.decodeHTML(data); | ||
return (0, decode_js_1.decodeHTML)(data); | ||
} | ||
return decode_1.decodeHTMLStrict(data); | ||
return (0, decode_js_1.decodeHTMLStrict)(data); | ||
} | ||
return decode_1.decodeXML(data); | ||
return (0, decode_js_1.decodeXML)(data); | ||
} | ||
@@ -92,31 +102,38 @@ exports.decodeStrict = decodeStrict; | ||
if (opts.mode === EncodingMode.UTF8) | ||
return encode_1.escapeUTF8(data); | ||
return (0, encode_js_1.escapeUTF8)(data); | ||
if (opts.mode === EncodingMode.Attribute) | ||
return (0, encode_js_1.escapeAttribute)(data); | ||
if (opts.mode === EncodingMode.Text) | ||
return (0, encode_js_1.escapeText)(data); | ||
if (opts.level === EntityLevel.HTML) { | ||
if (opts.mode === EncodingMode.ASCII) { | ||
return encode_1.encodeNonAsciiHTML(data); | ||
return (0, encode_js_1.encodeNonAsciiHTML)(data); | ||
} | ||
return encode_1.encodeHTML(data); | ||
return (0, encode_js_1.encodeHTML)(data); | ||
} | ||
// ASCII and Extensive are equivalent | ||
return encode_1.encodeXML(data); | ||
return (0, encode_js_1.encodeXML)(data); | ||
} | ||
exports.encode = encode; | ||
var encode_2 = require("./encode"); | ||
Object.defineProperty(exports, "encodeXML", { enumerable: true, get: function () { return encode_2.encodeXML; } }); | ||
Object.defineProperty(exports, "encodeHTML", { enumerable: true, get: function () { return encode_2.encodeHTML; } }); | ||
Object.defineProperty(exports, "encodeNonAsciiHTML", { enumerable: true, get: function () { return encode_2.encodeNonAsciiHTML; } }); | ||
Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return encode_2.escape; } }); | ||
Object.defineProperty(exports, "escapeUTF8", { enumerable: true, get: function () { return encode_2.escapeUTF8; } }); | ||
var encode_js_2 = require("./encode.js"); | ||
Object.defineProperty(exports, "encodeXML", { enumerable: true, get: function () { return encode_js_2.encodeXML; } }); | ||
Object.defineProperty(exports, "encodeHTML", { enumerable: true, get: function () { return encode_js_2.encodeHTML; } }); | ||
Object.defineProperty(exports, "encodeNonAsciiHTML", { enumerable: true, get: function () { return encode_js_2.encodeNonAsciiHTML; } }); | ||
Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return encode_js_2.escape; } }); | ||
Object.defineProperty(exports, "escapeUTF8", { enumerable: true, get: function () { return encode_js_2.escapeUTF8; } }); | ||
Object.defineProperty(exports, "escapeAttribute", { enumerable: true, get: function () { return encode_js_2.escapeAttribute; } }); | ||
Object.defineProperty(exports, "escapeText", { enumerable: true, get: function () { return encode_js_2.escapeText; } }); | ||
// Legacy aliases (deprecated) | ||
Object.defineProperty(exports, "encodeHTML4", { enumerable: true, get: function () { return encode_2.encodeHTML; } }); | ||
Object.defineProperty(exports, "encodeHTML5", { enumerable: true, get: function () { return encode_2.encodeHTML; } }); | ||
var decode_2 = require("./decode"); | ||
Object.defineProperty(exports, "decodeXML", { enumerable: true, get: function () { return decode_2.decodeXML; } }); | ||
Object.defineProperty(exports, "decodeHTML", { enumerable: true, get: function () { return decode_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTMLStrict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } }); | ||
Object.defineProperty(exports, "encodeHTML4", { enumerable: true, get: function () { return encode_js_2.encodeHTML; } }); | ||
Object.defineProperty(exports, "encodeHTML5", { enumerable: true, get: function () { return encode_js_2.encodeHTML; } }); | ||
var decode_js_2 = require("./decode.js"); | ||
Object.defineProperty(exports, "decodeXML", { enumerable: true, get: function () { return decode_js_2.decodeXML; } }); | ||
Object.defineProperty(exports, "decodeHTML", { enumerable: true, get: function () { return decode_js_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTMLStrict", { enumerable: true, get: function () { return decode_js_2.decodeHTMLStrict; } }); | ||
// Legacy aliases (deprecated) | ||
Object.defineProperty(exports, "decodeHTML4", { enumerable: true, get: function () { return decode_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTML5", { enumerable: true, get: function () { return decode_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTML4Strict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } }); | ||
Object.defineProperty(exports, "decodeHTML5Strict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } }); | ||
Object.defineProperty(exports, "decodeXMLStrict", { enumerable: true, get: function () { return decode_2.decodeXML; } }); | ||
Object.defineProperty(exports, "decodeHTML4", { enumerable: true, get: function () { return decode_js_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTML5", { enumerable: true, get: function () { return decode_js_2.decodeHTML; } }); | ||
Object.defineProperty(exports, "decodeHTML4Strict", { enumerable: true, get: function () { return decode_js_2.decodeHTMLStrict; } }); | ||
Object.defineProperty(exports, "decodeHTML5Strict", { enumerable: true, get: function () { return decode_js_2.decodeHTMLStrict; } }); | ||
Object.defineProperty(exports, "decodeXMLStrict", { enumerable: true, get: function () { return decode_js_2.decodeXML; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "entities", | ||
"version": "3.0.1", | ||
"description": "Encode & decode XML and HTML entities with ease", | ||
"version": "4.0.0", | ||
"description": "Encode & decode XML and HTML entities with ease & speed", | ||
"author": "Felix Boehm <me@feedic.com>", | ||
@@ -21,2 +21,13 @@ "funding": "https://github.com/fb55/entities?sponsor=1", | ||
"types": "lib/index.d.ts", | ||
"module": "lib/esm/index.js", | ||
"exports": { | ||
".": { | ||
"require": "./lib/index.js", | ||
"import": "./lib/esm/index.js" | ||
}, | ||
"./lib/decode.js": { | ||
"require": "./lib/decode.js", | ||
"import": "./lib/esm/decode.js" | ||
} | ||
}, | ||
"files": [ | ||
@@ -29,13 +40,14 @@ "lib/**/*" | ||
"devDependencies": { | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.4.13", | ||
"@typescript-eslint/eslint-plugin": "^4.29.0", | ||
"@typescript-eslint/parser": "^4.29.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"@types/jest": "^27.4.1", | ||
"@types/node": "^17.0.23", | ||
"@typescript-eslint/eslint-plugin": "^5.17.0", | ||
"@typescript-eslint/parser": "^5.17.0", | ||
"eslint": "^8.12.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"jest": "^27.0.3", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^27.0.4", | ||
"typescript": "^4.0.2" | ||
"jest": "^27.5.1", | ||
"prettier": "^2.6.1", | ||
"ts-jest": "^27.1.4", | ||
"typedoc": "^0.22.13", | ||
"typescript": "^4.6.3" | ||
}, | ||
@@ -52,3 +64,9 @@ "scripts": { | ||
"prettier": "prettier '**/*.{ts,md,json,yml}'", | ||
"build": "tsc && cp -r src/maps lib", | ||
"build": "npm run build:cjs && npm run build:esm", | ||
"build:cjs": "tsc && cp -r src/maps lib", | ||
"build:esm": "tsc --module esnext --target es2019 --outDir lib/esm && npm rum build:esm:fixup && echo '{\"type\":\"module\"}' > lib/esm/package.json", | ||
"build:esm:fixup": "sed -i.b '1s|\".*json\"|\"../maps/entities-encode.json\" assert {type:\"json\"}|' lib/esm/encode-trie.js && rm lib/esm/encode-trie.js.b", | ||
"build:docs": "typedoc --hideGenerator src/index.ts", | ||
"build:trie": "ts-node scripts/write-decode-map.ts", | ||
"build:encode-map": "jq -c 'to_entries | reverse | map( {(.value) : .key } ) | sort | add' maps/entities.json > src/maps/entities-encode.json", | ||
"prepare": "npm run build" | ||
@@ -64,3 +82,6 @@ }, | ||
"testEnvironment": "node", | ||
"coverageProvider": "v8" | ||
"coverageProvider": "v8", | ||
"moduleNameMapper": { | ||
"^(.*)\\.js$": "$1" | ||
} | ||
}, | ||
@@ -67,0 +88,0 @@ "prettier": { |
@@ -5,2 +5,15 @@ # entities [![NPM version](http://img.shields.io/npm/v/entities.svg)](https://npmjs.org/package/entities) [![Downloads](https://img.shields.io/npm/dm/entities.svg)](https://npmjs.org/package/entities) [![Build Status](http://img.shields.io/travis/fb55/entities.svg)](http://travis-ci.org/fb55/entities) [![Coverage](http://img.shields.io/coveralls/fb55/entities.svg)](https://coveralls.io/r/fb55/entities) | ||
## Features | ||
- ⚔️ Battle tested: `entities` is used by many popular libraries; eg. | ||
[`htmlparser2`](https://github.com/fb55/htmlparser2), the official | ||
[AWS SDK](https://github.com/aws/aws-sdk-js-v3) and | ||
[`commonmark`](https://github.com/commonmark/commonmark.js) use it to | ||
process HTML entities. | ||
- ⚡️ Fast: `entities` is the fastes library for decoding HTML entities (as of | ||
August 2021); see [performance](#performance). | ||
- 🎛 Configurable: Get an output tailored for your needs. You are fine with | ||
UTF8? That'll safe you some bytes. Prefer to only have ASCII characters? We | ||
can do that as well! | ||
## How to… | ||
@@ -17,8 +30,8 @@ | ||
//encoding | ||
entities.escape("&"); // "&#38;" | ||
entities.encodeXML("&"); // "&#38;" | ||
entities.encodeHTML("&"); // "&#38;" | ||
// Encoding | ||
entities.escapeUTF8("& ü"); // "&#38; ü" | ||
entities.encodeXML("& ü"); // "&#38; ü" | ||
entities.encodeHTML("& ü"); // "&#38; ü" | ||
//decoding | ||
// Decoding | ||
entities.decodeXML("asdf & ÿ ü '"); // "asdf & ÿ ü '" | ||
@@ -31,13 +44,49 @@ entities.decodeHTML("asdf & ÿ ü '"); // "asdf & ÿ ü '" | ||
This is how `entities` compares to other libraries on a very basic benchmark | ||
(see `scripts/benchmark.ts`, for 10,000,000 iterations): | ||
(see `scripts/benchmark.ts`, for 10,000,000 iterations; **lower is better**): | ||
| Library | `decode` perf | `encode` perf | `escape` perf | Bundle size | | ||
| -------------- | ------------- | ------------- | ------------- | -------------------------------------------------------------------------- | | ||
| entities | 1.418s | 6.786s | 2.196s | ![npm bundle size](https://img.shields.io/bundlephobia/min/entities) | | ||
| html-entities | 2.530s | 6.829s | 2.415s | ![npm bundle size](https://img.shields.io/bundlephobia/min/html-entities) | | ||
| he | 5.800s | 24.237s | 3.624s | ![npm bundle size](https://img.shields.io/bundlephobia/min/he) | | ||
| parse-entities | 9.660s | N/A | N/A | ![npm bundle size](https://img.shields.io/bundlephobia/min/parse-entities) | | ||
| Library | Version | `decode` perf | `encode` perf | `escape` perf | | ||
| -------------- | ------- | ------------- | ------------- | ------------- | | ||
| entities | `3.0.1` | 1.418s | 6.786s | 2.196s | | ||
| html-entities | `2.3.2` | 2.530s | 6.829s | 2.415s | | ||
| he | `1.2.0` | 5.800s | 24.237s | 3.624s | | ||
| parse-entities | `3.0.0` | 9.660s | N/A | N/A | | ||
--- | ||
## FAQ | ||
> What methods should I actually use to encode my documents? | ||
If your target supports UTF-8, the `escapeUTF8` method is going to be your best | ||
choice. Otherwise, use either `encodeHTML` or `encodeXML` based on whether | ||
you're dealing with an HTML or an XML document. | ||
You can have a look at the options for the `encode` and `decode` methods to see | ||
everything you can configure. | ||
> When should I use strict decoding? | ||
Strict decoding is for decoding entities in attributes, as well as in legacy | ||
environments. When strict decoding, entities not terminated with a semicolon | ||
will be ignored. | ||
> Why should I use `entities` instead of alternative modules? | ||
As of August 2021, `entities` is a bit faster than other modules. Still, this is | ||
not a very differentiated space and other modules can catch up. | ||
**More importantly**, you might already have `entities` in your dependency graph | ||
(as a dependency of eg. `cheerio`, or `htmlparser2`), and including it directly | ||
might not even increase your bundle size. The same is true for other entity | ||
libraries, so have a look through your `node_modules` directory! | ||
> Does `entities` support tree shaking? | ||
This depends on your bundler, but yes, it should! Eg. recent versions of Webpack | ||
are able to tree-shake commonjs projects, and having the `sideEffects` flag in | ||
the `package.json` set to `false` means that your bundles should be much | ||
smaller. | ||
--- | ||
## Acknowledgements | ||
@@ -44,0 +93,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
616366
62
2094
124
12
1