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

Add custom type for attribute values #1994

Merged
merged 7 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/keyed_list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "MIT OR Apache-2.0"

[dependencies]
fake = "2.2"
fake = "=2.4.1"
getrandom = { version = "0.2", features = ["js"] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
log = "0.4"
Expand All @@ -17,6 +17,6 @@ yew = { path = "../../packages/yew" }
[dependencies.web-sys]
version = "0.3"
features = [
"HtmlElement",
"HtmlInputElement",
"HtmlElement",
"HtmlInputElement",
]
7 changes: 3 additions & 4 deletions examples/router/src/pages/post.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{content, generator::Generated, Route};
use content::PostPart;
use std::borrow::Cow;
use yew::prelude::*;
use yew_router::prelude::*;

Expand Down Expand Up @@ -39,7 +38,7 @@ impl Component for Post {
html! {
<>
<section class="hero is-medium is-light has-background">
<img class="hero-background is-transparent" src={Cow::Owned(post.meta.image_url.clone())} />
<img class="hero-background is-transparent" src={post.meta.image_url.clone()} />
<div class="hero-body">
<div class="container">
<h1 class="title">
Expand Down Expand Up @@ -70,7 +69,7 @@ impl Post {
<article class="media block box my-6">
<figure class="media-left">
<p class="image is-64x64">
<img src={Cow::Owned(quote.author.image_url.clone())} loading="lazy" />
<img src={quote.author.image_url.clone()} loading="lazy" />
</p>
</figure>
<div class="media-content">
Expand All @@ -90,7 +89,7 @@ impl Post {
fn render_section_hero(&self, section: &content::Section) -> Html {
html! {
<section class="hero is-dark has-background mt-6 mb-3">
<img class="hero-background is-transparent" src={Cow::Owned(section.image_url.clone())} loading="lazy" />
<img class="hero-background is-transparent" src={section.image_url.clone()} loading="lazy" />
<div class="hero-body">
<div class="container">
<h2 class="subtitle">{ &section.title }</h2>
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl ToTokens for HtmlElement {
expr => Value::Dynamic(quote_spanned! {expr.span()=>
if #expr {
::std::option::Option::Some(
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#key)
::yew::virtual_dom::AttrValue::Static(#key)
)
} else {
::std::option::Option::None
Expand Down
8 changes: 4 additions & 4 deletions packages/yew-macro/src/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use syn::{Expr, Lit, LitStr};
/// Stringify a value at runtime.
fn stringify_at_runtime(src: impl ToTokens) -> TokenStream {
quote_spanned! {src.span()=>
::std::convert::Into::<::std::borrow::Cow::<'static, ::std::primitive::str>>::into(#src)
::std::convert::Into::<::yew::virtual_dom::AttrValue>::into(#src)
}
}

/// Create `Cow<'static, str>` construction calls.
/// Create `AttrValue` construction calls.
///
/// This is deliberately not implemented for strings to preserve spans.
pub trait Stringify {
/// Try to turn the value into a string literal.
fn try_into_lit(&self) -> Option<LitStr>;
/// Create `Cow<'static, str>` however possible.
/// Create `AttrValue` however possible.
fn stringify(&self) -> TokenStream;

/// Optimize literals to `&'static str`, otherwise keep the value as is.
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Stringify for LitStr {

fn stringify(&self) -> TokenStream {
quote_spanned! {self.span()=>
::std::borrow::Cow::<'static, ::std::primitive::str>::Borrowed(#self)
::yew::virtual_dom::AttrValue::Static(#self)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| ^ the trait `IntoPropValue<String>` is not implemented for `{integer}`
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<AttrValue>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
and 11 others
Expand All @@ -355,8 +355,8 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| ^ the trait `IntoPropValue<String>` is not implemented for `{integer}`
|
= help: the following implementations were found:
<&'static str as IntoPropValue<Cow<'static, str>>>
<&'static str as IntoPropValue<Option<Cow<'static, str>>>>
<&'static str as IntoPropValue<AttrValue>>
<&'static str as IntoPropValue<Option<AttrValue>>>
<&'static str as IntoPropValue<Option<String>>>
<&'static str as IntoPropValue<String>>
and 11 others
Expand Down
Loading