Skip to content

Commit 8c43b4d

Browse files
committed
Never allow for a level lower than -1
use solution from #26
1 parent 6b499c6 commit 8c43b4d

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

lib/parse.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ module.exports = function parse(html, options) {
1515
var byTag = {};
1616
var inComponent = false;
1717

18-
if (!new RegExp(tagRE).test(html)) {
18+
if (html.indexOf('<') !== 0) {
19+
var end = html.indexOf('<');
1920
result.push({
2021
type: 'text',
21-
content: html
22+
content: end === -1 ? html : html.substring(0, end)
2223
});
2324
}
2425

@@ -68,7 +69,10 @@ module.exports = function parse(html, options) {
6869
}
6970

7071
if (!isOpen || current.voidElement) {
71-
level--;
72+
if (level > -1 && (current.voidElement || current.name === tag.slice(2, tag.indexOf(' ')))) {
73+
level--;
74+
}
75+
7276
if (!inComponent && nextChar !== '<' && nextChar) {
7377
// trailing text node
7478
// if we're at the root, push a base text node. otherwise add as
@@ -87,8 +91,10 @@ module.exports = function parse(html, options) {
8791
});
8892
}
8993
}
94+
95+
current = arr[level];
9096
}
9197
});
9298

9399
return result;
94-
};
100+
};

test/parse.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,6 @@ test('parse', function (t) {
303303
children: []
304304
}], 'should not explode on trailing whitespace');
305305

306-
html = ' Hi There ';
307-
parsed = HTML.parse(html);
308-
t.deepEqual(parsed, [{
309-
type: 'text', content: ' Hi There '
310-
}], 'should handle text nodes at the top-level');
311-
312306
html = '<div>Hi</div> There ';
313307
parsed = HTML.parse(html);
314308
t.deepEqual(parsed, [{
@@ -323,6 +317,26 @@ test('parse', function (t) {
323317
type: 'text', content: ' There '
324318
}], 'should handle trailing text nodes at the top-level');
325319

320+
html = 'Hi <div>There</div>';
321+
parsed = HTML.parse(html);
322+
t.deepEqual(parsed, [{
323+
type: 'text', content: 'Hi '
324+
}, {
325+
type: 'tag',
326+
name: 'div',
327+
attrs: {},
328+
voidElement: false,
329+
children: [
330+
{ type: 'text', content: 'There' }
331+
]
332+
}], 'should handle leading text nodes at the top-level');
333+
334+
html = 'Hi There';
335+
parsed = HTML.parse(html);
336+
t.deepEqual(parsed, [{
337+
type: 'text', content: 'Hi There'
338+
}], 'should handle plain strings of text with no tags');
339+
326340
html = '<div>Hi</div> There <span>something</span> <a></a>else ';
327341
parsed = HTML.parse(html);
328342
t.deepEqual(parsed, [{
@@ -391,6 +405,41 @@ test('parse', function (t) {
391405
voidElement: false,children: [ { content: '\n !function() {\n var cookies = document.cookie ? document.cookie.split(\';\') : [];\n // | this less than is triggering probems\n for (var i = 0; i ', type: 'text' } ]
392406
}], 'should parse a script tag');
393407

408+
html = '<div>Hi</span>There</div>';
409+
parsed = HTML.parse(html);
410+
t.deepEqual(parsed, [{
411+
type: 'tag',
412+
name: 'div',
413+
attrs: {},
414+
voidElement: false,
415+
children: [
416+
{ type: 'text', content: 'Hi' },
417+
{ type: 'text', content: 'There' }
418+
]
419+
}], 'should skip over closing tags that don\'t match the current tag name');
420+
421+
html = '<p>Hi There</p></span>root text</p><p>Try again</p>';
422+
parsed = HTML.parse(html);
423+
t.deepEqual(parsed, [{
424+
type: 'tag',
425+
name: 'p',
426+
attrs: {},
427+
voidElement: false,
428+
children: [
429+
{ type: 'text', content: 'Hi There' }
430+
]
431+
},{
432+
type: 'text',
433+
content: 'root text'
434+
}, {
435+
type: 'tag',
436+
name: 'p',
437+
attrs: {},
438+
voidElement: false,
439+
children: [
440+
{ type: 'text', content: 'Try again' }
441+
]
442+
}], 'should not go lower than the root level (-1)');
394443
t.end();
395444
});
396445

0 commit comments

Comments
 (0)