Skip to content

Commit 42eaedd

Browse files
Scott J Mileskevinpschaaf
Scott J Miles
authored andcommitted
* remove bogus support for arbitrary JSON in literal arguments, literal arguments must now be either String or Number
* support using escaped-commas in literal-string arguments: `compute("foo\,bar", zot, 3)` * support other character escaping which is naturally broken by HTML parser `compute('foo\'bar')` * test for literal argument _before_ looking for structured args
1 parent 0fe9fe7 commit 42eaedd

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

src/standard/effectBuilder.html

+36-34
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,13 @@
168168
_parseMethod: function(expression) {
169169
var m = expression.match(/(\w*)\((.*)\)/);
170170
if (m) {
171-
return this._parseArgs({
172-
method: m[1],
173-
static: true
174-
}, m[2].split(','));
171+
// replace escaped commas with comma entity, split on un-escaped commas
172+
var args = m[2].replace(/\\,/g, ',').split(',');
173+
return this._parseArgs(args, { method: m[1], static: true });
175174
}
176175
},
177176

178-
_parseArgs: function(effect, argList) {
177+
_parseArgs: function(argList, effect) {
179178
effect.args = argList.map(function(rawArg) {
180179
var arg = this._parseArg(rawArg);
181180
if (!arg.literal) {
@@ -187,40 +186,43 @@
187186
},
188187

189188
_parseArg: function(rawArg) {
190-
// clean up string
191-
var arg = String(rawArg).trim();
189+
// clean up whitespace
190+
var arg = String(rawArg).trim()
191+
// replace comma entity with comma
192+
.replace(/,/g, ',')
193+
// repair escape sequences
194+
.replace(/\\(.)/g, '\$1')
195+
;
192196
// basic argument descriptor
193197
var a = {
194198
name: arg,
195199
model: this._modelForPath(arg)
196200
};
197-
// detect structured path (has dots)
198-
a.structured = arg.indexOf('.') > 0;
199-
if (a.structured) {
200-
a.wildcard = (arg.slice(-2) == '.*');
201-
if (a.wildcard) {
202-
a.name = arg.slice(0, -2);
203-
}
204-
} else {
205-
// detect literal value (must be JSON)
206-
var fc = arg[0];
207-
if (fc >= '0' && fc <= '9') {
208-
fc = '#';
209-
}
210-
switch(fc) {
211-
// TODO(sjmiles): single-quote is invalid JSON
212-
//case "'":
213-
case '"':
214-
case '{':
215-
case '[':
216-
case '#':
217-
try {
218-
a.value = JSON.parse(arg);
219-
a.literal = true;
220-
} catch(x) {
221-
// warn?
222-
}
223-
break;
201+
// detect literal value (must be String or Number)
202+
var fc = arg[0];
203+
if (fc >= '0' && fc <= '9') {
204+
fc = '#';
205+
}
206+
switch(fc) {
207+
case "'":
208+
case '"':
209+
a.value = arg.slice(1, -1);
210+
a.literal = true;
211+
break;
212+
case '#':
213+
a.value = Number(arg);
214+
a.literal = true;
215+
break;
216+
}
217+
// if not literal, look for structured path
218+
if (!a.literal) {
219+
// detect structured path (has dots)
220+
a.structured = arg.indexOf('.') > 0;
221+
if (a.structured) {
222+
a.wildcard = (arg.slice(-2) == '.*');
223+
if (a.wildcard) {
224+
a.name = arg.slice(0, -2);
225+
}
224226
}
225227
}
226228
return a;

test/unit/bind-elements.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
custom-event-value="{{customEventValue::custom}}"
1616
custom-event-object-value="{{customEventObject.value::change}}"
1717
computed-from-mixed-literals='{{computeFromLiterals(3, "foo", bool)}}'
18-
computed-from-pure-literals='{{computeFromLiterals(3, "foo")}}'
18+
computed-from-pure-literals='{{computeFromLiterals( 3, "foo")}}'
19+
computed-from-tricky-literals="{{computeFromTrickyLiterals(3, 'tricky\,\'zot\'')}}"
20+
computed-from-tricky-literals2='{{computeFromTrickyLiterals(3,"tricky\,\'zot\'" )}}'
1921
>
2022
Test
2123
</div>
2224
<span id="boundText">{{text}}</span>
2325
<span idtest id="{{boundId}}"></span>
26+
<s id="computedContent">{{computeFromTrickyLiterals(3, 'tricky\,\'zot\'')}}</s>
2427
</template>
2528
<script>
2629
Polymer({
@@ -192,6 +195,9 @@
192195
assert.equal(num, 3);
193196
assert.equal(str, 'foo');
194197
return num + str;
198+
},
199+
computeFromTrickyLiterals: function(a, b) {
200+
return a + b;
195201
}
196202
});
197203
</script>

test/unit/bind.html

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
el.bool = true;
143143
assert.equal(el.$.boundChild.computedFromMixedLiterals, '3foo', 'Wrong result from mixed literal arg computation');
144144
assert.equal(el.$.boundChild.computedFromPureLiterals, '3foo', 'Wrong result from pure literal arg computation');
145+
assert.equal(el.$.boundChild.computedFromTrickyLiterals, '3tricky,\'zot\'', 'Wrong result from tricky literal arg computation');
146+
assert.equal(el.$.computedContent.textContent, '3tricky,\'zot\'', 'Wrong textContent from tricky literal arg computation');
145147
});
146148

147149
test('no read-only observer called with assignment', function() {

0 commit comments

Comments
 (0)