-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow literals without braces in html!
#3440
Allow literals without braces in html!
#3440
Conversation
Visit the preview URL for this PR (updated for commit 2480410): https://yew-rs--pr3440-allow-literal-withou-5evvdh9m.web.app (expires Tue, 10 Oct 2023 19:13:40 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Size Comparison
✅ None of the examples has changed their size significantly. |
Benchmark - SSRYew Master
Pull Request
|
I remember it being a deliberate decision to require braces around the literals. I, personally, am not in favor of this change either. Why do you think it would be better to allow literals like that? I think if we can't allow <span>yew 0.21</span> then there isn't really a point in removing the braces (we can't allow that because it's a parsing nightmare). The braces indicate any rust expression, which includes literals. Also, There's also the question of what to do with white spaces. HTML treats them as one whitespace, Rust ignores them. How should multiple literals be joined? Should they just be concatenated? Should they be different text nodes? |
I've read the prior discussions on this proposal, and the main argument against it was that it makes it unclear whether the literals will be parsed as HTML or as Rust code, and that is a valid concern, however I believe that, in this case, being idiomatic about the syntax only hurts the developer experience. If we really wanted to be idiomatic, we'd have to remove Regarding handling of whitespace, even though Besides, the PR only makes braces optional, if you want your literals separated from your HTML, you can always do that. |
I agree with the point raised by @hamza1311 and this has been discussed numerous times.
There is also the question of whether
However, the common writing style is space aware. If we are not aware of the spaces, then we have to make a decision of whether we want to insert spaces for users. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also the question of whether
"Yew" 0.21
should be interpreted asYew 0.21
or"Yew" 0.21
when passed to create a Text node
you mean using the debug formatting? "Yew" 0.21
I think it's more intuitive to use the ToString implementation. That's already how it is today for the things wrapped in {}
no?
We could not make a choice of between of having space or not having spaces between literals and represents both
"Yew" 0.21
and20"km/h"
nicely.
Anecdote: well actually xD you don't write 20km/h
but 20 km/h
because it's the International System of Units https://grammarhow.com/do-you-put-a-space-between-number-and-unit/
More seriously: I think a single breakable space for separation is the most intuitive solution. It's like when writing spaces between arguments in bash, you do expect a single space to come out, not all the extra space you added in your terminal.
$ echo woow ok Hello
woow ok Hello
All in all I think allowing only literals to be written in the macro is safe and actually consistent with how attributes work (which also allow literals). ✔️
I meant to say that if the content is not inside a brace, it should be rendered as if the content are written between 2 html tags.
This is because
TIL, Interesting. Maybe I should switch to the canonical way in the future.
Terminals actually are aware of whether there are spaces around tokens.
We do not have the spacing information with the current procedural macro. There are cases where things are legitimately broken if spaces are present / not present between tokens when they should not or should be when it comes to html contents, in which I have come across in another project. So I do not wish Yew to pertain a similar kind of issue with |
I understand better where you're coming from. I'm not sure if the html! macro can be compared to the css! one tbh... The issues you explain are real in CSS but in HTML the behavior of spacing is very different:
I don't think we need to. The spacing behavior in Rust parser is actually similar to the HTML. One or more space is basically the same as a single space. This case you mention is very specific to bash:
Because "px" is first evaluated as string then put in place into the rest of the expression. This should not exist in html! macro. Actually I don't know any modern programing language that has this feature but it's a very confusing one so it's best not to have it. What I'm trying to say is that maybe we don't need more than that. It has to be close to HTML and that's it.
I do think it makes sense to wrap expressions into But just like the parser for attributes, literals were not the issue. Strings, numbers and booleans can be used with syn without the |
The thing is, we don't even get one space. The Rust lexer ignores the spaces and will give the literals to us as if they were written without any spaces |
CSS can be inlined in the
I agree, kind of. I feel if I write
If the ultimate goal is to pursue text content / literals rendered without braces,
The ideal result for these solutions are:
By the end of the day, I would definitely prefer 1 over 2. Solution 1 is partially achievable once proc_macro_span is stabilised. Pursuing 1 with If in the future we wish to make |
Good point
I would definitely prefer a compile error if no space.
Same 👍
Good point... though I feel comfortable committing into the solution 2. But your example clearly shows that not forcing the space could be really useful. So I'm in doubt. |
To be honest, the arguments here were so compelling I'm no longer in favor of my own PR, feel free to dismiss it. |
I misread. I'm definitely in favor of 2 instead of 1. Solution one is risky, it will brings tons of issues with the parser I can smell that.
coward lol tbh I don't care that much but I do recognize it would be useful and safe to allow literals. But again I don't really care, the |
Description
Allows for all kinds of literals to be passed to the
html!
macro without enclosing them in braces as would be required for any other Rust expression, thus, for example, making the following valid:The docs are updated accordingly.
Checklist