Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
allow serialization of json properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Jun 18, 2014
1 parent d7cc11c commit c3b9c8d
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions elements/dom-serializer/dom-serializer.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
var html = '';
html += indent + '<' + node.localName;
html += this.dumpAttributes(node);
html += this.dumpAttributes(node, indent);
html += '>';
var end = {input: 1}[node.localName] ? '\n' : '</' + node.localName + '>\n';
if (node._designMeta && node._designMeta.hideSubtree) {
Expand All @@ -42,9 +42,9 @@
return html;
},

dumpAttributes: function(node) {
dumpAttributes: function(node, indent) {
var html = '';
var attributes = this.calcAttributeValues(node);
var attributes = this.calcAttributeValues(node, indent);
Object.keys(attributes).forEach(function(k) {
var v = attributes[k];
if (v !== null && v !== undefined) {
Expand All @@ -63,7 +63,7 @@
return html;
},

calcAttributeValues: function(node) {
calcAttributeValues: function(node, indent) {
var p$ = Reflection.properties(node);
var attributes = {};
p$.forEach(function(p) {
Expand All @@ -80,10 +80,11 @@
try {
defaultValue = node.__proto__[n];
} catch(e) {}
if (!this.shouldSerializeProperty(v, defaultValue)) {
if (!this.shouldSerializeProperty(v, defaultValue, p.meta)) {
v = null;
}
attributes[n.toLowerCase()] = v;
attributes[n.toLowerCase()] = v === null ? v :
this.serializeAttributeValue(v, indent);
}, this);
for (var i=0, a, n; (a = node.attributes[i]); i++) {
n = a.name.toLowerCase();
Expand All @@ -95,11 +96,23 @@
return attributes;
},

shouldSerializeProperty: function(value, defaultValue) {
return (value !== defaultValue)
&& (value || value === 0)
&& (typeof value === 'string' || typeof value === 'number' ||
typeof value === 'boolean');
shouldSerializeProperty: function(value, defaultValue, meta) {
// must be valued
var valued = (value !== defaultValue) && (value || value === 0);
if (!valued) {
return;
}
// must be proper type
return (typeof value === 'string' || typeof value === 'number' ||
typeof value === 'boolean') || (typeof value === 'object' &&
meta && meta.kind === 'json');
},

serializeAttributeValue: function(value, indent) {
if (typeof value === 'object') {
value = JSON.stringify(value);
}
return value;
},

// special handling for certain attributes: style, class
Expand Down Expand Up @@ -287,8 +300,7 @@
var imports = c.querySelectorAll('link[rel=import]').array();
for (var i=0, l=imports.length, p, h; (i<l) && (p=imports[i]); i++) {
// make serializable href
h = new URL(p.getAttribute('href'), t.ownerDocument.baseURI);
h = h.href.replace(basePath, '').replace('/components', '..');
h = p.getAttribute('href').replace('components', '..');
p.setAttribute('href', h);
}
return imports;
Expand Down

0 comments on commit c3b9c8d

Please sign in to comment.