diff --git a/src/utils.js b/src/utils.js index a756030a125..7627d0924d6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -73,16 +73,19 @@ var utils = module.exports = { }, /** - * Make sure only strings and numbers are output to html - * output empty string is value is not string or number + * Make sure only strings, booleans, numbers and + * objects are output to html. otherwise, ouput empty string. */ toText: function (value) { /* jshint eqeqeq: false */ - return (typeof value === 'string' || - typeof value === 'boolean' || - (typeof value === 'number' && value == value)) // deal with NaN - ? value - : '' + var type = typeof value + return (type === 'string' || + type === 'boolean' || + (type === 'number' && value == value)) // deal with NaN + ? value + : type === 'object' && value !== null + ? JSON.stringify(value) + : '' }, /** diff --git a/test/unit/specs/directives.js b/test/unit/specs/directives.js index 90f8f16f338..2b77b0e97a7 100644 --- a/test/unit/specs/directives.js +++ b/test/unit/specs/directives.js @@ -32,13 +32,16 @@ describe('UNIT: Directives', function () { assert.strictEqual(dir.el.textContent, 'true') }) + it('should work with objects', function () { + dir.update({foo:"bar"}) + assert.strictEqual(dir.el.textContent, '{"foo":"bar"}') + }) + it('should be empty with other stuff', function () { dir.update(null) assert.strictEqual(dir.el.textContent, '') dir.update(undefined) assert.strictEqual(dir.el.textContent, '') - dir.update({a:123}) - assert.strictEqual(dir.el.textContent, '') dir.update(function () {}) assert.strictEqual(dir.el.textContent, '') }) @@ -65,13 +68,16 @@ describe('UNIT: Directives', function () { assert.strictEqual(dir.el.textContent, 'true') }) + it('should work with objects', function () { + dir.update({foo:"bar"}) + assert.strictEqual(dir.el.textContent, '{"foo":"bar"}') + }) + it('should be empty with other stuff', function () { dir.update(null) assert.strictEqual(dir.el.innerHTML, '') dir.update(undefined) assert.strictEqual(dir.el.innerHTML, '') - dir.update({a:123}) - assert.strictEqual(dir.el.innerHTML, '') dir.update(function () {}) assert.strictEqual(dir.el.innerHTML, '') }) diff --git a/test/unit/specs/utils.js b/test/unit/specs/utils.js index 92455ff6ad5..4ac5c904817 100644 --- a/test/unit/specs/utils.js +++ b/test/unit/specs/utils.js @@ -110,13 +110,15 @@ describe('UNIT: Utils', function () { }) it('should output empty string if value is not string or number', function () { - assert.strictEqual(txt({}), '') - assert.strictEqual(txt([]), '') assert.strictEqual(txt(undefined), '') assert.strictEqual(txt(null), '') assert.strictEqual(txt(NaN), '') }) + it('should stringify value if is object', function () { + assert.strictEqual(txt({foo:"bar"}), '{"foo":"bar"}') + }) + }) describe('extend', function () {