html-tokenizer
Version:
Small, fast, event-driven, fault-tolerant html tokenizer. Works in node or browsers.
51 lines • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAttributeName = exports.getTagEnd = exports.getScript = exports.getComment = exports.getCommentOpen = exports.getClosingTag = exports.getText = exports.getOpeningTag = void 0;
/**
* Opening tag chunker function.
*/
exports.getOpeningTag = chunker(/(<(([a-z0-9-]+:)?[a-z0-9-]+))/ig);
/**
* Text node chunker function.
*/
exports.getText = chunker(/([^<]+)/g);
/**
* Closing tag chunker function.
*/
exports.getClosingTag = chunker(/(<\/(([a-z0-9-]+:)?[a-z0-9-]+)>)/ig);
/**
* Comment open chunker function.
*/
exports.getCommentOpen = chunker(/(<!--)/g);
/**
* Comment content chunker function.
*/
exports.getComment = chunker(/(([\s\S]*?)-->)/g);
/**
* Script content chunker function.
*/
exports.getScript = chunker(/(([\s\S]*?)<\/script>)/g);
/**
* End tag chunker function.
*/
exports.getTagEnd = chunker(/(\s*(\/?>))/g);
/**
* Attribute name chunker function.
*/
exports.getAttributeName = chunker(/(\s+(([a-z0-9\-_]+:)?[a-z0-9\-_]+)(\s*=\s*)?)/ig);
function chunker(regex) {
return (str, pos) => {
regex.lastIndex = pos;
const match = regex.exec(str);
if (!match || match.index !== pos) {
return undefined;
}
else {
return {
length: match[1].length,
match,
};
}
};
}
//# sourceMappingURL=chunks.js.map