diff --git a/lib/saxes.js b/lib/saxes.js index 2af7fcb2..abbe4988 100644 --- a/lib/saxes.js +++ b/lib/saxes.js @@ -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; @@ -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: @@ -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 = ""; } @@ -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; @@ -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, @@ -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) { @@ -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; diff --git a/test/issue-86.js b/test/issue-86.js index 051150a5..d68e92f1 100644 --- a/test/issue-86.js +++ b/test/issue-86.js @@ -31,6 +31,10 @@ require(".").test({ isSelfClosing: false, }, ], + [ + "text", + "de", + ], [ "error", "1:19: text data outside of root node.", @@ -39,10 +43,6 @@ require(".").test({ "error", "1:20: unexpected end.", ], - [ - "text", - "de", - ], ], opt: {}, }); diff --git a/test/parser-position.js b/test/parser-position.js index ccf17c1e..116a7c46 100644 --- a/test/parser-position.js +++ b/test/parser-position.js @@ -38,7 +38,7 @@ describe("parser position", () => { ["
abcdefgh
"], [ ["opentagstart", { position: 5 }], ["opentag", { position: 5 }], - ["text", { position: 19 }], + ["text", { position: 14 }], ["closetag", { position: 19 }], ]); @@ -47,7 +47,7 @@ describe("parser position", () => { ["
abcde", "fgh
"], [ ["opentagstart", { position: 5 }], ["opentag", { position: 5 }], - ["text", { position: 19 }], + ["text", { position: 14 }], ["closetag", { position: 19 }], ]); @@ -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 }], ]; @@ -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 }], ];