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 HTML comment parsing #16

Merged
merged 3 commits into from
Dec 23, 2023
Merged

Fix HTML comment parsing #16

merged 3 commits into from
Dec 23, 2023

Conversation

vrischmann
Copy link
Owner

This PR fixes HTML comment parsing for comments containing a dash - by using a custom scanner.

This file:

package main

templ example() {
     <html>
	<head>
	   <meta charset="UTF-8"/>
	   <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
	   <title>Example</title>
	   <!--
                <link rel="stylesheet" href="/assets/css/bulma.min.css"/> What is bulma??
                <link rel="apple-touch-icon" sizes="180x180" href="/assets/favicon/apple-touch-icon.png"/>
            -->
	   <link href="/assets/styles/tailwind.css" rel="stylesheet"/>
	 </head>
	 <body>
            Hello, World!
	 </body>
    </html>
}

Parses to this without this PR:

(source_file [0, 0] - [19, 0]
  (package_clause [0, 0] - [0, 12]
    (package_identifier [0, 8] - [0, 12]))
  (component_declaration [2, 0] - [18, 1]
    name: (component_identifier [2, 6] - [2, 13])
    (parameter_list [2, 13] - [2, 15])
    (component_block [2, 16] - [18, 1]
      (element [3, 5] - [17, 11]
        (tag_start [3, 5] - [3, 11]
          name: (element_identifier [3, 6] - [3, 10]))
        (element [4, 2] - [13, 10]
          (tag_start [4, 2] - [4, 8]
            name: (element_identifier [4, 3] - [4, 7]))
          (element [5, 5] - [5, 28]
            (self_closing_tag [5, 5] - [5, 28]
              name: (element_identifier [5, 6] - [5, 10])
              (attribute [5, 11] - [5, 26]
                name: (attribute_name [5, 11] - [5, 18])
                value: (quoted_attribute_value [5, 19] - [5, 26]
                  (attribute_value [5, 20] - [5, 25])))))
          (element [6, 5] - [6, 76]
            (self_closing_tag [6, 5] - [6, 76]
              name: (element_identifier [6, 6] - [6, 10])
              (attribute [6, 11] - [6, 26]
                name: (attribute_name [6, 11] - [6, 15])
                value: (quoted_attribute_value [6, 16] - [6, 26]
                  (attribute_value [6, 17] - [6, 25])))
              (attribute [6, 27] - [6, 74]
                name: (attribute_name [6, 27] - [6, 34])
                value: (quoted_attribute_value [6, 35] - [6, 74]
                  (attribute_value [6, 36] - [6, 73])))))
          (element [7, 5] - [7, 27]
            (tag_start [7, 5] - [7, 12]
              name: (element_identifier [7, 6] - [7, 11]))
            (element_text [7, 12] - [7, 19])
            (tag_end [7, 19] - [7, 27]
              name: (element_identifier [7, 21] - [7, 26])))
          (ERROR [8, 5] - [11, 15])
          (element [12, 5] - [12, 64]
            (self_closing_tag [12, 5] - [12, 64]
              name: (element_identifier [12, 6] - [12, 10])
              (attribute [12, 11] - [12, 45]
                name: (attribute_name [12, 11] - [12, 15])
                value: (quoted_attribute_value [12, 16] - [12, 45]
                  (attribute_value [12, 17] - [12, 44])))
              (attribute [12, 46] - [12, 62]
                name: (attribute_name [12, 46] - [12, 49])
                value: (quoted_attribute_value [12, 50] - [12, 62]
                  (attribute_value [12, 51] - [12, 61])))))
          (tag_end [13, 3] - [13, 10]
            name: (element_identifier [13, 5] - [13, 9])))
        (element [14, 3] - [16, 10]
          (tag_start [14, 3] - [14, 9]
            name: (element_identifier [14, 4] - [14, 8]))
          (element_text [15, 12] - [15, 25])
          (tag_end [16, 3] - [16, 10]
            name: (element_identifier [16, 5] - [16, 9])))
        (tag_end [17, 4] - [17, 11]
          name: (element_identifier [17, 6] - [17, 10]))))))
