Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/browser/hyperdomSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,32 @@ describe('hyperdom', function () {
expect(find('.haha').text()).to.equal('object {"name":"asdf"}')
})

it('can render an object that implements toString()', function () {
function Foo () {}
Foo.prototype.toString = function () { return '[sausages]' }

function render () {
return h('div.haha', 'object ', new Foo())
}

attach(render, {})

expect(find('.haha').text()).to.equal('object [sausages]')
})

it('can render an object with circular references', function () {
var object = {}
object.circularReference = object

function render () {
return h('div.haha', 'object ', object)
}

attach(render, {})

expect(find('.haha').text()).to.equal('object [object Object]')
})

describe('class', function () {
it('accepts a string', function () {
function render () {
Expand Down
15 changes: 14 additions & 1 deletion toVdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function toVdom (object) {
} else if (typeof object.render === 'function') {
return new Component(object)
} else {
return new Vtext(JSON.stringify(object))
return new Vtext(objectAsString(object))
}
}

Expand All @@ -32,6 +32,19 @@ function addChild (children, child) {
}
}

function objectAsString (object) {
var string
if (typeof object.toString === 'function') {
string = object.toString()
}
if (string === '[object Object]') {
try {
string = JSON.stringify(object)
} catch (_) {}
}
return string
}

module.exports.recursive = function (child) {
var children = []
addChild(children, child)
Expand Down