diff --git a/src/Parser.ts b/src/Parser.ts index 8ab4d7c48..73f9c2f8d 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -9,58 +9,60 @@ const formTags = new Set([ "datalist", "textarea", ]); - const pTag = new Set(["p"]); - -const openImpliesClose: Record> = { - tr: new Set(["tr", "th", "td"]), - th: new Set(["th"]), - td: new Set(["thead", "th", "td"]), - body: new Set(["head", "link", "script"]), - li: new Set(["li"]), - p: pTag, - h1: pTag, - h2: pTag, - h3: pTag, - h4: pTag, - h5: pTag, - h6: pTag, - select: formTags, - input: formTags, - output: formTags, - button: formTags, - datalist: formTags, - textarea: formTags, - option: new Set(["option"]), - optgroup: new Set(["optgroup", "option"]), - dd: new Set(["dt", "dd"]), - dt: new Set(["dt", "dd"]), - address: pTag, - article: pTag, - aside: pTag, - blockquote: pTag, - details: pTag, - div: pTag, - dl: pTag, - fieldset: pTag, - figcaption: pTag, - figure: pTag, - footer: pTag, - form: pTag, - header: pTag, - hr: pTag, - main: pTag, - nav: pTag, - ol: pTag, - pre: pTag, - section: pTag, - table: pTag, - ul: pTag, - rt: new Set(["rt", "rp"]), - rp: new Set(["rt", "rp"]), - tbody: new Set(["thead", "tbody"]), - tfoot: new Set(["thead", "tbody"]), -}; +const tableSectionTags = new Set(["thead", "tbody"]); +const ddtTags = new Set(["dd", "dt"]); +const rtpTags = new Set(["rt", "rp"]); + +const openImpliesClose = new Map>([ + ["tr", new Set(["tr", "th", "td"])], + ["th", new Set(["th"])], + ["td", new Set(["thead", "th", "td"])], + ["body", new Set(["head", "link", "script"])], + ["li", new Set(["li"])], + ["p", pTag], + ["h1", pTag], + ["h2", pTag], + ["h3", pTag], + ["h4", pTag], + ["h5", pTag], + ["h6", pTag], + ["select", formTags], + ["input", formTags], + ["output", formTags], + ["button", formTags], + ["datalist", formTags], + ["textarea", formTags], + ["option", new Set(["option"])], + ["optgroup", new Set(["optgroup", "option"])], + ["dd", ddtTags], + ["dt", ddtTags], + ["address", pTag], + ["article", pTag], + ["aside", pTag], + ["blockquote", pTag], + ["details", pTag], + ["div", pTag], + ["dl", pTag], + ["fieldset", pTag], + ["figcaption", pTag], + ["figure", pTag], + ["footer", pTag], + ["form", pTag], + ["header", pTag], + ["hr", pTag], + ["main", pTag], + ["nav", pTag], + ["ol", pTag], + ["pre", pTag], + ["section", pTag], + ["table", pTag], + ["ul", pTag], + ["rt", rtpTags], + ["rp", rtpTags], + ["tbody", tableSectionTags], + ["tfoot", tableSectionTags], +]); const voidElements = new Set([ "area", @@ -245,13 +247,14 @@ export class Parser { name = name.toLowerCase(); } this.tagname = name; - if ( - !this.options.xmlMode && - Object.prototype.hasOwnProperty.call(openImpliesClose, name) - ) { + + const impliesClose = + !this.options.xmlMode && openImpliesClose.get(name); + + if (impliesClose) { while ( this.stack.length > 0 && - openImpliesClose[name].has(this.stack[this.stack.length - 1]) + impliesClose.has(this.stack[this.stack.length - 1]) ) { const el = this.stack.pop()!; this.cbs.onclosetag?.(el);