foo.templ	0 ms	(ERROR [8, 5] - [11, 15])

Notice it errors on the HTML comment.

With this PR it parses correctly:

(source_file [0, 0] - [19, 0]
  (package_clause [0, 0] - [0, 12]
    (package_identifier [0, 8] - [0, 12]))
  (component_declaration [2, 0] - [18, 1]
    name: (component_identifier [2, 6] - [2, 13])
    (parameter_list [2, 13] - [2, 15])
    (component_block [2, 16] - [18, 1]
      (element [3, 5] - [17, 11]
        (tag_start [3, 5] - [3, 11]
          name: (element_identifier [3, 6] - [3, 10]))
        (element [4, 2] - [13, 10]
          (tag_start [4, 2] - [4, 8]
            name: (element_identifier [4, 3] - [4, 7]))
          (element [5, 5] - [5, 28]
            (self_closing_tag [5, 5] - [5, 28]
              name: (element_identifier [5, 6] - [5, 10])
              (attribute [5, 11] - [5, 26]
                name: (attribute_name [5, 11] - [5, 18])
                value: (quoted_attribute_value [5, 19] - [5, 26]
                  (attribute_value [5, 20] - [5, 25])))))
          (element [6, 5] - [6, 76]
            (self_closing_tag [6, 5] - [6, 76]
              name: (element_identifier [6, 6] - [6, 10])
              (attribute [6, 11] - [6, 26]
                name: (attribute_name [6, 11] - [6, 15])
                value: (quoted_attribute_value [6, 16] - [6, 26]
                  (attribute_value [6, 17] - [6, 25])))
              (attribute [6, 27] - [6, 74]
                name: (attribute_name [6, 27] - [6, 34])
                value: (quoted_attribute_value [6, 35] - [6, 74]
                  (attribute_value [6, 36] - [6, 73])))))
          (element [7, 5] - [7, 27]
            (tag_start [7, 5] - [7, 12]
              name: (element_identifier [7, 6] - [7, 11]))
            (element_text [7, 12] - [7, 19])
            (tag_end [7, 19] - [7, 27]
              name: (element_identifier [7, 21] - [7, 26])))
          (element_comment [8, 5] - [11, 15])
          (element [12, 5] - [12, 64]
            (self_closing_tag [12, 5] - [12, 64]
              name: (element_identifier [12, 6] - [12, 10])
              (attribute [12, 11] - [12, 45]
                name: (attribute_name [12, 11] - [12, 15])
                value: (quoted_attribute_value [12, 16] - [12, 45]
                  (attribute_value [12, 17] - [12, 44])))
              (attribute [12, 46] - [12, 62]
                name: (attribute_name [12, 46] - [12, 49])
                value: (quoted_attribute_value [12, 50] - [12, 62]
                  (attribute_value [12, 51] - [12, 61])))))
          (tag_end [13, 3] - [13, 10]
            name: (element_identifier [13, 5] - [13, 9])))
        (element [14, 3] - [16, 10]
          (tag_start [14, 3] - [14, 9]
            name: (element_identifier [14, 4] - [14, 8]))
          (element_text [15, 12] - [15, 25])
          (tag_end [16, 3] - [16, 10]
            name: (element_identifier [16, 5] - [16, 9])))
        (tag_end [17, 4] - [17, 11]
          name: (element_identifier [17, 6] - [17, 10]))))))

Fixes #15

Specifically test an HTML comment containing a dash (-) because this
breaks the parser currently.
@vrischmann vrischmann merged commit eab4f31 into master Dec 23, 2023
4 checks passed
@vrischmann vrischmann deleted the element-comment branch December 23, 2023 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multi line comments are not displayed correctly if there is a '-' character inside the comment
1 participant