Skip to content

Commit b4f9720

Browse files
committed
don't serialize properties whose original value is object; slight simplification
1 parent 6958462 commit b4f9720

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/instance/attributes.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,30 @@
6262
deserializeValue: function(stringValue, defaultValue) {
6363
return scope.deserializeValue(stringValue, defaultValue);
6464
},
65-
serializeValue: function(value) {
66-
if (typeof value != 'object' && value !== undefined) {
65+
serializeValue: function(value, inferredType) {
66+
if (inferredType === 'boolean') {
67+
return value ? '' : undefined;
68+
} else if (inferredType !== 'object' && typeof value !== 'object' &&
69+
value !== undefined) {
6770
return value;
6871
}
6972
},
7073
propertyToAttribute: function(name) {
7174
if (Object.keys(this[PUBLISHED]).indexOf(name) >= 0) {
72-
var serializedValue = this.serializeValue(this[name]);
75+
var inferredType = typeof this.__proto__[name];
76+
var serializedValue = this.serializeValue(this[name], inferredType);
7377
// boolean properties must reflect as boolean attributes
74-
if (typeof this.__proto__[name] === 'boolean') {
75-
if (serializedValue) {
76-
this.setAttribute(name, '');
77-
} else {
78-
this.removeAttribute(name);
79-
}
80-
} else if (serializedValue !== undefined) {
78+
if (serializedValue !== undefined) {
8179
this.setAttribute(name, serializedValue);
80+
// TODO(sorvell): we should remove attr for all properties
81+
// that have undefined serialization; however, we will need to
82+
// refine the attr reflection system to achieve this; pica, for example,
83+
// relies on having inferredType object properties not removed as
84+
// attrs.
85+
} else if (inferredType === 'boolean') {
86+
this.removeAttribute(name);
8287
}
88+
8389
}
8490
}
8591
};

test/html/prop-attr-reflection.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
<x-foo></x-foo>
1212
<polymer-element name="x-foo" attributes="foo baz">
1313
<script>
14-
Polymer('x-foo');
14+
Polymer('x-foo', {
15+
foo: '',
16+
baz: ''
17+
});
1518
</script>
1619
</polymer-element>
1720

@@ -69,12 +72,12 @@
6972
Platform.endOfMicrotask(function() {
7073
assert.equal(xbar.foo, xbar.getAttribute('foo'), 'inherited published property is reflected');
7174
assert.equal(String(xbar.zot), xbar.getAttribute('zot'), 'attribute reflects property as number');
72-
assert.equal('', xbar.getAttribute('zim'), 'attribute reflects true valued boolean property as having attribute');
75+
assert.equal(xbar.getAttribute('zim'), '', 'attribute reflects true valued boolean property as having attribute');
7376
assert.equal(xbar.str, xbar.getAttribute('str'), 'attribute reflects property as published string');
7477
assert.isFalse(xbar.hasAttribute('obj'), 'attribute does not reflect object property');
78+
xbar.setAttribute('zim', 'false');
7579
xbar.setAttribute('foo', 'foo!!');
7680
xbar.setAttribute('zot', 54);
77-
xbar.setAttribute('zim', 'false');
7881
xbar.setAttribute('str', 'str!!');
7982
xbar.setAttribute('obj', "{'hello': 'world'}");
8083
assert.equal(xbar.foo, xbar.getAttribute('foo'), 'property reflects attribute as string');
@@ -86,7 +89,13 @@
8689
Platform.flush();
8790
Platform.endOfMicrotask(function() {
8891
assert.isFalse(xbar.hasAttribute('zim'), 'attribute reflects false valued boolean property as NOT having attribute');
89-
done();
92+
var objAttr = xbar.getAttribute('obj');
93+
xbar.obj = 'hi';
94+
Platform.endOfMicrotask(function() {
95+
assert.equal(xbar.getAttribute('obj'), objAttr, 'do not reflect property with default type of object');
96+
//assert.isFalse(xbar.hasAttribute('obj'), 'property with default type of object does not serialize');
97+
done();
98+
});
9099
});
91100
});
92101
});

0 commit comments

Comments
 (0)