From 1674c0cac511302fe1bcc20aa7463d076e16f307 Mon Sep 17 00:00:00 2001 From: tobymao Date: Mon, 16 Aug 2021 21:05:00 -0700 Subject: [PATCH] allow children to be string objects fixes #977 --- src/h.ts | 4 ++-- src/is.ts | 2 +- test/core.js | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/h.ts b/src/h.ts index da57f4e..34f4a00 100644 --- a/src/h.ts +++ b/src/h.ts @@ -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; } } diff --git a/src/is.ts b/src/is.ts index 9f7ef1f..cc1b1c9 100644 --- a/src/is.ts +++ b/src/is.ts @@ -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; } diff --git a/test/core.js b/test/core.js index 1f38cb9..948a76c 100644 --- a/test/core.js +++ b/test/core.js @@ -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, {});