-
-
Notifications
You must be signed in to change notification settings - Fork 279
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
Html atributes get escaped on formatting #293
Comments
Agree, most of the time attributes are not client inputs, and maybe if required is possible to create a helper like |
I believe this is only an issue with constant attributes This format feature was introduced recently in the html format package that templ depends on: a-h/htmlformat@5bd994f One way to avoid this in your case @ross96D would be to define your string as a go string |
@joerdav - the HTML formatting change isn't actually related, the HTML format lib is only used in tests to compare minified templ output with unminified expected outputs. Formatting in templ is done by parsing a templ file into an object model, and then writing it back out. So, when parsing a HTML element opening tag, the attribute parser is used: The parser attempts to grab various types of attributes from the input,. In the case of a constant attribute it returns a Constant attribute parser: templ/parser/v2/elementparser.go Lines 105 to 152 in fb53582
Element struct: Lines 392 to 399 in fb53582
To write out the formatted templ code, the attributes all have a In the case of the constant attribute, the contents are escaped... Lines 661 to 673 in fb53582
However, I don't believe they need to be, since the code is directly under the developer's control - i.e. was typed out by the developer. The general concept of templ's security model is... don't mess with constants that the developer typed, escape any content that comes from a Go string, since the Go string could contain data that comes from an untrusted source. In this case the constant attribute value is directly under the developer's control - i.e. was typed out by the developer, so I think it's OK to simply remove the escaping here (would need to check the tests). |
@ross96D - I forgot to say that I'd happily accept a PR for this change. If you're too busy, please just say, so that someone else can feel free to pick it up. 😃 Thanks! |
Co-authored-by: Adrian Hesketh <[email protected]>
Co-authored-by: Adrian Hesketh <[email protected]>
This was closed in the previous commits! Thanks for the PR. |
Given this code i would write using alpinejs
When saving the file, so the lsp formating is call and it writes back this
I find this behavior uncomfortable. I would prefer the escape happens at generation time, and do no affect my templ file so i dont have to interpret what these characters mean.
I was looking at the source code and was able to craft a solution. I would be glad to submit a pull request if this behavior was not intended
The text was updated successfully, but these errors were encountered: