allow children to be string objects

fixes #977
pull/978/head
tobymao 4 years ago
parent f49c793eb4
commit 1674c0cac5

@ -26,11 +26,11 @@ export function h(sel: any, b?: any, c?: any): VNode {
if (c !== undefined) {
if (b !== null) { data = b; }
if (is.array(c)) { children = c; }
else if (is.primitive(c)) { text = c; }
else if (is.primitive(c)) { text = c.toString(); }
else if (c && c.sel) { children = [c]; }
} else if (b !== undefined && b !== null) {
if (is.array(b)) { children = b; }
else if (is.primitive(b)) { text = b; }
else if (is.primitive(b)) { text = b.toString(); }
else if (b && b.sel) { children = [b]; }
else { data = b; }
}

@ -1,4 +1,4 @@
export const array = Array.isArray;
export function primitive(s: any): s is (string | number) {
return typeof s === 'string' || typeof s === 'number';
return typeof s === 'string' || typeof s === 'number' || s instanceof String;
}

@ -67,6 +67,18 @@ describe('snabbdom', function() {
var vnode = h('a', {}, 'I am a string');
assert.equal(vnode.text, 'I am a string');
});
it('can create vnode with String obj content', function() {
var vnode = h('a', new String('b'));
assert.equal(vnode.text, 'b');
});
it('can create vnode with props and String obj content', function() {
var vnode = h('a', {}, new String('b'));
assert.equal(vnode.text, 'b');
});
it('can create vnode with array String obj content', function() {
var vnode = h('a', ['b', new String('c')]);
assert.equal(vnode.text, 'bc');
});
it('can create vnode with null props', function() {
var vnode = h('a', null);
assert.deepStrictEqual(vnode.data, {});

Loading…
Cancel
Save