Skip to content

Commit d7676f8

Browse files
nikkufake-join[bot]
authored andcommitted
chore(write): split up generic parsing into ns + containments
1 parent 7ccb4e9 commit d7676f8

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

lib/write.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ ElementSerializer.prototype.build = function(element) {
311311
var isGeneric = elementDescriptor.isGeneric;
312312

313313
if (isGeneric) {
314-
otherAttrs = this.parseGeneric(element);
314+
otherAttrs = this.parseGenericNsAttributes(element);
315315
} else {
316316
otherAttrs = this.parseNsAttributes(element);
317317
}
@@ -325,7 +325,9 @@ ElementSerializer.prototype.build = function(element) {
325325
// compute tag name
326326
this.tagName = this.addTagName(this.ns);
327327

328-
if (!isGeneric) {
328+
if (isGeneric) {
329+
this.parseGenericContainments(element);
330+
} else {
329331
properties = getSerializableProperties(element);
330332

331333
this.parseAttributes(filterAttributes(properties));
@@ -388,35 +390,29 @@ ElementSerializer.prototype.nsAttributeName = function(element) {
388390
}
389391
};
390392

391-
ElementSerializer.prototype.parseGeneric = function(element) {
393+
ElementSerializer.prototype.parseGenericNsAttributes = function(element) {
392394

393-
var self = this,
394-
body = this.body;
395-
396-
var attributes = [];
397-
398-
forEach(element, function(val, key) {
395+
return Object.entries(element).filter(
396+
([ key, value ]) => !key.startsWith('$') && this.parseNsAttribute(element, key, value)
397+
).map(
398+
([ key, value ]) => ({ name: key, value: value })
399+
);
400+
};
399401

400-
var nonNsAttr;
402+
ElementSerializer.prototype.parseGenericContainments = function(element) {
403+
var body = element.$body;
401404

402-
if (key === '$body') {
403-
body.push(new BodySerializer().build({ type: 'String' }, val));
404-
} else
405-
if (key === '$children') {
406-
forEach(val, function(child) {
407-
body.push(new ElementSerializer(self).build(child));
408-
});
409-
} else
410-
if (key.indexOf('$') !== 0) {
411-
nonNsAttr = self.parseNsAttribute(element, key, val);
405+
if (body) {
406+
this.body.push(new BodySerializer().build({ type: 'String' }, body));
407+
}
412408

413-
if (nonNsAttr) {
414-
attributes.push({ name: key, value: val });
415-
}
416-
}
417-
});
409+
var children = element.$children;
418410

419-
return attributes;
411+
if (children) {
412+
forEach(children, child => {
413+
this.body.push(new ElementSerializer(this).build(child));
414+
});
415+
}
420416
};
421417

422418
ElementSerializer.prototype.parseNsAttribute = function(element, name, value) {

0 commit comments

Comments
 (0)