moddle-xml
Advanced tools
Comparing version 0.9.3 to 0.9.4
@@ -30,2 +30,18 @@ 'use strict'; | ||
function getNsAttrs(namespaces) { | ||
function isUsed(ns) { | ||
return namespaces.used[ns.uri]; | ||
} | ||
function toAttr(ns) { | ||
var name = 'xmlns' + (ns.prefix ? ':' + ns.prefix : ''); | ||
return { name: name, value: ns.uri }; | ||
} | ||
var allNs = [].concat(namespaces.wellknown, namespaces.custom); | ||
return map(filter(allNs, isUsed), toAttr); | ||
} | ||
function getElementNs(ns, descriptor) { | ||
@@ -277,2 +293,4 @@ if (descriptor.isGeneric) { | ||
var model = element.$model; | ||
var attributes = []; | ||
@@ -286,7 +304,22 @@ | ||
var ns; | ||
// parse xmlns:foo="http://foo.bar" | ||
if (nameNs.prefix === 'xmlns') { | ||
self.logNamespace({ prefix: nameNs.localName, uri: value }); | ||
} else | ||
ns = { prefix: nameNs.localName, uri: value }; | ||
} | ||
// parse xmlns="http://foo.bar" | ||
if (!nameNs.prefix && nameNs.localName === 'xmlns') { | ||
self.logNamespace({ uri: value }); | ||
ns = { uri: value }; | ||
} | ||
if (ns) { | ||
if (model.getPackage(value)) { | ||
// register well known namespace | ||
self.logNamespace(ns, true); | ||
} else { | ||
// log custom namespace directly as used | ||
self.logNamespaceUsed(ns); | ||
} | ||
} else { | ||
@@ -374,27 +407,37 @@ attributes.push({ name: name, value: value }); | ||
ElementSerializer.prototype.getNamespaces = function() { | ||
if (!this.parent) { | ||
if (!this.namespaces) { | ||
this.namespaces = { | ||
prefixMap: {}, | ||
uriMap: {}, | ||
used: {} | ||
}; | ||
} | ||
} else { | ||
this.namespaces = this.parent.getNamespaces(); | ||
var namespaces = this.namespaces, | ||
parent = this.parent; | ||
if (!namespaces) { | ||
namespaces = this.namespaces = parent ? parent.getNamespaces() : { | ||
prefixMap: {}, | ||
uriMap: {}, | ||
used: {}, | ||
wellknown: [], | ||
custom: [] | ||
}; | ||
} | ||
return this.namespaces; | ||
return namespaces; | ||
}; | ||
ElementSerializer.prototype.logNamespace = function(ns) { | ||
ElementSerializer.prototype.logNamespace = function(ns, wellknown) { | ||
var namespaces = this.getNamespaces(); | ||
var existing = namespaces.uriMap[ns.uri]; | ||
var nsUri = ns.uri; | ||
var existing = namespaces.uriMap[nsUri]; | ||
if (!existing) { | ||
namespaces.uriMap[ns.uri] = ns; | ||
namespaces.uriMap[nsUri] = ns; | ||
if (wellknown) { | ||
namespaces.wellknown.push(ns); | ||
} else { | ||
namespaces.custom.push(ns); | ||
} | ||
} | ||
namespaces.prefixMap[ns.prefix] = ns.uri; | ||
namespaces.prefixMap[ns.prefix] = nsUri; | ||
@@ -415,5 +458,7 @@ return ns; | ||
var prefix = ns.prefix; | ||
var uri = ns.uri || DEFAULT_NS_MAP[prefix] || | ||
namespaces.prefixMap[prefix] || (model ? (model.getPackage(prefix) || {}).uri : null); | ||
var wellknownUri = DEFAULT_NS_MAP[prefix] || model && (model.getPackage(prefix) || {}).uri; | ||
var uri = ns.uri || namespaces.prefixMap[prefix] || wellknownUri; | ||
if (!uri) { | ||
@@ -426,3 +471,3 @@ throw new Error('no namespace uri given for prefix <' + ns.prefix + '>'); | ||
if (!ns) { | ||
ns = this.logNamespace({ prefix: prefix, uri: uri }); | ||
ns = this.logNamespace({ prefix: prefix, uri: uri }, wellknownUri); | ||
} | ||
@@ -477,14 +522,6 @@ | ||
var attrs = this.attrs, | ||
root = !this.parent, | ||
namespaces = this.namespaces; | ||
root = !this.parent; | ||
function collectNsAttrs() { | ||
return map(namespaces.used, function(ns) { | ||
var name = 'xmlns' + (ns.prefix ? ':' + ns.prefix : ''); | ||
return { name: name, value: ns.uri }; | ||
}); | ||
} | ||
if (root) { | ||
attrs = collectNsAttrs().concat(attrs); | ||
attrs = getNsAttrs(this.namespaces).concat(attrs); | ||
} | ||
@@ -491,0 +528,0 @@ |
{ | ||
"name": "moddle-xml", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"description": "XML import/export for documents described with moddle", | ||
@@ -43,2 +43,3 @@ "directories": { | ||
"grunt": "~0.4.2", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-connect": "~0.6.0", | ||
@@ -45,0 +46,0 @@ "grunt-contrib-jshint": "~0.7.2", |
@@ -879,2 +879,23 @@ 'use strict'; | ||
it('should write manually added custom namespace', function() { | ||
// given | ||
var writer = createWriter(extensionModel); | ||
var root = extensionModel.create('e:Root', { | ||
'xmlns:foo': 'http://fooo' | ||
}); | ||
// when | ||
var xml = writer.toXML(root); | ||
var expectedXml = | ||
'<e:root xmlns:e="http://extensions" ' + | ||
'xmlns:foo="http://fooo" />'; | ||
// then | ||
expect(xml).to.eql(expectedXml); | ||
}); | ||
it('should ignore unknown namespace prefix', function() { | ||
@@ -881,0 +902,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122667
3512
10