Skip to content

Commit

Permalink
feat(parser): Add isVoidElement method (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 22, 2021
1 parent 3562865 commit 00ce57a
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Parser.ts
Expand Up @@ -240,6 +240,10 @@ export class Parser {
this.cbs.ontext?.(data);
}

isVoidElement(name: string): boolean {
return !this.options.xmlMode && voidElements.has(name);
}

onopentagname(name: string): void {
if (this.lowerCaseTagNames) {
name = name.toLowerCase();
Expand All @@ -259,7 +263,7 @@ export class Parser {
this.onclosetag(el);
}
}
if (this.options.xmlMode || !voidElements.has(name)) {
if (!this.isVoidElement(name)) {
this.stack.push(name);
if (foreignContextElements.has(name)) {
this.foreignContext.push(true);
Expand All @@ -277,11 +281,7 @@ export class Parser {
this.cbs.onopentag?.(this.tagname, this.attribs);
this.attribs = null;
}
if (
!this.options.xmlMode &&
this.cbs.onclosetag &&
voidElements.has(this.tagname)
) {
if (this.cbs.onclosetag && this.isVoidElement(this.tagname)) {
this.cbs.onclosetag(this.tagname);
}
this.tagname = "";
Expand All @@ -298,10 +298,7 @@ export class Parser {
) {
this.foreignContext.pop();
}
if (
this.stack.length &&
(this.options.xmlMode || !voidElements.has(name))
) {
if (this.stack.length && !this.isVoidElement(name)) {
let pos = this.stack.lastIndexOf(name);
if (pos !== -1) {
if (this.cbs.onclosetag) {
Expand Down

0 comments on commit 00ce57a

Please sign in to comment.