Skip to content

Commit

Permalink
feat: self-closing script/style tags
Browse files Browse the repository at this point in the history
Fixes #27.
  • Loading branch information
virchau13 committed Apr 23, 2024
1 parent 506515c commit 8624ff8
Show file tree
Hide file tree
Showing 5 changed files with 1,778 additions and 1,276 deletions.
38 changes: 38 additions & 0 deletions corpus/self-closing-script-and-style-tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
===
Self closing script and style tags
===

<p> works </p>
<script src="./fake" />
<style someattr="idk" />
<p> worker </p>

------

(document
(element
(start_tag
(tag_name))
(text)
(end_tag
(tag_name)))
(script_element
(self_closing_tag
(tag_name)
(attribute
(attribute_name)
(quoted_attribute_value
(attribute_value)))))
(style_element
(self_closing_tag
(tag_name)
(attribute
(attribute_name)
(quoted_attribute_value
(attribute_value)))))
(element
(start_tag
(tag_name))
(text)
(end_tag
(tag_name))))
25 changes: 25 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ module.exports = grammar(HTML, {
repeat($._node_with_permissible_text),
alias($._html_interpolation_end, '}'),
),

// Astro supports self-closing script/style tags.
self_closing_script_tag: $ => seq(
'<',
alias($._script_start_tag_name, $.tag_name),
repeat($.attribute),
'/>',
),

self_closing_style_tag: $ => seq(
'<',
alias($._style_start_tag_name, $.tag_name),
repeat($.attribute),
'/>'
),

script_element: ($, original) => choice(
original,
alias($.self_closing_script_tag, $.self_closing_tag),
),

style_element: ($, original) => choice(
original,
alias($.self_closing_style_tag, $.self_closing_tag),
),

/* Astro doesn't provide any way to escape curly braces apart from
* the standard HTML method of e.g. `&x30;` */
Expand Down
150 changes: 118 additions & 32 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,62 +127,90 @@
]
},
"script_element": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "script_start_tag"
},
"named": true,
"value": "start_tag"
},
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "raw_text"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "script_start_tag"
},
"named": true,
"value": "start_tag"
},
{
"type": "BLANK"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "raw_text"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "end_tag"
}
]
},
{
"type": "SYMBOL",
"name": "end_tag"
}
]
},
"style_element": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "style_start_tag"
"name": "self_closing_script_tag"
},
"named": true,
"value": "start_tag"
},
"value": "self_closing_tag"
}
]
},
"style_element": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "raw_text"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "style_start_tag"
},
"named": true,
"value": "start_tag"
},
{
"type": "BLANK"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "raw_text"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "end_tag"
}
]
},
{
"type": "SYMBOL",
"name": "end_tag"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "self_closing_style_tag"
},
"named": true,
"value": "self_closing_tag"
}
]
},
Expand Down Expand Up @@ -644,6 +672,64 @@
"value": "}"
}
]
},
"self_closing_script_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_script_start_tag_name"
},
"named": true,
"value": "tag_name"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "attribute"
}
},
{
"type": "STRING",
"value": "/>"
}
]
},
"self_closing_style_tag": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "<"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_style_start_tag_name"
},
"named": true,
"value": "tag_name"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "attribute"
}
},
{
"type": "STRING",
"value": "/>"
}
]
}
},
"extras": [
Expand Down
8 changes: 8 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@
"type": "raw_text",
"named": true
},
{
"type": "self_closing_tag",
"named": true
},
{
"type": "start_tag",
"named": true
Expand Down Expand Up @@ -316,6 +320,10 @@
"type": "raw_text",
"named": true
},
{
"type": "self_closing_tag",
"named": true
},
{
"type": "start_tag",
"named": true
Expand Down
Loading

0 comments on commit 8624ff8

Please sign in to comment.