Skip to content

Commit

Permalink
refactor: split openTag into two specialized functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jan 16, 2019
1 parent 738150d commit fafa29b
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,10 @@ class SaxesParser {
}

this.emitNode("onopentagstart", tag);
this.sawRoot = true;
if (!this.fragmentOpt && this.closedRoot) {
this.fail("documents may contain only one root.");
}

switch (c) {
case GREATER:
Expand All @@ -1316,7 +1320,7 @@ class SaxesParser {
sOpenTagSlash() {
const c = this.getCode();
if (c === GREATER) {
this.openTag(true);
this.openSelfClosingTag();
}
else {
this.fail("forward-slash in opening tag not followed by >.");
Expand Down Expand Up @@ -1775,33 +1779,38 @@ class SaxesParser {
* the whole tag. This method checks for well-formeness and then emits
* ``onopentag``.
*
* @param {boolean} [selfClosing=false] Whether the tag is self-closing.
* @private
*/
openTag() {
this.processAttribs();

const { tag, tags } = this;
tag.isSelfClosing = false;

this.emitNode("onopentag", tag);
this.inRoot = true;
tags.push(tag);
this.state = S_TEXT;
this.name = "";
}

/**
* Handle a complete self-closing tag. This parser code calls this once it has
* seen the whole tag. This method checks for well-formeness and then emits
* ``onopentag`` and ``onclosetag``.
*
* @private
*/
openTag(selfClosing) {
openSelfClosingTag() {
this.processAttribs();

const { tag } = this;
selfClosing = !!selfClosing;
tag.isSelfClosing = selfClosing;
const { tag, tags } = this;
tag.isSelfClosing = true;

if (!this.fragmentOpt && this.closedRoot) {
this.fail("documents may contain only one root.");
}
this.sawRoot = true;
const { tags } = this;
if (selfClosing) {
this.emitNodes("onopentag", "onclosetag", tag);
const top = this.tag = tags[tags.length - 1];
if (!top) {
this.closedRoot = true;
}
}
else {
this.emitNode("onopentag", tag);
this.inRoot = true;
tags.push(tag);
this.emitNodes("onopentag", "onclosetag", tag);
const top = this.tag = tags[tags.length - 1];
if (!top) {
this.closedRoot = true;
}
this.state = S_TEXT;
this.name = "";
Expand Down Expand Up @@ -1832,12 +1841,10 @@ class SaxesParser {
while (l-- > 0) {
const tag = this.tag = tags.pop();
this.emitNode("onclosetag", tag);
if (tag.name !== name) {
this.fail("unexpected close tag.");
}
else {
if (tag.name === name) {
break;
}
this.fail("unexpected close tag.");
}

if (l === 0) {
Expand Down

0 comments on commit fafa29b

Please sign in to comment.