aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLucario387 <hoangtun0810@gmail.com>2023-01-02 02:38:54 +0900
committerStephan Seitz <stephan.seitz@fau.de>2023-01-03 12:05:10 +0100
commit6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3 (patch)
treeb8c60657ad04717f5ba2ff33e7034400916e71b0
parentUpdate parsers: scala (diff)
downloadnvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar.gz
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar.bz2
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar.lz
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar.xz
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.tar.zst
nvim-treesitter-6f4b9b190baaa4c10d8e60108ceb61b486f6e1a3.zip
refactor(vue, html): Update injections
Update html_tags injections so that html/vue/svelte parsers can use it without having a script/style tag being captured twice Signed-off-by: Pham Huy Hoang <hoangtun0810@gmail.com>
-rw-r--r--queries/html/injections.scm13
-rw-r--r--queries/html_tags/injections.scm68
-rw-r--r--queries/vue/injections.scm55
-rw-r--r--tests/query/injections/html/test-html-injections.html45
4 files changed, 122 insertions, 59 deletions
diff --git a/queries/html/injections.scm b/queries/html/injections.scm
index 682431195..7ddb5beae 100644
--- a/queries/html/injections.scm
+++ b/queries/html/injections.scm
@@ -1,12 +1 @@
-((style_element
- (raw_text) @css))
-
-((attribute
- (attribute_name) @_attr
- (quoted_attribute_value (attribute_value) @css))
- (#eq? @_attr "style"))
-
-((script_element
- (raw_text) @javascript))
-
-(comment) @comment
+; inherits html_tags
diff --git a/queries/html_tags/injections.scm b/queries/html_tags/injections.scm
index 80c0d2a4a..63d00a753 100644
--- a/queries/html_tags/injections.scm
+++ b/queries/html_tags/injections.scm
@@ -1,7 +1,67 @@
-((style_element
- (raw_text) @css))
+; <style>...</style>
+(
+ (style_element
+ (start_tag) @_no_attribute
+ (raw_text) @css)
+ (#match? @_no_attribute "^\\<\\s*style\\s*\\>$")
+ ; unsure why, but without escaping &lt; and &gt; the query breaks
+)
-((script_element
- (raw_text) @javascript))
+; <style blocking> ...</style>
+; Add "lang" to predicate check so that vue/svelte can inherit this
+; without having this element being captured twice
+(
+ (style_element
+ (start_tag
+ (attribute
+ (attribute_name) @_no_set_type))
+ (raw_text) @css)
+ (#not-any-of? @_no_set_type "type" "lang")
+)
+
+(
+ (style_element
+ (start_tag
+ (attribute
+ (attribute_name) @_type
+ (quoted_attribute_value (attribute_value) @_css)))
+ (raw_text) @css)
+ (#eq? @_type "type")
+ (#eq? @_css "text/css")
+)
+
+; <script>...</script>
+(
+ (script_element
+ (start_tag) @_no_attribute
+ (raw_text) @javascript)
+ (#match? @_no_attribute "^\\<\\s*script\\s*\\>$")
+)
+
+; <script defer>...</script>
+(
+ (script_element
+ (start_tag
+ (attribute
+ (attribute_name) @_no_set_type))
+ (raw_text) @javascript)
+ (#not-any-of? @_no_set_type "type" "lang")
+)
+
+(
+ (script_element
+ (start_tag
+ (attribute
+ (attribute_name) @_type
+ (quoted_attribute_value (attribute_value) @_javascript)))
+ (raw_text) @javascript)
+ (#eq? @_type "type")
+ (#eq? @_javascript "text/javascript")
+)
+
+((attribute
+ (attribute_name) @_attr
+ (quoted_attribute_value (attribute_value) @css))
+ (#eq? @_attr "style"))
(comment) @comment
diff --git a/queries/vue/injections.scm b/queries/vue/injections.scm
index 223791c09..84ac2160e 100644
--- a/queries/vue/injections.scm
+++ b/queries/vue/injections.scm
@@ -1,20 +1,6 @@
-(
- (style_element
- (start_tag) @_no_attribute
- (raw_text) @css)
- (#match? @_no_attribute "^\\<\\s*style\\s*\\>$")
- ; unsure why, but without escaping &lt; and &gt; the query breaks
-)
-
-(
- (style_element
- (start_tag
- (attribute
- (attribute_name) @_no_lang))
- (raw_text) @css)
- (#not-eq? @_no_lang "lang")
-)
+; inherits html_tags
+; <script lang="css">
(
(style_element
(start_tag
@@ -26,24 +12,20 @@
(#eq? @_css "css")
)
-; If script tag does not have any extra attributes, set it to javascript
-(
- (script_element
- (start_tag) @_no_attribute
- (raw_text) @javascript)
- (#match? @_no_attribute "^\\<\\s*script\\s*\\>$")
-)
-
-; if start_tag does not specify `lang="..."` then set it to javascript
+; TODO: When nvim-treesitter have postcss and less parser, use @language and @content instead
+; <script lang="scss">
(
- (script_element
+ (style_element
(start_tag
(attribute
- (attribute_name) @_no_lang))
- (raw_text) @javascript)
- (#not-eq? @_no_lang "lang")
+ (attribute_name) @_lang
+ (quoted_attribute_value (attribute_value) @_scss)))
+ (raw_text) @scss)
+ (#eq? @_lang "lang")
+ (#any-of? @_scss "scss" "less" "postcss")
)
+; <script lang="js">
(
(script_element
(start_tag
@@ -55,18 +37,7 @@
(#eq? @_js "js")
)
-; TODO: When nvim-treesitter have postcss and less parser, use @language and @content instead
-(
- (style_element
- (start_tag
- (attribute
- (attribute_name) @_lang
- (quoted_attribute_value (attribute_value) @_scss)))
- (raw_text) @scss)
- (#eq? @_lang "lang")
- (#any-of? @_scss "scss" "less" "postcss")
-)
-
+; <script lang="ts">
(
(script_element
(start_tag
@@ -93,5 +64,3 @@
(text) @pug)
(#eq? @_lang "pug")
)
-
-(comment) @comment
diff --git a/tests/query/injections/html/test-html-injections.html b/tests/query/injections/html/test-html-injections.html
new file mode 100644
index 000000000..536ec8734
--- /dev/null
+++ b/tests/query/injections/html/test-html-injections.html
@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title></title>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <link href="css/style.css" rel="stylesheet">
+ <style>
+ footer{
+/* ^ css
+*/
+ }
+ </style>
+ <style title="Test Style without type attribute">
+ footer{
+/* ^ css
+*/
+ }
+ </style>
+ <style type="text/css" title="test style with defined type attribute">
+ footer{
+/* ^ css
+*/
+ }
+ </style>
+ </head>
+ <body>
+ <script>
+ const x = 1
+// ^ javascript
+ </script>
+ <script defer>
+ const x = 1
+// ^ javascript
+ </script>
+ <script type="text/javascript">
+ const x = 1
+// ^ javascript
+ </script>
+ <div style="height: 100%">
+<!-- ^ css -->
+ Test div to test css injections for style attributes
+ </div>
+ </body>
+</html>