Skip to content

Commit

Permalink
refactor: perform ns mapping checks as we go
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Jan 16, 2019
1 parent e34c6d6 commit 738150d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
73 changes: 42 additions & 31 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,48 @@ const DOCTYPE_DTD_TERMINATOR = [...QUOTES, CLOSE_BRACKET];
const XML_DECL_NAME_TERMINATOR = [EQUAL, QUESTION, ...S_LIST];
const ATTRIB_VALUE_UNQUOTED_TERMINATOR = [...S_LIST, GREATER, AMP, LESS];

function nsMappingCheck(parser, mapping) {
const { xml, xmlns } = mapping;
if (xml && xml !== XML_NAMESPACE) {
parser.fail(`xml prefix must be bound to ${XML_NAMESPACE}.`);
}

if (xmlns && xmlns !== XMLNS_NAMESPACE) {
parser.fail(`xmlns prefix must be bound to ${XMLNS_NAMESPACE}.`);
function nsPairCheck(parser, prefix, uri) {
switch (prefix) {
case "xml":
if (uri !== XML_NAMESPACE) {
parser.fail(`xml prefix must be bound to ${XML_NAMESPACE}.`);
}
break;
case "xmlns":
if (uri !== XMLNS_NAMESPACE) {
parser.fail(`xmlns prefix must be bound to ${XMLNS_NAMESPACE}.`);
}
break;
default:
}

for (const local of Object.keys(mapping)) {
const uri = mapping[local];
switch (uri) {
case XMLNS_NAMESPACE:
parser.fail(local === "" ?
`the default namespace may not be set to ${uri}.` :
`may not assign a prefix (even "xmlns") to the URI \
switch (uri) {
case XMLNS_NAMESPACE:
parser.fail(prefix === "" ?
`the default namespace may not be set to ${uri}.` :
`may not assign a prefix (even "xmlns") to the URI \
${XMLNS_NAMESPACE}.`);
break;
case XML_NAMESPACE:
switch (prefix) {
case "xml":
// Assinging the XML namespace to "xml" is fine.
break;
case XML_NAMESPACE:
switch (local) {
case "xml":
// Assinging the XML namespace to "xml" is fine.
break;
case "":
parser.fail(`the default namespace may not be set to ${uri}.`);
break;
default:
parser.fail("may not assign the xml namespace to another prefix.");
}
case "":
parser.fail(`the default namespace may not be set to ${uri}.`);
break;
default:
parser.fail("may not assign the xml namespace to another prefix.");
}
break;
default:
}
}


function nsMappingCheck(parser, mapping) {
for (const local of Object.keys(mapping)) {
nsPairCheck(parser, local, mapping[local]);
}
}

Expand Down Expand Up @@ -1341,10 +1350,14 @@ class SaxesParser {
const { prefix, local } = this.qname(name);
this.attribList.push({ name, prefix, local, value, uri: undefined });
if (prefix === "xmlns") {
this.tag.ns[local] = value.trim();
const trimmed = value.trim();
this.tag.ns[local] = trimmed;
nsPairCheck(this, local, trimmed);
}
else if (name === "xmlns") {
this.tag.ns[""] = value.trim();
const trimmed = value.trim();
this.tag.ns[""] = trimmed;
nsPairCheck(this, "", trimmed);
}
}

Expand Down Expand Up @@ -1689,9 +1702,7 @@ class SaxesParser {
/** @private */
processAttribsNS() {
const { tag, attribList } = this;
const { name: tagName, attributes, ns } = tag;

nsMappingCheck(this, ns);
const { name: tagName, attributes } = tag;

{
// add namespace info to tag
Expand Down
2 changes: 1 addition & 1 deletion test/xmlns-xml-default-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe("xml default prefix", () => {
],
[
"error",
"undefined:1:29: xml prefix must be bound to \
"undefined:1:27: xml prefix must be bound to \
http://www.w3.org/XML/1998/namespace.",
],
[
Expand Down

0 comments on commit 738150d

Please sign in to comment.