diff --git a/src/polymer-expressions.js b/src/polymer-expressions.js index 628faec..a784d4d 100644 --- a/src/polymer-expressions.js +++ b/src/polymer-expressions.js @@ -121,7 +121,11 @@ // is that ECMAScript indentifiers are more limited than CSS classnames. var resolveFn = delegate.labeledStatements.length ? newLabeledResolve(delegate.labeledStatements) : - resolveFn = delegate.expression; + delegate.expression; + + delegate.filters.forEach(function(filter) { + resolveFn = filter.toDOM(resolveFn); + }); var paths = []; for (var prop in delegate.deps) { @@ -180,8 +184,31 @@ function Filter(name, args) { this.name = name; this.args = args; + this.object_ = null; } + Filter.prototype = { + get object() { + if (this.object_) + return this.object_; + + var f = PolymerExpressions.filters[this.name]; + var argumentValues = this.args.map(function(arg) { + var fn = getFn(arg); + return fn(); + }); + return this.object_ = f.apply(null, argumentValues); + }, + + toDOM: function(fn) { + var object = this.object; + return function(values) { + var value = fn(values); + return object.toDOM(value); + } + } + }; + function ASTDelegate() { this.expression = null; this.filters = []; @@ -316,10 +343,7 @@ }, createFilter: function(name, args) { - var argValues = args.map(function(f) { - return f(); - }); - this.filters.push(new Filter(name, argValues)); + this.filters.push(new Filter(name, args)); }, createTopLevel: function(expression) { @@ -331,6 +355,8 @@ function PolymerExpressions() {} + PolymerExpressions.filters = Object.create(null); + PolymerExpressions.prototype = { getBinding: function(model, pathString, name, node) { pathString = pathString.trim(); diff --git a/tests/tests.js b/tests/tests.js index 3644a63..08547fa 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -40,6 +40,10 @@ suite('PolymerExpressions', function() { unbindAll(testDiv); Platform.performMicrotaskCheckpoint(); assert.strictEqual(4, Observer._allObserversCount); + + delete PolymerExpressions.filters.hex; + delete PolymerExpressions.filters.toFixed; + delete PolymerExpressions.filters.upperCase; }); function hasClass(node, className) { @@ -75,6 +79,33 @@ suite('PolymerExpressions', function() { }); } + function hex() { + return { + toDOM: function(value) { + return Number(value).toString(16); + }, + toModel: function(value) { + return parseInt(value, 16); + } + }; + } + + function toFixed(fractions) { + return { + toDOM: function(value) { + return Number(value).toFixed(fractions); + } + }; + } + + function upperCase() { + return { + toDOM: function(value) { + return String(value).toUpperCase(); + } + }; + } + test('ClassName Singular', function() { var div = createTestHtml( '