Skip to content

Commit 36420cf

Browse files
nikkufake-join[bot]
authored andcommitted
fix(write): correct export of generic element
* Properly read parent declared namespace * Properly register local non-prefix namespace
1 parent 5bac2a3 commit 36420cf

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

lib/write.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ Namespaces.prototype.add = function(ns, isWellknown) {
6666
};
6767

6868
Namespaces.prototype.uriByPrefix = function(prefix) {
69-
return this.prefixMap[prefix || 'xmlns'];
69+
return this.prefixMap[prefix || 'xmlns'] || (
70+
this.parent && this.parent.uriByPrefix(prefix)
71+
);
7072
};
7173

7274
Namespaces.prototype.mapPrefix = function(prefix, uri) {
@@ -463,7 +465,7 @@ ElementSerializer.prototype.parseNsAttribute = function(element, name, value) {
463465
* @param {Object} element
464466
* @return {Array<Object>}
465467
*/
466-
ElementSerializer.prototype.parseNsAttributes = function(element, attrs) {
468+
ElementSerializer.prototype.parseNsAttributes = function(element) {
467469
var self = this;
468470

469471
var genericAttrs = element.$attrs;
@@ -622,6 +624,11 @@ ElementSerializer.prototype.logNamespaceUsed = function(ns, local) {
622624

623625
ns = namespaces.byUri(uri);
624626

627+
// register new default prefix <xmlns> in local scope
628+
if (!ns && !prefix) {
629+
ns = this.logNamespace({ uri }, wellknownUri === uri, true);
630+
}
631+
625632
if (!ns) {
626633
newPrefix = prefix;
627634
idx = 1;
@@ -653,8 +660,7 @@ ElementSerializer.prototype.parseAttributes = function(properties) {
653660

654661
if (!p.isMany) {
655662
value = value.id;
656-
}
657-
else {
663+
} else {
658664
var values = [];
659665
forEach(value, function(v) {
660666
values.push(v.id);

test/spec/rountrip.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,33 @@ describe('Roundtrip', function() {
175175

176176
describe('generic', function() {
177177

178+
it('should keep local ns attribute', async function() {
179+
180+
// given
181+
var extendedModel = createModel([ 'extensions' ]);
182+
183+
var reader = new Reader(extendedModel);
184+
var writer = createWriter(extendedModel);
185+
186+
var rootHandler = reader.handler('e:Root');
187+
188+
var input =
189+
'<e:root xmlns:e="http://extensions" xmlns:woop="https://woop">' +
190+
'<Bar xmlns="http://foobar">' +
191+
'<Foo woop:boop="Some" />' +
192+
'</Bar>' +
193+
'</e:root>';
194+
195+
// when
196+
var {
197+
rootElement
198+
} = await reader.fromXML(input, rootHandler);
199+
200+
var output = writer.toXML(rootElement);
201+
202+
// then
203+
expect(output).to.eql(input);
204+
});
178205

179206

180207
it('should keep local <xsi:type>', async function() {

0 commit comments

Comments
 (0)