Skip to content

Commit

Permalink
chore(write): split up generic parsing into ns + containments
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku authored and fake-join[bot] committed Jan 10, 2023
1 parent 7ccb4e9 commit d7676f8
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions lib/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ ElementSerializer.prototype.build = function(element) {
var isGeneric = elementDescriptor.isGeneric;

if (isGeneric) {
otherAttrs = this.parseGeneric(element);
otherAttrs = this.parseGenericNsAttributes(element);
} else {
otherAttrs = this.parseNsAttributes(element);
}
Expand All @@ -325,7 +325,9 @@ ElementSerializer.prototype.build = function(element) {
// compute tag name
this.tagName = this.addTagName(this.ns);

if (!isGeneric) {
if (isGeneric) {
this.parseGenericContainments(element);
} else {
properties = getSerializableProperties(element);

this.parseAttributes(filterAttributes(properties));
Expand Down Expand Up @@ -388,35 +390,29 @@ ElementSerializer.prototype.nsAttributeName = function(element) {
}
};

ElementSerializer.prototype.parseGeneric = function(element) {
ElementSerializer.prototype.parseGenericNsAttributes = function(element) {

var self = this,
body = this.body;

var attributes = [];

forEach(element, function(val, key) {
return Object.entries(element).filter(
([ key, value ]) => !key.startsWith('$') && this.parseNsAttribute(element, key, value)
).map(
([ key, value ]) => ({ name: key, value: value })
);
};

var nonNsAttr;
ElementSerializer.prototype.parseGenericContainments = function(element) {
var body = element.$body;

if (key === '$body') {
body.push(new BodySerializer().build({ type: 'String' }, val));
} else
if (key === '$children') {
forEach(val, function(child) {
body.push(new ElementSerializer(self).build(child));
});
} else
if (key.indexOf('$') !== 0) {
nonNsAttr = self.parseNsAttribute(element, key, val);
if (body) {
this.body.push(new BodySerializer().build({ type: 'String' }, body));
}

if (nonNsAttr) {
attributes.push({ name: key, value: val });
}
}
});
var children = element.$children;

return attributes;
if (children) {
forEach(children, child => {
this.body.push(new ElementSerializer(this).build(child));
});
}
};

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

0 comments on commit d7676f8

Please sign in to comment.