From 049ca612abff69ae0bfc3f417d82e0d2b776d063 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 17 May 2017 15:25:53 +0200 Subject: [PATCH] fix whitespace creation --- index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ae78ad4..4e3083d 100644 --- a/index.js +++ b/index.js @@ -131,12 +131,23 @@ function belCreateElement (tag, props, children) { } if (typeof node === 'string') { - if (/^[\n\r\s]+$/.test(node)) continue - if (el.lastChild && el.lastChild.nodeName === '#text') { - el.lastChild.nodeValue += node + // - if empty space, skip + // - if last node was a text node + // - if current node is a newline, push a space + // - else push the node value + // - else create a text node with the new text + if (/^[\r\s]+$/.test(node)) { continue + } else if (el.lastChild && el.lastChild.nodeName === '#text') { + if (/^[\n]+$/.test(node)) { + el.lastChild.nodeValue += ' ' + } else { + el.lastChild.nodeValue += node + } + continue + } else { + node = document.createTextNode(node) } - node = document.createTextNode(node) } if (node && node.nodeType) {