Skip to content

Commit

Permalink
fix: report an error on duplicate attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jul 4, 2018
1 parent 0773c15 commit ee4e340
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ ${XML_NAMESPACE}.`);
}
}

const seen = new Set();
// Note: do not apply default ns to attributes:
// http://www.w3.org/TR/REC-xml-names/#defaulting
for (const [name, value] of this.attribList) {
Expand All @@ -952,6 +953,12 @@ ${XML_NAMESPACE}.`);
uri,
};

const eqname = `{${uri}}${local}`;
if (seen.has(eqname)) {
this.fail(`Duplicate attribute: ${eqname}`);
}
seen.add(eqname);

// if there's any attributes with an undefined namespace,
// then fail on them now.
if (prefix && prefix !== "xmlns" && !uri) {
Expand All @@ -966,6 +973,9 @@ ${XML_NAMESPACE}.`);
else {
for (const [name, value] of this.attribList) {
const a = { name, value };
if (this.tag.attributes[name]) {
this.fail(`Duplicate attribute: ${name}.`);
}
this.tag.attributes[name] = value;
this.emitNode("onattribute", a);
}
Expand Down
1 change: 1 addition & 0 deletions test/duplicate-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require(".").test({
attributes: {},
}],
["attribute", { name: "id", value: "hello" }],
["error", "Duplicate attribute: id.\nLine: 0\nColumn: 28\nChar: >"],
["attribute", { name: "id", value: "there" }],
["opentag", {
name: "span",
Expand Down

0 comments on commit ee4e340

Please sign in to comment.