Skip to content

Commit

Permalink
fix: detect unclosed tags in fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jan 16, 2019
1 parent fafa29b commit 5642f36
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,8 +1586,10 @@ class SaxesParser {
if (!this.sawRoot) {
this.fail("document must contain a root element.");
}
if (this.sawRoot && !this.closedRoot) {
this.fail("unclosed root tag.");
const { tags } = this;
while (tags.length > 0) {
const tag = tags.pop();
this.fail(`unclosed tag: ${tag.name}`);
}
if ((this.state !== S_BEGIN_WHITESPACE) &&
(this.state !== S_TEXT)) {
Expand Down
54 changes: 54 additions & 0 deletions test/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,60 @@ describe("fragments", () => {
},
});

// This case was added to check for a bug in the parsing logic that prevented
// detecting if an element was not closed.
test({
name: "unclosed tag",
xml: "Something <blah>1</blah><more>2",
expect: [
["text", "Something "],
["opentagstart", {
name: "blah",
attributes: {},
ns: {},
}],
["opentag", {
name: "blah",
local: "blah",
prefix: "",
uri: "",
attributes: {},
ns: {},
isSelfClosing: false,
}],
["text", "1"],
["closetag", {
name: "blah",
local: "blah",
prefix: "",
uri: "",
attributes: {},
ns: {},
isSelfClosing: false,
}],
["opentagstart", {
name: "more",
attributes: {},
ns: {},
}],
["opentag", {
name: "more",
local: "more",
prefix: "",
uri: "",
attributes: {},
ns: {},
isSelfClosing: false,
}],
["error", "undefined:1:31: unclosed tag: more"],
["text", "2"],
],
opt: {
xmlns: true,
fragment: true,
},
});

test({
name: "namespaces",
xml: "Something <foo:blah>1</foo:blah> something",
Expand Down
2 changes: 1 addition & 1 deletion test/unclosed-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require(".").test({
],
[
"error",
"undefined:1:6: unclosed root tag.",
"undefined:1:6: unclosed tag: root",
],
],
opt: {},
Expand Down

0 comments on commit 5642f36

Please sign in to comment.