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 overly greedy parsing in raw elements parser #702

Merged
merged 2 commits into from
May 3, 2024
Merged

Conversation

deej-io
Copy link
Contributor

@deej-io deej-io commented Apr 25, 2024

Hello!

I have been using templ for a project where I am using non-standard HTML elements and I hit a parse error when I used a tag named <scripts>.

In the existing code, if the raw element name is successfully parsed, it immediately attempts to parse the attributes, without checking for whitespace or a closing >.

This means that something like <scripts> would be parsed as a <script> tag with a boolean attribute of s (the last character of the tag name). This would then give a confusing error about mismatched tags as the closing </scripts> tag was actually parsed correctly.

--- FAIL: TestRawElementParserIsNotGreedy (0.00s)
    --- FAIL: TestRawElementParserIsNotGreedy/styles_tag (0.00s)
        raw_test.go:115: unexpected error: <style>: expected end tag not present: line 0, col 8
    --- FAIL: TestRawElementParserIsNotGreedy/scripts_tag (0.00s)
        raw_test.go:115: unexpected error: <script>: expected end tag not present: line 0, col 9

This commit fixes this issue by delegating to the elementNameParser, which checks for white space or a closing >, and then compares the name of the expected raw element.

In the existing code, if the raw element name is successfully parsed,
it immediately attempts to parse the attributes, without checking
for whitespace or a closing `>`.

This means that something like `<scripts>` would be parsed as a
`<script>` tag with a boolean attribute of `s` (the last character of
the tag name). This would then give a confusing error about mismatched
tags as the closing `</scripts>` tag was actually parsed correctly.

This commit fixes this issue by delegating to the elementNameParser,
which checks for whitespace or a closing `>`, and then compares the
names of the elements.
@a-h a-h merged commit fe1f70e into a-h:main May 3, 2024
@a-h
Copy link
Owner

a-h commented May 3, 2024

Thanks for that!

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.

2 participants