Skip to content

Commit

Permalink
feat(parser): support anonymous namespaces
Browse files Browse the repository at this point in the history
This allow anonymous namespace elements to be imported without
an error being thrown.

The parser makes no correction of the prefix according to a
defined document-wide targetNamespace. It is up to the
sophisticated client library to perform that adjustment.

This partically reverts e876f1f.
  • Loading branch information
nikku committed Nov 9, 2017
1 parent a5014b2 commit 2f48744
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
29 changes: 15 additions & 14 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ function Saxen(options) {
i = 0, j = 0,
x, y, q, w,
xmlns,
xmlnsStack = isNamespace ? [] : null,
_xmlns,
elementName,
_elementName,
elementProxy
Expand Down Expand Up @@ -832,14 +830,12 @@ function Saxen(options) {
if (isNamespace) {

_nsMatrix = nsMatrix;
_xmlns = xmlns;

if (tagStart) {
// remember old namespace
// unless we're self-closing
if (!tagEnd) {
nsMatrixStack.push(_nsMatrix);
xmlnsStack.push(xmlns);
}

if (cachedAttrs !== true) {
Expand All @@ -862,22 +858,29 @@ function Saxen(options) {
w = elementName.indexOf(':');
if (w !== -1) {
xmlns = nsMatrix[elementName.substring(0, w)];

// prefix given; namespace must exist
if (!xmlns) {
return handleError('missing namespace on <' + _elementName + '>');
}

elementName = elementName.substr(w + 1);
} else {
xmlns = nsMatrix['xmlns'];

if (!xmlns && _xmlns) {
// if no default xmlns is defined,
// inherit xmlns from parent
xmlns = _xmlns;
}
// if no default namespace is defined,
// we'll import the element as anonymous.
//
// it is up to users to correct that to the document defined
// targetNamespace, or whatever their undersanding of the
// XML spec mandates.
}

if (!xmlns) {
return handleError('missing namespace on <' + _elementName + '>');
// adjust namespace prefixs as configured
if (xmlns) {
elementName = xmlns + ':' + elementName;
}

elementName = xmlns + ':' + elementName;
}

if (tagStart) {
Expand Down Expand Up @@ -908,10 +911,8 @@ function Saxen(options) {
if (isNamespace) {
if (!tagStart) {
nsMatrix = nsMatrixStack.pop();
xmlns = xmlnsStack.pop();
} else {
nsMatrix = _nsMatrix;
xmlns = _xmlns;
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions test/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ test({
]
});

// namespaces
test({
xml: '<xmlns/>',
ns: true,
expect: [
['openTag', 'xmlns'],
['closeTag', 'xmlns'],
]
});

test({
xml: '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" id="aa" media:title="bb"/>',
ns: true,
Expand Down Expand Up @@ -629,8 +639,8 @@ test({
expect: [
['openTag', 'foo:root'],
['openTag', 'bar:outer'],
['openTag', 'bar:nested'],
['closeTag', 'bar:nested'],
['openTag', 'nested'],
['closeTag', 'nested'],
['closeTag', 'bar:outer'],
['closeTag', 'foo:root'],
],
Expand Down

0 comments on commit 2f48744

Please sign in to comment.