Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regression with comments inside parsed text state #132

Merged
merged 1 commit into from
Aug 9, 2022
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
5 changes: 5 additions & 0 deletions .changeset/neat-cobras-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Fix regression which caused script tags with a trailing comment as the same line as the closing tag to not always parse properly.
5 changes: 5 additions & 0 deletions .changeset/quick-seals-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Remove unecessary check for cdata inside parsed text state.
12 changes: 3 additions & 9 deletions src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
1╭─ <html-comment><![CDATA[[if lt IE 9]><div><![endif]]]></html-comment>
│ ││ ││ │ │ │ ╰─ closeTagEnd(html-comment)
│ ││ ││ │ │ ╰─ closeTagName "html-comment"
│ ││ ││ │ ╰─ closeTagStart "</"
│ ││ ││ ╰─ cdata.value "[if lt IE 9]><div><![endif]"
│ ││ │╰─ cdata "<![CDATA[[if lt IE 9]><div><![endif]]]>"
│ ││ ╰─ openTagEnd
│ │╰─ tagName "html-comment"
╰─ ╰─ openTagStart
1╭─ <![CDATA[[if lt IE 9]><div><![endif]]]>
│ │ ╰─ cdata.value "[if lt IE 9]><div><![endif]"
╰─ ╰─ cdata "<![CDATA[[if lt IE 9]><div><![endif]]]>"
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/cdata/input.marko
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<html-comment><![CDATA[[if lt IE 9]><div><![endif]]]></html-comment>
<![CDATA[[if lt IE 9]><div><![endif]]]>
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,33 @@
│ ││ │╰─ text "// this is a comment"
│ ││ ╰─ openTagEnd
│ │╰─ tagName "script"
╰─ ╰─ openTagStart
╰─ ╰─ openTagStart
2├─
3╭─ <div>
│ ││ ╰─ openTagEnd
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
4╭─ <script>// this is a comment</script>
│ │ ││ ││ │ │ ╰─ closeTagEnd(script)
│ │ ││ ││ │ ╰─ closeTagName "script"
│ │ ││ ││ ╰─ closeTagStart "</"
│ │ ││ │╰─ text "// this is a comment"
│ │ ││ ╰─ openTagEnd
│ │ │╰─ tagName "script"
│ │ ╰─ openTagStart
╰─ ╰─ text "\n "
5╭─ <span>hi</span>
│ │ ││ ││ │ │ ╰─ closeTagEnd(span)
│ │ ││ ││ │ ╰─ closeTagName "span"
│ │ ││ ││ ╰─ closeTagStart "</"
│ │ ││ │╰─ text "hi"
│ │ ││ ╰─ openTagEnd
│ │ │╰─ tagName "span"
│ │ ╰─ openTagStart
╰─ ╰─ text "\n "
6╭─ </div>
│ │ │ ╰─ closeTagEnd(div)
│ │ ╰─ closeTagName "div"
│ ├─ text "\n"
╰─ ╰─ closeTagStart "</"
7╰─
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<script>// this is a comment</script>
<script>// this is a comment</script>

<div>
<script>// this is a comment</script>
<span>hi</span>
</div>
7 changes: 4 additions & 3 deletions src/states/JS_COMMENT_LINE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export const JS_COMMENT_LINE: StateDefinition = {
if (
!this.isConcise &&
code === CODE.OPEN_ANGLE_BRACKET &&
this.activeTag?.type === TagType.text
this.activeTag?.type === TagType.text &&
STATE.checkForClosingTag(this)
) {
// First, see if we need to see if we reached the closing tag
// We need to see if we reached the closing tag
// eg: <script>//foo</script>
STATE.checkForClosingTag(this);
this.exitState();
}
},

Expand Down
5 changes: 1 addition & 4 deletions src/states/PARSED_TEXT_CONTENT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export const PARSED_TEXT_CONTENT: StateDefinition<ParsedTextContentMeta> = {
char(code) {
switch (code) {
case CODE.OPEN_ANGLE_BRACKET:
if (
this.isConcise ||
!(STATE.checkForClosingTag(this) || STATE.checkForCDATA(this))
) {
if (this.isConcise || !STATE.checkForClosingTag(this)) {
this.startText();
}
break;
Expand Down