-
-
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
Add support for optional attributes on HTML elements/tags #1433
Conversation
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.
I have a few issues with the current implementation (I hope my comments don't come across as too negative) but I'm generally in support of this feature.
Oh and just to make sure we're not wasting our time here. |
I agree that it's a nice feature to have. Looks like the current implementation will generate extra code every time the special syntax is used. Maybe we could implement this in a different way with |
@alexschrod if you don't mind, could you also add some (failing) compilation tests for the other tag-like constructs like components and lists ( |
Do you mean doing so in this PR? Feels a bit out of scope, although I'm not opposed to doing this in a separate PR if you would like me to. |
@alexschrod
I'm only talking about the new syntax. For instance, you're missing a test for the error message emitted when trying to use the syntax on components. |
I misunderstood what you meant. I get it now. Will do! |
I've added a failing compilation test for components (not committed/pushed yet). I'm not sure I understand what you mean by "lists" and how would you even add an attribute to a fragment? |
* Use fully qualified types in macro output * Use fully qualified method calls for better error messages * Remove support for optional attributes on boolean attributes
@alexschrod <key?=None>
<div>{ "Hello World!" }</div>
</> This should cause the error "The 'key' attribute does not support being used as an optional attribute". |
Also, move tests where they belong
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.
It might look like a lot but most of my comments are tiny things.
There is however still a rather big issue that I'd like to tackle. Currently even non-optional attributes are wrapped in Option::Some
. Even if the compiler is able to completely optimize this away (which would need to be tested) it would come at the cost of increased compile time.
It should definitely be possible to refactor the code so that normal attributes aren't wrapped in Option
. One way to achieve this might be to add them to the vtag directly instead of storing them in attr_pairs
first.
The approach suggested by @jstarry would also do the trick because Into<Option<T>>
is implemented for all T
as Some(T)
.
I don't know why this is the case, but the build case fails because of this difference in output: --- alexschrod
+++ Travis
@@ -301,13 +301,8 @@
error[E0277]: `NotToString` doesn't implement `std::fmt::Display`
--> $DIR/html-tag-fail.rs:49:23
|
-49 | html! { <a media?=Some(NotToString) /> };
- | ^^^^ `NotToString` cannot be formatted with the default formatter
- |
- ::: $RUST/src/liballoc/string.rs:2194:1
- |
-2194 | pub trait ToString {
- | ------------------ required by this bound in `std::string::ToString::to_string`
+49 | html! { <a media?=Some(NotToString) /> };
+ | ^^^^ `NotToString` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `NotToString`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
@@ -344,13 +339,8 @@
error[E0277]: `NotToString` doesn't implement `std::fmt::Display`
--> $DIR/html-tag-fail.rs:54:24
|
-54 | html! { <li value?=Some(NotToString) /> };
- | ^^^^ `NotToString` cannot be formatted with the default formatter
- |
- ::: $RUST/src/liballoc/string.rs:2194:1
- |
-2194 | pub trait ToString {
- | ------------------ required by this bound in `std::string::ToString::to_string`
+54 | html! { <li value?=Some(NotToString) /> };
+ | ^^^^ `NotToString` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `NotToString`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead I can't help it that my rustc decides to throw in something about
|
Co-authored-by: Simon <[email protected]>
Travis appears to be having issues. |
Travis should be working again. Just need to update to the latest commit. |
@alexschrod There have been some significant changes to the macro internals. I updated the code and opened a new PR: #1537 |
#1537 just landed 🎉 |
Description
I've added support for optional attributes on HTML elements/tags as described in #903. That is, this is now possible:
Fixes #903
Checklist:
./ci/run_stable_checks.sh
(Can't, I'm on Windows, but I did the things manually.)