html-tokenizer
Version:
Small, fast, event-driven, fault-tolerant html tokenizer. Works in node or browsers.
114 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isClosedByParent = exports.isClosedBy = exports.isSelfClosing = void 0;
/**
* A list of tags which are self-closing in HTML.
*/
const SELF_CLOSING_TAGS = new Set([
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
]);
/**
* A list of tags which are automatically closed
* when closing tags for their parents are encountered.
*/
const CLOSED_BY_PARENTS = new Set([
'p',
'li',
'dd',
'rb',
'rt',
'rtc',
'rp',
'optgroup',
'option',
'tbody',
'tfoot',
'tr',
'td',
'th',
]);
/**
* Tags which are closed when a start tag
* of another type ocurrs.
*/
const CLOSED_BY_SIBLINGS = {
p: new Set([
'address',
'article',
'aside',
'blockquote',
'div',
'dl',
'fieldset',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hgroup',
'hr',
'main',
'nav',
'ol',
'p',
'pre',
'section',
'table',
'ul',
]),
li: new Set(['li']),
dt: new Set(['dt', 'dd']),
dd: new Set(['dt', 'dd']),
rb: new Set(['rb', 'rt', 'rtc', 'rp']),
rt: new Set(['rb', 'rt', 'rtc', 'rp']),
rtc: new Set(['rb', 'rtc', 'rp']),
rp: new Set(['rb', 'rt', 'rtc', 'rp']),
optgroup: new Set(['optgroup']),
option: new Set(['option', 'optgroup']),
thead: new Set(['tbody', 'tfoot']),
tbody: new Set(['tbody', 'tfoot']),
tfoot: new Set(['tbody']),
tr: new Set(['tr']),
td: new Set(['td', 'th']),
th: new Set(['td', 'th']),
};
/**
* Determine whether a tag is a self-closing tag.
*/
function isSelfClosing(tag) {
return SELF_CLOSING_TAGS.has(tag);
}
exports.isSelfClosing = isSelfClosing;
/**
* Determine whether a tag is closed by another tag
*/
function isClosedBy(tag, otherTag) {
var _a, _b;
return (_b = (_a = CLOSED_BY_SIBLINGS[tag]) === null || _a === void 0 ? void 0 : _a.has(otherTag)) !== null && _b !== void 0 ? _b : false;
}
exports.isClosedBy = isClosedBy;
/** Determine whether a tag is auto-closed by its parent. */
function isClosedByParent(tag) {
return CLOSED_BY_PARENTS.has(tag);
}
exports.isClosedByParent = isClosedByParent;
//# sourceMappingURL=util.js.map