Skip to content

Commit 5b97224

Browse files
committed
merged in comment support code
2 parents 330c4da + 4593b12 commit 5b97224

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

src/parse-tag.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export default function stringify(tag) {
1919
) {
2020
res.voidElement = true
2121
}
22+
23+
// handle comment tag
24+
if (res.name.startsWith('!--')) {
25+
const endIndex = tag.indexOf('-->')
26+
return {
27+
type: 'comment',
28+
comment: endIndex !== -1 ? tag.slice(4, endIndex) : '',
29+
}
30+
}
2231
}
2332

2433
const reg = new RegExp(attrRE)

src/parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ export default function parse(html, options) {
2222
}
2323
}
2424
const isOpen = tag.charAt(1) !== '/'
25+
const isComment = tag.startsWith('<!--')
2526
const start = index + tag.length
2627
const nextChar = html.charAt(start)
2728
let parent
2829

30+
if (isComment) {
31+
const comment = parseTag(tag)
32+
33+
// if we're at root, push new base node
34+
if (level < 0) {
35+
result.push(comment)
36+
return result
37+
}
38+
parent = arr[level]
39+
parent.children.push(comment)
40+
return result
41+
}
42+
2943
if (isOpen) {
3044
level++
3145

src/stringify.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function stringify(buff, doc) {
2323
return buff
2424
}
2525
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>'
26+
case 'comment':
27+
buff += '<!--' + doc.comment + '-->'
28+
return buff
2629
}
2730
}
2831

test/parse.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,130 @@ test('parse', function (t) {
5555
])
5656
t.equal(html, HTML.stringify(parsed))
5757

58+
html = '<!-- just a comment node -->'
59+
parsed = HTML.parse(html)
60+
t.deepEqual(parsed, [
61+
{
62+
type: 'comment',
63+
comment: ' just a comment node ',
64+
},
65+
])
66+
t.equal(html, HTML.stringify(parsed))
67+
68+
html =
69+
'<div><h2>Comment below this header</h2><!-- just a comment node --></div>'
70+
parsed = HTML.parse(html)
71+
t.deepEqual(parsed, [
72+
{
73+
name: 'div',
74+
type: 'tag',
75+
attrs: {},
76+
voidElement: false,
77+
children: [
78+
{
79+
attrs: {},
80+
name: 'h2',
81+
type: 'tag',
82+
voidElement: false,
83+
children: [
84+
{
85+
content: 'Comment below this header',
86+
type: 'text',
87+
},
88+
],
89+
},
90+
{
91+
type: 'comment',
92+
comment: ' just a comment node ',
93+
},
94+
],
95+
},
96+
])
97+
t.equal(html, HTML.stringify(parsed))
98+
99+
html =
100+
'<div><h2>Comment below this header</h2><!-- just a comment node --><!-- subsequent comment node --></div>'
101+
parsed = HTML.parse(html)
102+
t.deepEqual(parsed, [
103+
{
104+
name: 'div',
105+
type: 'tag',
106+
attrs: {},
107+
voidElement: false,
108+
children: [
109+
{
110+
attrs: {},
111+
name: 'h2',
112+
type: 'tag',
113+
voidElement: false,
114+
children: [
115+
{
116+
content: 'Comment below this header',
117+
type: 'text',
118+
},
119+
],
120+
},
121+
{
122+
type: 'comment',
123+
comment: ' just a comment node ',
124+
},
125+
{
126+
type: 'comment',
127+
comment: ' subsequent comment node ',
128+
},
129+
],
130+
},
131+
])
132+
t.equal(html, HTML.stringify(parsed))
133+
134+
html = '<div><h2><!-- comment inside h2 tag --></h2></div>'
135+
parsed = HTML.parse(html)
136+
t.deepEqual(parsed, [
137+
{
138+
name: 'div',
139+
type: 'tag',
140+
attrs: {},
141+
voidElement: false,
142+
children: [
143+
{
144+
attrs: {},
145+
name: 'h2',
146+
type: 'tag',
147+
voidElement: false,
148+
children: [
149+
{
150+
type: 'comment',
151+
comment: ' comment inside h2 tag ',
152+
},
153+
],
154+
},
155+
],
156+
},
157+
])
158+
t.equal(html, HTML.stringify(parsed))
159+
160+
html = '<!---->'
161+
parsed = HTML.parse(html)
162+
t.deepEqual(parsed, [
163+
{
164+
type: 'comment',
165+
comment: '',
166+
},
167+
])
168+
t.equal(html, HTML.stringify(parsed))
169+
170+
html =
171+
'<!---this comment starts with a hyphen b/c web developers love curveballs -->'
172+
parsed = HTML.parse(html)
173+
t.deepEqual(parsed, [
174+
{
175+
type: 'comment',
176+
comment:
177+
'-this comment starts with a hyphen b/c web developers love curveballs ',
178+
},
179+
])
180+
t.equal(html, HTML.stringify(parsed))
181+
58182
html = '<div>oh <strong>hello</strong> there! How are <span>you</span>?</div>'
59183
parsed = HTML.parse(html)
60184

0 commit comments

Comments
 (0)