Skip to content

Commit

Permalink
Add custom type for attribute values (#1994)
Browse files Browse the repository at this point in the history
* Add custom type for attribute values

* fix tests/example

* Remove `PartialEq<String>` and it's usage

* `ReferenceCounted` -> `Rc`

* fucking fake

* please CI, just turn green
  • Loading branch information
ranile authored Nov 8, 2021
1 parent 3ebd866 commit 2f47b09
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 110 deletions.
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

0 comments on commit 2f47b09

Please sign in to comment.