Skip to content

Commit

Permalink
refactor(parser): Use Map for openImpliesClose (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Aug 20, 2021
1 parent 5ab080e commit 39a8109
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions src/Parser.ts
Expand Up @@ -9,58 +9,60 @@ const formTags = new Set([
"datalist",
"textarea",
]);

const pTag = new Set(["p"]);

const openImpliesClose: Record<string, Set<string>> = {
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<string, Set<string>>([
["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",
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 39a8109

Please sign in to comment.