Skip to content

Commit

Permalink
perf: reduce the number of calls to closeText
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Oct 3, 2019
1 parent 3412fcb commit 3e68df5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
21 changes: 3 additions & 18 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,9 @@ class SaxesParser {
// a state handler cannot return ``undefined``. That's why we don't test
// for it.
const c = this.getCode();
if (this.text.length !== 0) {
this.closeText();
}
// either a /, ?, !, or text is coming next.
if (isNameStartChar(c)) {
this.state = S_OPEN_TAG;
Expand Down Expand Up @@ -1222,9 +1225,6 @@ class SaxesParser {
if (this.doctype || this.sawRoot) {
this.fail("inappropriately located doctype declaration.");
}
if (this.text.length !== 0) {
this.closeText();
}
this.openWakaBang = "";
break;
default:
Expand Down Expand Up @@ -1387,9 +1387,6 @@ class SaxesParser {
const c = this.getCode();
if (c === MINUS) {
this.state = S_COMMENT_ENDED;
if (this.text.length !== 0) {
this.closeText();
}
this.oncomment(this.comment);
this.comment = "";
}
Expand Down Expand Up @@ -1438,9 +1435,6 @@ class SaxesParser {
const c = this.getCode();
switch (c) {
case GREATER:
if (this.text.length !== 0) {
this.closeText();
}
this.oncdata(this.cdata);
this.cdata = "";
this.state = S_TEXT;
Expand Down Expand Up @@ -1680,9 +1674,6 @@ class SaxesParser {
if (this.piTarget.trim().toLowerCase() === "xml") {
this.fail("the XML declaration must appear at the start of the document.");
}
if (this.text.length !== 0) {
this.closeText();
}
this.onprocessinginstruction({
target: this.piTarget,
body: this.piBody,
Expand Down Expand Up @@ -1721,9 +1712,6 @@ class SaxesParser {
tag.ns = Object.create(null);
}

if (this.text.length !== 0) {
this.closeText();
}
this.onopentagstart(tag);
this.sawRoot = true;
if (!this.fragmentOpt && this.closedRoot) {
Expand Down Expand Up @@ -2250,9 +2238,6 @@ class SaxesParser {
let l = tags.length;
while (l-- > 0) {
const tag = this.tag = tags.pop();
if (this.text.length !== 0) {
this.closeText();
}
this.onclosetag(tag);
if (tag.name === name) {
break;
Expand Down
8 changes: 4 additions & 4 deletions test/issue-86.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ require(".").test({
isSelfClosing: false,
},
],
[
"text",
"de",
],
[
"error",
"1:19: text data outside of root node.",
Expand All @@ -39,10 +43,6 @@ require(".").test({
"error",
"1:20: unexpected end.",
],
[
"text",
"de",
],
],
opt: {},
});
12 changes: 6 additions & 6 deletions test/parser-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("parser position", () => {
["<div>abcdefgh</div>"], [
["opentagstart", { position: 5 }],
["opentag", { position: 5 }],
["text", { position: 19 }],
["text", { position: 14 }],
["closetag", { position: 19 }],
]);

Expand All @@ -47,7 +47,7 @@ describe("parser position", () => {
["<div>abcde", "fgh</div>"], [
["opentagstart", { position: 5 }],
["opentag", { position: 5 }],
["text", { position: 19 }],
["text", { position: 14 }],
["closetag", { position: 19 }],
]);

Expand All @@ -62,11 +62,11 @@ describe("parser position", () => {
const expected = [
["opentagstart", { position: 5, line: 1, column: 5 }],
["opentag", { position: 5, line: 1, column: 5 }],
["text", { position: 17, line: 2, column: 5 }, "abcde\n"],
["text", { position: 13, line: 2, column: 1 }, "abcde\n"],
["opentagstart", { position: 17, line: 2, column: 5 }],
["opentag", { position: 18, line: 2, column: 6 }],
["closetag", { position: 18, line: 2, column: 6 }],
["text", { position: 35, line: 5, column: 11 }, "f\ngh\n\ni\u0085j\u2028k"],
["text", { position: 30, line: 5, column: 6 }, "f\ngh\n\ni\u0085j\u2028k"],
["closetag", { position: 35, line: 5, column: 11 }],
];

Expand All @@ -79,11 +79,11 @@ describe("parser position", () => {
const expected = [
["opentagstart", { position: 5, line: 1, column: 5 }],
["opentag", { position: 5, line: 1, column: 5 }],
["text", { position: 17, line: 2, column: 5 }, "abcde\n"],
["text", { position: 13, line: 2, column: 1 }, "abcde\n"],
["opentagstart", { position: 17, line: 2, column: 5 }],
["opentag", { position: 18, line: 2, column: 6 }],
["closetag", { position: 18, line: 2, column: 6 }],
["text", { position: 35, line: 7, column: 7 }, "f\ngh\n\ni\nj\nk"],
["text", { position: 30, line: 7, column: 2 }, "f\ngh\n\ni\nj\nk"],
["closetag", { position: 35, line: 7, column: 7 }],
];

Expand Down

0 comments on commit 3e68df5

Please sign in to comment.