From 3bf0a499af6790116e43d95ab510e2ee8b42438b Mon Sep 17 00:00:00 2001 From: tohosaku Date: Sun, 6 Jun 2021 20:04:00 +0900 Subject: [PATCH 1/2] Add failing test --- test/parse.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/test/parse.js b/test/parse.js index 1ea26bf..6799fdb 100644 --- a/test/parse.js +++ b/test/parse.js @@ -227,6 +227,31 @@ test('parse', function (t) { ]) t.equal(html, HTML.stringify(parsed)) + html = '
Hi Something
' + parsed = HTML.parse(html) + t.deepEqual(parsed, [ + { + type: "tag", + name: "div", + voidElement: false, + attrs: {}, + children: [ + { type: "text", content: " " }, + { type: "comment", comment: " First comment " }, + { + type: "tag", + name: "span", + voidElement: false, + attrs: {}, + children: [{ type: "text", content: "Hi" }], + }, + { type: "text", content: " " }, + { type: "comment", comment: " Another comment " }, + { type: "text", content: " Something" }, + ], + } + ]) + html = '
oh hello there! How are you?
' parsed = HTML.parse(html) @@ -850,7 +875,7 @@ test('simple speed sanity check', function (t) { total += stepAverage waitCount = waitLoopSize // forcing a bit of a pause between tests - while (waitCount--) {} + while (waitCount--) { } } } @@ -908,7 +933,7 @@ test('whitespace', function (t) { children: [ { type: 'text', content: 'Hi' } ] - },{ + }, { type: 'text', content: ' ' }, @@ -920,10 +945,10 @@ test('whitespace', function (t) { children: [ { type: 'text', content: 'There' } ] - },{ + }, { type: 'text', content: ' ' - },{ + }, { type: 'tag', name: 'div', attrs: {}, From 2baf587acf38f1ab027d32150d103613006935d9 Mon Sep 17 00:00:00 2001 From: tohosaku Date: Sun, 6 Jun 2021 20:42:47 +0900 Subject: [PATCH 2/2] The text following the comment has been discarded --- src/parse.js | 8 ++++++++ test/parse.js | 1 + 2 files changed, 9 insertions(+) diff --git a/src/parse.js b/src/parse.js index 8caaf64..d141338 100644 --- a/src/parse.js +++ b/src/parse.js @@ -48,6 +48,14 @@ export default function parse(html, options) { } parent = arr[level] parent.children.push(comment) + + const text = html.slice(start, html.indexOf('<', start)); + if (text.length > 0) { + parent.children.push({ + type: 'text', + content: text + }) + } return result } diff --git a/test/parse.js b/test/parse.js index 6799fdb..c8ba578 100644 --- a/test/parse.js +++ b/test/parse.js @@ -238,6 +238,7 @@ test('parse', function (t) { children: [ { type: "text", content: " " }, { type: "comment", comment: " First comment " }, + { type: "text", content: " " }, { type: "tag", name: "span",