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: onCloseTagStart event for text mode tags #117

Merged
merged 1 commit into from
Jun 27, 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/late-cougars-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Fix issue with onCloseTagStart not called for text mode tags (eg style, script, textarea & html-comment).
5 changes: 3 additions & 2 deletions src/__tests__/fixtures/cdata/__snapshots__/cdata.expected.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <html-comment><![CDATA[[if lt IE 9]><div><![endif]]]></html-comment>
│ ││ ││ │ │ ╰─ closeTagEnd(html-comment)
│ ││ ││ │ ╰─ closeTagName "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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
│ │ ╰─ comment "<!--123-->"
╰─ ╰─ text "\n "
3╭─ <html-comment>abc</html-comment>
│ │ ││ ││ │ ╰─ closeTagEnd(html-comment)
│ │ ││ ││ ╰─ closeTagName "html-comment"
│ │ ││ ││ │ │ ╰─ closeTagEnd(html-comment)
│ │ ││ ││ │ ╰─ closeTagName "html-comment"
│ │ ││ ││ ╰─ closeTagStart "</"
│ │ ││ │╰─ text "abc"
│ │ ││ ╰─ openTagEnd
│ │ │╰─ tagName "html-comment"
Expand All @@ -19,8 +20,9 @@
│ │ ╰─ comment "<!--[if lt IE 9]><script src=\"...\"></script><![endif]-->"
╰─ ╰─ text "\n "
5╭─ <html-comment>[if lt IE 9]><script src="..."></script><![endif]</html-comment>
│ │ ││ ││ │ ╰─ closeTagEnd(html-comment)
│ │ ││ ││ ╰─ closeTagName "html-comment"
│ │ ││ ││ │ │ ╰─ closeTagEnd(html-comment)
│ │ ││ ││ │ ╰─ closeTagName "html-comment"
│ │ ││ ││ ╰─ closeTagStart "</"
│ │ ││ │╰─ text "[if lt IE 9]><script src=\"...\"></script><![endif]"
│ │ ││ ╰─ openTagEnd
│ │ │╰─ tagName "html-comment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
5├─ ${bar}
6├─ */
7╭─ </script>
│ │ ╰─ closeTagEnd(script)
╰─ ╰─ closeTagName "script"
│ │ │ ╰─ closeTagEnd(script)
│ │ ╰─ closeTagName "script"
╰─ ╰─ closeTagStart "</"
8╰─
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <style>/* block comment within <style> */"string within \"<style>\""'string within \'<style>\''</style>
│ ││ ││ │ ╰─ closeTagEnd(style)
│ ││ ││ ╰─ closeTagName "style"
│ ││ ││ │ │ ╰─ closeTagEnd(style)
│ ││ ││ │ ╰─ closeTagName "style"
│ ││ ││ ╰─ closeTagStart "</"
│ ││ │╰─ text "/* block comment within <style> */\"string within \\\"<style>\\\"\"'string within \\'<style>\\''"
│ ││ ╰─ openTagEnd
│ │╰─ tagName "style"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <script>/* Copyright $!{date} */</script>
│ ││ ││ │ ╰─ closeTagEnd(script)
│ ││ ││ ╰─ closeTagName "script"
│ ││ ││ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ ╰─ closeTagName "script"
│ ││ ││ ╰─ closeTagStart "</"
│ ││ │╰─ text "/* Copyright $!{date} */"
│ ││ ╰─ openTagEnd
│ │╰─ tagName "script"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
│ │╰─ tagName "script"
╰─ ╰─ openTagStart
2╭─ </script>
│ │ ╰─ closeTagEnd(script)
╰─ ╰─ closeTagName "script"
│ │ │ ╰─ closeTagEnd(script)
│ │ ╰─ closeTagName "script"
╰─ ╰─ closeTagStart "</"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <script>Hello ${xyz}!</script>
│ ││ ││ │ │ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ │ ╰─ closeTagName "script"
│ ││ ││ │ │ ││ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ ││ ╰─ closeTagName "script"
│ ││ ││ │ │ │╰─ closeTagStart "</"
│ ││ ││ │ │ ╰─ text
│ ││ ││ │ ╰─ placeholder:escape.value "xyz"
│ ││ ││ ╰─ placeholder:escape "${xyz}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <script>alert("Hello ${xyz}!")</script>
│ ││ ││ │ │ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ │ ╰─ closeTagName "script"
│ ││ ││ │ │ │ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ │ │ ╰─ closeTagName "script"
│ ││ ││ │ │ │ ╰─ closeTagStart "</"
│ ││ ││ │ │ ╰─ text "!\")"
│ ││ ││ │ ╰─ placeholder:escape.value "xyz"
│ ││ ││ ╰─ placeholder:escape "${xyz}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
4╭─ </script>
│ │ │ ╰─ closeTagEnd(script)
│ │ ╰─ closeTagName "script"
╰─ ╰─ text "\n"
│ ├─ text "\n"
╰─ ╰─ closeTagStart "</"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <script>// this is a comment</script>
│ ││ ││ │ ╰─ closeTagEnd(script)
│ ││ ││ ╰─ closeTagName "script"
│ ││ ││ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ ╰─ closeTagName "script"
│ ││ ││ ╰─ closeTagStart "</"
│ ││ │╰─ text "// this is a comment"
│ ││ ╰─ openTagEnd
│ │╰─ tagName "script"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1╭─ <script>console.log('<div>Hello ${input.name}</div>');</script>
│ ││ ││ │ │ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ │ ╰─ closeTagName "script"
│ ││ ││ │ │ │ │ │ ╰─ closeTagEnd(script)
│ ││ ││ │ │ │ │ ╰─ closeTagName "script"
│ ││ ││ │ │ │ ╰─ closeTagStart "</"
│ ││ ││ │ │ ╰─ text "</div>');"
│ ││ ││ │ ╰─ placeholder:escape.value "input.name"
│ ││ ││ ╰─ placeholder:escape "${input.name}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
│ │╰─ tagName "script"
╰─ ╰─ openTagStart
2╭─ /* block comment within <script> */"string within \"<script>\""'string within \'<script>\''</script>
│ │ ╰─ closeTagEnd(script)
╰─ ╰─ closeTagName "script"
│ │ │ ╰─ closeTagEnd(script)
│ │ ╰─ closeTagName "script"
╰─ ╰─ closeTagStart "</"
4 changes: 4 additions & 0 deletions src/states/CLOSE_TAG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export function checkForClosingTag(parser: Parser) {

if (match) {
parser.endText();
parser.options.onCloseTagStart?.({
start: curPos - 1,
end: curPos + 1,
});

if (
ensureExpectedCloseTag(parser, {
Expand Down