Skip to content

Commit

Permalink
feat(parser): normalize xsi:type without ns declarations
Browse files Browse the repository at this point in the history
Related to #4
  • Loading branch information
nikku committed Nov 8, 2017
1 parent 0a96ae4 commit ab3ee63
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
34 changes: 26 additions & 8 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function Saxen(options) {
var nsUri,
nsUriPrefix,
nsName,
defaultAlias,
defaultAlias = isNamespace && nsMatrix['xmlns'],
attrList = isNamespace && maybeNS ? [] : null,
i = attrsStart,
s = attrsString,
Expand Down Expand Up @@ -471,6 +471,7 @@ function Saxen(options) {
nsMatrix[newalias] = alias;
if (newalias === 'xmlns') {
nsMatrix[uriPrefix(alias)] = nsUri;
defaultAlias = alias;
}

nsMatrix[nsUriPrefix] = nsUri;
Expand All @@ -485,8 +486,11 @@ function Saxen(options) {
// declarations are processed
attrList.push(name, value);
continue;
}

} /** end if (maybeNs) */

// handle attributes on element without
// namespace declarations
w = name.indexOf(':');
if (w === -1) {
attrs[name] = value;
Expand All @@ -499,12 +503,27 @@ function Saxen(options) {
continue;
}

attrs[
nsMatrix['xmlns'] === nsName
? name.substr(w + 1)
: nsName + name.substr(w)
] = value;
name = defaultAlias === nsName
? name.substr(w + 1)
: nsName + name.substr(w);
// end: normalize ns attribute name

// normalize xsi:type ns attribute value
if (name === XSI_TYPE) {
w = value.indexOf(':');

if (w !== -1) {
nsName = value.substring(0, w);
// handle default prefixes, i.e. xs:String gracefully
nsName = nsMatrix[nsName] || nsName;
value = nsName + value.substring(w);
} else {
value = defaultAlias + ':' + value;
}
}
// end: normalize xsi:type ns attribute value

attrs[name] = value;
}


Expand All @@ -515,7 +534,6 @@ function Saxen(options) {

// handle deferred, possibly namespaced attributes
if (maybeNS) {
defaultAlias = nsMatrix['xmlns'];

// normalize captured attributes
for (i = 0, l = attrList.length; i < l; i++) {
Expand Down
48 changes: 48 additions & 0 deletions test/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,54 @@ test({
],
});

// normalize nested xsi:type
test({
xml: (
'<foo xmlns="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<bar xsi:type="Bar" />' +
'</foo>'
),
ns: true,
to: [
['openTag', 'ns0:foo' ],
['openTag', 'ns0:bar', { 'xsi:type': 'ns0:Bar' } ],
['closeTag', 'ns0:bar'],
['closeTag', 'ns0:foo'],
],
});

// normalize nested prefixed xsi:type
test({
xml: (
'<foo xmlns="http://foo" xmlns:bar="http://bar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<bar xsi:type="bar:Bar" />' +
'</foo>'
),
ns: true,
to: [
['openTag', 'ns0:foo' ],
['openTag', 'ns0:bar', { 'xsi:type': 'bar:Bar' } ],
['closeTag', 'ns0:bar'],
['closeTag', 'ns0:foo'],
],
});

// normalize nested xsi:type / preserve unknown prefix in value
test({
xml: (
'<foo xmlns="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
'<bar xsi:type="xs:string" />' +
'</foo>'
),
ns: true,
to: [
['openTag', 'ns0:foo' ],
['openTag', 'ns0:bar', { 'xsi:type': 'xs:string' } ],
['closeTag', 'ns0:bar'],
['closeTag', 'ns0:foo'],
],
});

// unmapped prefix
test({
xml: (
Expand Down

0 comments on commit ab3ee63

Please sign in to comment.