Skip to content
Merged
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
14 changes: 11 additions & 3 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ export default function parse(html, options) {
// calculate correct end of the content slice in case there's
// no tag after the text node.
const end = html.indexOf('<', start)
const content = html.slice(start, end === -1 ? undefined : end)
// if a node is nothing but whitespace, no need to add it.
if (!/^\s*$/.test(content)) {
let content = html.slice(start, end === -1 ? undefined : end)
// if a node is nothing but whitespace, collapse it as the spec states:
// https://www.w3.org/TR/html4/struct/text.html#h-9.1
if (/^\s*$/.test(content)) {
content = ' ';
}
// don't add whitespace-only text nodes if they would be trailing text nodes
// or if they would be leading whitespace-only text nodes:
// * end > -1 indicates this is not a trailing text node
// * leading node is when level is -1 and parent has length 0
if ((end > -1 && level + parent.length >= 0) || content !== ' ') {
parent.push({
type: 'text',
content: content,
Expand Down
55 changes: 55 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ test('parse', function (t) {
voidElement: false,
children: [{ type: 'text', content: 'something' }],
},
{
type: 'text',
content: ' ',
},
{
type: 'tag',
name: 'a',
Expand Down Expand Up @@ -686,6 +690,7 @@ test('parse', function (t) {
voidElement: false,
children: [{ type: 'text', content: 'Hi' }],
},
{ type: 'text', content: ' ' },
{
type: 'tag',
name: 'span',
Expand Down Expand Up @@ -881,3 +886,53 @@ test('ReDoS vulnerability reported by Sam Sanoop of Snyk', function (t) {
t.ok(duration < 100, 'should not hang')
t.end()
})

test('whitespace', function (t) {
let html = '<div></div>\n'
let parsed = HTML.parse(html)
t.deepEqual(parsed, [{
type: 'tag',
name: 'div',
attrs: {},
voidElement: false,
children: []
}], 'should not explode on trailing whitespace')

html = '<div>Hi</div>\n\n <span>There</span> \t <div> </div>'
parsed = HTML.parse(html)
t.deepEqual(parsed, [{
type: 'tag',
name: 'div',
attrs: {},
voidElement: false,
children: [
{ type: 'text', content: 'Hi' }
]
},{
type: 'text',
content: ' '
},
{
type: 'tag',
name: 'span',
attrs: {},
voidElement: false,
children: [
{ type: 'text', content: 'There' }
]
},{
type: 'text',
content: ' '
},{
type: 'tag',
name: 'div',
attrs: {},
voidElement: false,
children: [
{ type: 'text', content: ' ' }
]
}], 'should collapse whitespace')
// See https://www.w3.org/TR/html4/struct/text.html#h-9.1

t.end()
})