Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Treat numbers as numbers in expressions
Browse files Browse the repository at this point in the history
This is is revert of the following two reverts
4115525
d03bd9b

The original commit had an issue where '' was treated as 0.
  • Loading branch information
arv committed Mar 26, 2014
1 parent 4115525 commit 99229bb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
32 changes: 18 additions & 14 deletions src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,20 @@
}
}

function isLiteralExpression(name) {
switch (name) {
function isLiteralExpression(pathString) {
switch (pathString) {
case '':
return false;

case 'false':
case 'null':
case 'true':
return true;
}

if (!isNaN(Number(pathString)))
return true;

return false;
};

Expand Down Expand Up @@ -638,20 +645,17 @@
return prepareEventBinding(path, name, this);
}

if (path.valid) {
if (!isLiteralExpression(pathString) && path.valid) {
if (path.length == 1) {
if (!isLiteralExpression(path[0])) {
return function(model, node, oneTime) {
if (oneTime)
return path.getValueFrom(model);

var scope = findScope(model, path[0]);
return new PathObserver(scope, path);
};
}
} else {
return; // bail out early if pathString is simple path.
return function(model, node, oneTime) {
if (oneTime)
return path.getValueFrom(model);

var scope = findScope(model, path[0]);
return new PathObserver(scope, path);
};
}
return; // bail out early if pathString is simple path.
}

return prepareBinding(pathString, name, node, this);
Expand Down
62 changes: 61 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ suite('PolymerExpressions', function() {
});

// https://github.com/Polymer/polymer-expressions/issues/24
test('issue-24', function(done) {
test('issue-24 - 1', function(done) {
var div = createTestHtml(
'<template id="t" bind if="{{ true }}">' +
'<span>{{ b }}</span>' +
Expand Down Expand Up @@ -1914,6 +1914,25 @@ suite('PolymerExpressions', function() {
});

test('issue-24 - 4', function(done) {
var div = createTestHtml(
'<template id="t" bind if="{{ 0 }}">' +
'<span>{{ b }}</span>' +
'</template>');

var model = {
b: 'hi',
0: true
};

recursivelySetTemplateModel(div, model);

then(function() {
assert.equal(div.childNodes.length, 1);
done();
});
});

test('issue-24 - 5', function(done) {
var div = createTestHtml(
'<template id="t" bind>' +
'<span>{{ true }}</span>' +
Expand All @@ -1930,4 +1949,45 @@ suite('PolymerExpressions', function() {
done();
});
});

test('issue-24 - 6', function(done) {
var div = createTestHtml(
'<template id="t" bind>' +
'<span>{{ 111 }}</span>' +
'</template>');

var model = {111: 222};

recursivelySetTemplateModel(div, model);

then(function() {
var target = div.childNodes[1];
assert.equal(div.childNodes.length, 2);
assert.equal(target.textContent, '111');
done();
});
});

test('issue-24 - 7', function(done) {
var div = createTestHtml(
'<template id="t" bind>' +
'<span>{{ 1.2 }}</span>' +
'</template>');

var model = {
1: {
2: 3
}
};

recursivelySetTemplateModel(div, model);

then(function() {
var target = div.childNodes[1];
assert.equal(div.childNodes.length, 2);
assert.equal(target.textContent, '1.2');
done();
});
});

});

0 comments on commit 99229bb

Please sign in to comment.