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

String literals are not allowed in html! macro #757

Closed
jstarry opened this issue Nov 28, 2019 · 10 comments
Closed

String literals are not allowed in html! macro #757

jstarry opened this issue Nov 28, 2019 · 10 comments
Labels
feature-request A feature request

Comments

@jstarry
Copy link
Member

jstarry commented Nov 28, 2019

Description

Yew's html!{} macro deviates from JSX by not allowing text literals.

Proposed Syntax:
html! {
    <p>Hello</p>
}
Current Syntax
html! {
    <p>{ "Hello" }</p>
}
@mdtusz
Copy link
Contributor

mdtusz commented Nov 28, 2019

I actually like the strictness of not allowing string literals and keeping a separation between the DOM structure created and the "contents". Has this feature been requested lots?

@jstarry
Copy link
Member Author

jstarry commented Nov 28, 2019

@mdtusz it hasn't, just wanted to document the request. It will likely be marked as won't fix

@hgzimmerman
Copy link
Member

I like the strictness and singular way to do things as they stand, but I do sometimes find it frustrating to not be able to copy+paste HTML from some external site (like some CSS framework guide showing how to use their classes) and instead have to meticulously wrap every bit of text in {" and "} to appease the html! macro.

I don't think its too much of a problem if its implemented or not.

@jplatte
Copy link
Contributor

jplatte commented Jan 30, 2020

I think it's important that html! accepts pretty much any valid HTML. Allowing plain text is probably the most obvious thing, but there are other examples too. I could imagine making the following legal:

html! {
    <ul>
        <li>List item
        <li>Another list item
    </ul>
}

(yes, that is valid HTML5)

Also, I think yew should recognize tags that HTML5 specifies to be self-closing (to make <meta> and <input> be equal to <meta /> and <input />.

Maybe I should just implement this in a new proc macro crate 🤔

@mdtusz
Copy link
Contributor

mdtusz commented Jan 30, 2020

While html! outputs html, the input isn't html and is a lot closer to jsx and other html-likes. I think it's probably prudent to keep it strict. It can be slightly annoying to have to remember and fix closing tags, but it is in line with "the rust way" of keeping things correct.

If the desire is to be able to have pure input html, perhaps a raw_html! macro could be useful?

@tracker1
Copy link

tracker1 commented Jan 30, 2020

Just my $.02, but sticking as closely as possible to JSX syntax is probably a very good thing given the broad adoption of the syntax... which means literal text should be allowed, and non-xml input would not be allowed.

Along the same lines would be empty tag (fragment) support, in case you need it (such as for a dictionary list item).

html! {
  <>
    <dt>{term}</dt>
    <dd>{definition}</dd>
  </>
}

@hgzimmerman
Copy link
Member

Another minor detail I've recalled about this topic is that I believe that syn is much faster at parsing quote-delimited strings instead of arbitrary text tokens.
I think it would be prudent to benchmark compile times before such a change would be merged because there might be the possibility of drastically slowing down compile times.

@jplatte
Copy link
Contributor

jplatte commented Jan 31, 2020

I hadn't considered JSX. If it's also much more strict than HTML, that's a reason to be a little stricter as well.

syn is much faster at parsing quote-delimited strings instead of arbitrary text tokens

I hadn't previously considered how html! input is parsed. I wonder why it would be that much slower. Personally I've always heard that compiling syn takes much more time than its actual parsing.

@mdtusz
Copy link
Contributor

mdtusz commented Jan 31, 2020

@tracker1 fyi fragments like

<>
    <span>{"Yay!"}</span>
    <span>{"Yew!"}</span>
</>

already work!

@siku2
Copy link
Member

siku2 commented Feb 23, 2021

Closing this in favor of #1758

@siku2 siku2 closed this as completed Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature request
Projects
None yet
Development

No branches or pull requests

6 participants