From 2eb6955939cbe66144ff6ca5e7499825439f049d Mon Sep 17 00:00:00 2001 From: Mark Carver Date: Fri, 7 Oct 2016 17:45:09 +0100 Subject: [PATCH 1/2] Add option to not ignore extra whitespace --- lib/parse.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index b8ab953..fbf090d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -4,13 +4,13 @@ var parseTag = require('./parse-tag'); // re-used obj for quick lookups of components var empty = Object.create ? Object.create(null) : {}; // common logic for pushing a child node onto a list -function pushTextNode(list, html, start) { +function pushTextNode(list, html, start, ignoreWhitespace) { // calculate correct end of the content slice in case there's // no tag after the text node. var end = html.indexOf('<', start); var content = html.slice(start, end === -1 ? undefined : end); // if a node is nothing but whitespace, no need to add it. - if (!/^\s*$/.test(content)) { + if (!ignoreWhitespace || !/^\s*$/.test(content)) { list.push({ type: 'text', content: content @@ -21,6 +21,9 @@ function pushTextNode(list, html, start) { module.exports = function parse(html, options) { options || (options = {}); options.components || (options.components = empty); + if (options.ignoreWhitespace === void 0) { + options.ignoreWhitespace = true; + } var result = []; var current; var level = -1; @@ -53,7 +56,7 @@ module.exports = function parse(html, options) { } if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') { - pushTextNode(current.children, html, start); + pushTextNode(current.children, html, start, options.ignoreWhitespace); } byTag[current.tagName] = current; @@ -81,7 +84,7 @@ module.exports = function parse(html, options) { // if we're at the root, push a base text node. otherwise add as // a child to the current node. parent = level === -1 ? result : arr[level].children; - pushTextNode(parent, html, start); + pushTextNode(parent, html, start, options.ignoreWhitespace); } } }); From 1faa34b592023b10bcf0fa7cae12e166eab65858 Mon Sep 17 00:00:00 2001 From: Mark Carver Date: Fri, 7 Oct 2016 17:45:55 +0100 Subject: [PATCH 2/2] Return a text node for just text passed --- lib/parse.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/parse.js b/lib/parse.js index fbf090d..e281079 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -89,5 +89,10 @@ module.exports = function parse(html, options) { } }); + // If the "html" passed isn't actually html, add it as a text node. + if (!result.length && html.length) { + pushTextNode(result, html, 0, options.ignoreWhitespace); + } + return result; };