html-tokenizer
Version:
Small, fast, event-driven, fault-tolerant html tokenizer. Works in node or browsers.
37 lines • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const PATTERN = /&(#?)([a-z0-9]+);/ig;
const HANDLERS = new WeakMap();
function getHandler(map) {
let handler = HANDLERS.get(map);
if (!handler) {
const callback = function (ent, isNum, content) {
if (isNum) {
const num = content.charAt(0) === 'x'
? parseInt('0' + content, 16)
: parseInt(content, 10);
return String.fromCharCode(num);
}
else {
return map[content] || ent;
}
};
handler = (text) => {
return text.indexOf('&') > -1 // attempt short circuit
? text.replace(PATTERN, callback)
: text;
};
HANDLERS.set(map, handler);
}
return handler;
}
/**
* Replace entities within a chunk of text with the
* characters they represent.
*/
function deentify(text, map) {
const handler = getHandler(map);
return handler(text);
}
exports.default = deentify;
//# sourceMappingURL=deentify.js.map