Skip to content

Commit 4f4b0d2

Browse files
committed
Allow optional arguments in complex expressions.
1 parent 6e07d38 commit 4f4b0d2

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

src/lib/annotations/annotations.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
},
9191

9292
_bindingRegex: (function() {
93-
var IDENT = '(?:' + '[a-zA-Z_$][\\w.:$\\-*]*' + ')';
93+
var IDENT = '(?:' + '[?]?[a-zA-Z_$][\\w.:$\\-*]*' + ')';
9494
var NUMBER = '(?:' + '[-+]?[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?' + ')';
9595
var SQUOTE_STRING = '(?:' + '\'(?:[^\'\\\\]|\\\\.)*\'' + ')';
9696
var DQUOTE_STRING = '(?:' + '"(?:[^"\\\\]|\\\\.)*"' + ')';

src/lib/bind/effects.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
} else {
134134
v = model[name];
135135
}
136-
if (bailoutEarly && v === undefined) {
136+
if (!arg.optional && bailoutEarly && v === undefined) {
137137
return;
138138
}
139139
if (arg.wildcard) {

src/standard/effectBuilder.html

+4
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@
289289
}
290290
// if not literal, look for structured path
291291
if (!a.literal) {
292+
if (arg[0] === '?') {
293+
a.optional = true;
294+
a.name = arg = arg.slice(1);
295+
}
292296
a.model = this._modelForPath(arg);
293297
// detect structured path (has dots)
294298
a.structured = arg.indexOf('.') > 0;

test/unit/bind-elements.html

+17
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,20 @@
682682
})
683683
</script>
684684
</dom-module>
685+
686+
<dom-module id="x-optional-argument-on-computed-annotation">
687+
<template>
688+
<div id="check" prop="[[compute(?foo.bar, 'literal')]]"></div>
689+
</template>
690+
<script>
691+
Polymer({
692+
is: 'x-optional-argument-on-computed-annotation',
693+
properties: {
694+
foo: Object,
695+
},
696+
compute: function(bar, literal) {
697+
return bar + literal;
698+
}
699+
})
700+
</script>
701+
</dom-module>

test/unit/bind.html

+40-9
Original file line numberDiff line numberDiff line change
@@ -398,17 +398,10 @@
398398

399399
var el;
400400

401-
setup(function() {
402-
el = document.createElement('x-undefined-default-for-computeds');
403-
document.body.appendChild(el);
404-
});
405-
406-
teardown(function() {
407-
document.body.removeChild(el);
408-
});
409-
410401
test('if an argument becomes unreachable, set undefined (annotation)',
411402
function() {
403+
el = document.createElement('x-undefined-default-for-computeds');
404+
412405
el.foo = {bar: 'quux'};
413406
assert.equal(el.$.check.prop, 'quuxliteral');
414407

@@ -418,13 +411,51 @@
418411

419412
test('if an argument becomes unreachable, set undefined (computed)',
420413
function() {
414+
el = document.createElement('x-undefined-default-for-computeds');
415+
421416
el.foo = {bar: 'quux'};
422417
assert.equal(el.computed, 'quuxliteral');
423418

424419
el.foo = {baz: 'quod'};
425420
assert.equal(el.computed, undefined);
426421
});
427422

423+
test('optional arguments can be undefined', function() {
424+
var called = 0;
425+
var calledWith;
426+
Polymer({
427+
is: 'optional-arguments-can-be-undefined',
428+
properties: {
429+
foo: Object
430+
},
431+
observers: ['compute(?foo.bar, "literal")'],
432+
433+
compute: function(bar, literal) {
434+
called += 1;
435+
calledWith = [bar, literal];
436+
}
437+
});
438+
439+
el = document.createElement('optional-arguments-can-be-undefined');
440+
441+
el.foo = {bar: 'quux'};
442+
assert.equal(called, 1);
443+
assert.deepEqual(calledWith, ['quux', 'literal']);
444+
445+
el.foo = {baz: 'quod'};
446+
assert.equal(called, 2);
447+
assert.deepEqual(calledWith, [undefined, 'literal']);
448+
});
449+
450+
test('check that optional arguments work on annotations', function() {
451+
el = document.createElement("x-optional-argument-on-computed-annotation");
452+
453+
el.foo = {bar: 'quux'};
454+
assert.equal(el.$.check.prop, 'quuxliteral');
455+
456+
el.foo = {baz: 'quod'};
457+
assert.equal(el.$.check.prop, 'undefinedliteral');
458+
});
428459
});
429460

430461
</script>

0 commit comments

Comments
 (0)