Skip to content

Commit 818e2f5

Browse files
committed
fix: correctly parse text content after comments
1 parent 72c21b3 commit 818e2f5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
133133
if (typeof last === 'object') {
134134
if (last.content && last.content.length > 0) {
135135
const lastContentNode = last.content[last.content.length - 1];
136-
if (typeof lastContentNode === 'string') {
136+
if (typeof lastContentNode === 'string' && !lastContentNode.startsWith('<!--')) {
137137
last.content[last.content.length - 1] = `${lastContentNode}${text}`;
138138
return;
139139
}

test/test-core.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,30 @@ test.skip('should be parse tag with object in attribute data escape', t => {
6868
t.deepEqual(tree, expected);
6969
});
7070

71-
test('should be parse comment in content', t => {
71+
test('should be parse isolated comment', t => {
7272
const tree = parser('<div><!--comment--></div>');
7373
const expected = [{tag: 'div', content: ['<!--comment-->']}];
7474
t.deepEqual(tree, expected);
7575
});
7676

77+
test('should be parse comment before text content', t => {
78+
const tree = parser('<div><!--comment-->Text after comment</div>');
79+
const expected = [{tag: 'div', content: ['<!--comment-->', 'Text after comment']}];
80+
t.deepEqual(tree, expected);
81+
});
82+
83+
test('should be parse comment after text content', t => {
84+
const tree = parser('<div>Text before comment.<!--comment--></div>');
85+
const expected = [{tag: 'div', content: ['Text before comment.', '<!--comment-->']}];
86+
t.deepEqual(tree, expected);
87+
});
88+
89+
test('should be parse comment in the middle of text content', t => {
90+
const tree = parser('<div>Text surrounding <!--comment--> a comment.</div>');
91+
const expected = [{tag: 'div', content: ['Text surrounding ', '<!--comment-->', ' a comment.']}];
92+
t.deepEqual(tree, expected);
93+
});
94+
7795
test('should be parse doctype', t => {
7896
const tree = parser('<!doctype html>');
7997
const expected = ['<!doctype html>'];

0 commit comments

Comments
 (0)