Skip to content

Commit

Permalink
fix: fail on colon at start of QName
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jul 4, 2018
1 parent a1add21 commit 507addd
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,6 @@ function notMatch(regex, c) {
return !isMatch(regex, c);
}

function qname(name, attribute) {
const colon = name.indexOf(":");
let prefix;
let local;

if (colon < 0) {
// <x "xmlns"="http://foo">
if (attribute && name === "xmlns") {
prefix = "xmlns";
local = "";
}
else {
prefix = "";
local = name;
}
}
else {
prefix = name.substr(0, colon);
local = name.substr(colon + 1);
}

return { prefix, local };
}

class SAXParser {
constructor(opt) {
this._init(opt);
Expand Down Expand Up @@ -859,7 +835,7 @@ class SAXParser {

attrib() {
if (this.opt.xmlns) {
const { prefix, local } = qname(this.attribName, true);
const { prefix, local } = this.qname(this.attribName, true);

if (prefix === "xmlns") {
const uri = this.attribValue;
Expand Down Expand Up @@ -910,6 +886,33 @@ ${XML_NAMESPACE}.`);
this.attribName = this.attribValue = "";
}

qname(name, attribute) {
const colon = name.indexOf(":");
let prefix;
let local;

if (colon < 0) {
// <x "xmlns"="http://foo">
if (attribute && name === "xmlns") {
prefix = "xmlns";
local = "";
}
else {
prefix = "";
local = name;
}
}
else {
// A colon at the start of the name is illegal.
if (colon === 0) {
this.fail(`Invalid name: ${name}`);
}
prefix = name.substr(0, colon);
local = name.substr(colon + 1);
}
return { prefix, local };
}

newTag() {
const parent = this.tags[this.tags.length - 1] || this;
const tag = this.tag = {
Expand All @@ -931,7 +934,7 @@ ${XML_NAMESPACE}.`);
const { tag } = this;

// add namespace info to tag
const qn = qname(this.tagName);
const qn = this.qname(this.tagName);
tag.prefix = qn.prefix;
tag.local = qn.local;
tag.uri = tag.ns[qn.prefix] || "";
Expand Down Expand Up @@ -959,7 +962,7 @@ ${XML_NAMESPACE}.`);
// Note: do not apply default ns to attributes:
// http://www.w3.org/TR/REC-xml-names/#defaulting
for (const [name, value] of this.attribList) {
const { prefix, local } = qname(name, true);
const { prefix, local } = this.qname(name, true);
const uri = prefix === "" ? "" : (tag.ns[prefix] || "");
const a = {
name,
Expand Down

0 comments on commit 507addd

Please sign in to comment.