Skip to content

Fix text is discarded #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
34 changes: 30 additions & 4 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,32 @@ test('parse', function (t) {
])
t.equal(html, HTML.stringify(parsed))

html = '<div> <!-- First comment --> <span>Hi</span> <!-- Another comment --> Something</div>'
parsed = HTML.parse(html)
t.deepEqual(parsed, [
{
type: "tag",
name: "div",
voidElement: false,
attrs: {},
children: [
{ type: "text", content: " " },
{ type: "comment", comment: " First comment " },
{ type: "text", content: " " },
{
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 = '<div>oh <strong>hello</strong> there! How are <span>you</span>?</div>'
parsed = HTML.parse(html)

Expand Down Expand Up @@ -850,7 +876,7 @@ test('simple speed sanity check', function (t) {
total += stepAverage
waitCount = waitLoopSize
// forcing a bit of a pause between tests
while (waitCount--) {}
while (waitCount--) { }
}
}

Expand Down Expand Up @@ -908,7 +934,7 @@ test('whitespace', function (t) {
children: [
{ type: 'text', content: 'Hi' }
]
},{
}, {
type: 'text',
content: ' '
},
Expand All @@ -920,10 +946,10 @@ test('whitespace', function (t) {
children: [
{ type: 'text', content: 'There' }
]
},{
}, {
type: 'text',
content: ' '
},{
}, {
type: 'tag',
name: 'div',
attrs: {},
Expand Down