Skip to content

Commit 72d35e0

Browse files
committed
Do not reflect uppercase properties
Add tests for warning on uppercase properties and attribute bindings beginning with '-' Fixes #3500
1 parent 8066919 commit 72d35e0

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

src/standard/effectBuilder.html

+25-16
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@
6565
event: Polymer.CaseMap.camelToDashCase(p) + '-changed'});
6666
}
6767
if (prop.reflectToAttribute) {
68-
this._addPropertyEffect(p, 'reflect', {
69-
attribute: Polymer.CaseMap.camelToDashCase(p)
70-
});
68+
var attr = Polymer.CaseMap.camelToDashCase(p);
69+
if (attr[0] === '-') {
70+
this._warn(this._logf('_addPropertyEffects', 'Property ' + p + ' cannot be reflected to attribute ' + attr + ' because "-" is not a valid starting attribute name. Use a lowercase first letter for the property instead.'));
71+
} else {
72+
this._addPropertyEffect(p, 'reflect', {
73+
attribute: attr
74+
});
75+
}
7176
}
7277
if (prop.readOnly) {
7378
// Ensure accessor is created
@@ -98,7 +103,7 @@
98103
trigger: null,
99104
name: name,
100105
dynamicFn: dynamicFn
101-
})
106+
});
102107
}
103108
},
104109

@@ -168,18 +173,22 @@
168173
this._addAnnotatedComputationEffect(note, part, index);
169174
} else if (!part.literal) {
170175
// add 'annotation' binding effect for property 'model'
171-
this._addPropertyEffect(part.model, 'annotation', {
172-
kind: note.kind,
173-
index: index,
174-
name: note.name,
175-
propertyName: note.propertyName,
176-
value: part.value,
177-
isCompound: note.isCompound,
178-
compoundIndex: part.compoundIndex,
179-
event: part.event,
180-
customEvent: part.customEvent,
181-
negate: part.negate
182-
});
176+
if (note.kind === 'attribute' && note.name[0] === '-') {
177+
this._warn(this._logf('_addAnnotationEffect', 'Cannot set attribute ' + note.name + ' because "-" is not a valid attribute starting character'));
178+
} else {
179+
this._addPropertyEffect(part.model, 'annotation', {
180+
kind: note.kind,
181+
index: index,
182+
name: note.name,
183+
propertyName: note.propertyName,
184+
value: part.value,
185+
isCompound: note.isCompound,
186+
compoundIndex: part.compoundIndex,
187+
event: part.event,
188+
customEvent: part.customEvent,
189+
negate: part.negate
190+
});
191+
}
183192
}
184193
}
185194
},

test/unit/attributes-elements.html

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
type: String,
4545
value: 'none'
4646
},
47+
UPCASE: {
48+
type: String,
49+
value: 'none'
50+
},
4751
noType: {
4852
value: 'none'
4953
},
@@ -120,6 +124,11 @@
120124
reflectToAttribute: true,
121125
value: 'none'
122126
},
127+
UPCASE: {
128+
type: String,
129+
reflectToAttribute: true,
130+
value: 'none'
131+
},
123132
noType: {
124133
value: 'none'
125134
},
@@ -128,6 +137,12 @@
128137
value: 'default',
129138
readOnly: true
130139
}
140+
},
141+
_warn: function() {
142+
var search = Array.prototype.join.call(arguments, '');
143+
if (search.indexOf('UPCASE') > -1) {
144+
this.__warnedAboutUPCASE = true;
145+
}
131146
}
132147
});
133148
</script>

