Skip to content

Commit 59a10d5

Browse files
authored
fix: regression with comments inside parsed text state (#132)
1 parent ab0abcf commit 59a10d5

File tree

8 files changed

+55
-19
lines changed

8 files changed

+55
-19
lines changed

.changeset/neat-cobras-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Fix regression which caused script tags with a trailing comment as the same line as the closing tag to not always parse properly.

.changeset/quick-seals-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Remove unecessary check for cdata inside parsed text state.
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
1╭─ <html-comment><![CDATA[[if lt IE 9]><div><![endif]]]></html-comment>
2-
│ ││ ││ │ │ │ ╰─ closeTagEnd(html-comment)
3-
│ ││ ││ │ │ ╰─ closeTagName "html-comment"
4-
│ ││ ││ │ ╰─ closeTagStart "</"
5-
│ ││ ││ ╰─ cdata.value "[if lt IE 9]><div><![endif]"
6-
│ ││ │╰─ cdata "<![CDATA[[if lt IE 9]><div><![endif]]]>"
7-
│ ││ ╰─ openTagEnd
8-
│ │╰─ tagName "html-comment"
9-
╰─ ╰─ openTagStart
1+
1╭─ <![CDATA[[if lt IE 9]><div><![endif]]]>
2+
│ │ ╰─ cdata.value "[if lt IE 9]><div><![endif]"
3+
╰─ ╰─ cdata "<![CDATA[[if lt IE 9]><div><![endif]]]>"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<html-comment><![CDATA[[if lt IE 9]><div><![endif]]]></html-comment>
1+
<![CDATA[[if lt IE 9]><div><![endif]]]>

src/__tests__/fixtures/script-single-line-comment/__snapshots__/script-single-line-comment.expected.txt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,33 @@
55
│ ││ │╰─ text "// this is a comment"
66
│ ││ ╰─ openTagEnd
77
│ │╰─ tagName "script"
8-
╰─ ╰─ openTagStart
8+
╰─ ╰─ openTagStart
9+
2├─
10+
3╭─ <div>
11+
│ ││ ╰─ openTagEnd
12+
│ │╰─ tagName "div"
13+
╰─ ╰─ openTagStart
14+
4╭─ <script>// this is a comment</script>
15+
│ │ ││ ││ │ │ ╰─ closeTagEnd(script)
16+
│ │ ││ ││ │ ╰─ closeTagName "script"
17+
│ │ ││ ││ ╰─ closeTagStart "</"
18+
│ │ ││ │╰─ text "// this is a comment"
19+
│ │ ││ ╰─ openTagEnd
20+
│ │ │╰─ tagName "script"
21+
│ │ ╰─ openTagStart
22+
╰─ ╰─ text "\n "
23+
5╭─ <span>hi</span>
24+
│ │ ││ ││ │ │ ╰─ closeTagEnd(span)
25+
│ │ ││ ││ │ ╰─ closeTagName "span"
26+
│ │ ││ ││ ╰─ closeTagStart "</"
27+
│ │ ││ │╰─ text "hi"
28+
│ │ ││ ╰─ openTagEnd
29+
│ │ │╰─ tagName "span"
30+
│ │ ╰─ openTagStart
31+
╰─ ╰─ text "\n "
32+
6╭─ </div>
33+
│ │ │ ╰─ closeTagEnd(div)
34+
│ │ ╰─ closeTagName "div"
35+
│ ├─ text "\n"
36+
╰─ ╰─ closeTagStart "</"
37+
7╰─
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
<script>// this is a comment</script>
1+
<script>// this is a comment</script>
2+
3+
<div>
4+
<script>// this is a comment</script>
5+
<span>hi</span>
6+
</div>

src/states/JS_COMMENT_LINE.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ export const JS_COMMENT_LINE: StateDefinition = {
2121
if (
2222
!this.isConcise &&
2323
code === CODE.OPEN_ANGLE_BRACKET &&
24-
this.activeTag?.type === TagType.text
24+
this.activeTag?.type === TagType.text &&
25+
STATE.checkForClosingTag(this)
2526
) {
26-
// First, see if we need to see if we reached the closing tag
27+
// We need to see if we reached the closing tag
2728
// eg: <script>//foo</script>
28-
STATE.checkForClosingTag(this);
29+
this.exitState();
2930
}
3031
},
3132

src/states/PARSED_TEXT_CONTENT.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ export const PARSED_TEXT_CONTENT: StateDefinition<ParsedTextContentMeta> = {
2929
char(code) {
3030
switch (code) {
3131
case CODE.OPEN_ANGLE_BRACKET:
32-
if (
33-
this.isConcise ||
34-
!(STATE.checkForClosingTag(this) || STATE.checkForCDATA(this))
35-
) {
32+
if (this.isConcise || !STATE.checkForClosingTag(this)) {
3633
this.startText();
3734
}
3835
break;

0 commit comments

Comments
 (0)