test/unit/attributes.html

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
string="The quick brown fox"
3030
bool
3131
date="Wed Mar 04 2015 10:46:05 GMT-0800 (PST)"
32+
-u-p-c-a-s-e="The quick brown fox"
3233
dash-case="The quick brown fox"
3334
no-type="Should be String"
3435
read-only="Should not change"
@@ -47,6 +48,7 @@
4748
bool
4849
date="Wed Mar 04 2015 10:46:05 GMT-0800 (PST)"
4950
dash-case="The quick brown fox"
51+
-u-p-c-a-s-e="The quick brown fox"
5052
no-type="Should be String"
5153
read-only="Should not change"
5254
class="Should not deserialize"
@@ -75,6 +77,7 @@
7577
assert.strictEqual(basicDefault.negBool, false);
7678
assert.strictEqual(basicDefault.date.getTime(), 0);
7779
assert.strictEqual(basicDefault.dashCase, 'none');
80+
assert.strictEqual(basicDefault.UPCASE, 'none');
7881
assert.strictEqual(basicDefault.noType, 'none');
7982
assert.strictEqual(basicDefault.readOnly, 'default');
8083
});
@@ -88,6 +91,7 @@
8891
assert.strictEqual(basicConfigured.negBool, false);
8992
assert.strictEqual(basicConfigured.date.getTime(), configuredTime);
9093
assert.strictEqual(basicConfigured.dashCase, configuredString);
94+
assert.strictEqual(basicConfigured.UPCASE, configuredString);
9195
assert.strictEqual(basicConfigured.noType, configuredNoType);
9296
assert.strictEqual(basicConfigured.readOnly, 'default');
9397
assert.strictEqual(basicConfigured.class, undefined);
@@ -103,6 +107,7 @@
103107
assert.strictEqual(reflectDefault.negBool, false);
104108
assert.strictEqual(reflectDefault.date.getTime(), 0);
105109
assert.strictEqual(reflectDefault.dashCase, 'none');
110+
assert.strictEqual(reflectDefault.UPCASE, 'none');
106111
assert.strictEqual(reflectDefault.noType, 'none');
107112
assert.strictEqual(reflectDefault.readOnly, 'default');
108113
});
@@ -116,12 +121,17 @@
116121
assert.strictEqual(reflectConfigured.negBool, false);
117122
assert.strictEqual(reflectConfigured.date.getTime(), configuredTime);
118123
assert.strictEqual(reflectConfigured.dashCase, configuredString);
124+
assert.strictEqual(reflectConfigured.UPCASE, configuredString);
119125
assert.strictEqual(reflectConfigured.noType, configuredNoType);
120126
assert.strictEqual(reflectConfigured.readOnly, 'default');
121127
assert.strictEqual(reflectConfigured.class, undefined);
122128
assert.strictEqual(reflectConfigured.nard, undefined);
123129
});
124130

131+
test('reflected warned about reflection for UPCASE', function() {
132+
assert.isTrue(reflectDefault.__warnedAboutUPCASE = true);
133+
});
134+
125135
});
126136

127137
suite('imperative attribute change (no-reflect)', function() {

test/unit/bind-elements.html

+23
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,26 @@
662662
});
663663
</script>
664664
</dom-module>
665+
666+
<dom-module id="x-bind-bad-attribute-name">
667+
<template>
668+
<div -u-p-c-a-s-e$="[[UPCASE]]"></div>
669+
</template>
670+
<script>
671+
Polymer({
672+
is: 'x-bind-bad-attribute-name',
673+
properties: {
674+
UPCASE: {
675+
type: String,
676+
value: 'foo'
677+
}
678+
},
679+
_warn: function() {
680+
var search = Array.prototype.join.call(arguments, '');
681+
if (search.indexOf('-u-p-c-a-s-e') > -1) {
682+
this.__warnedAboutUPCASE = true;
683+
}
684+
}
685+
});
686+
</script>
687+
</dom-module>

test/unit/bind.html

+5
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,11 @@
826826
assert.equal(warned, true, 'no warning for undefined computed function');
827827
});
828828

829+
test('binding to a bad attribute warns', function() {
830+
var el = document.createElement('x-bind-bad-attribute-name');
831+
assert.equal(el.__warnedAboutUPCASE, true, 'no warning for setting a bad attribute');
832+
});
833+
829834
});
830835

831836
suite('binding corner cases', function() {

0 commit comments

Comments
 (0)