Skip to content

Commit

Permalink
Make test pass, fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza1311 committed Aug 8, 2022
1 parent 43ab9d0 commit 9b418bf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 46 deletions.
84 changes: 51 additions & 33 deletions packages/yew-macro/src/html_tree/html_element.rs
Expand Up @@ -135,40 +135,58 @@ impl ToTokens for HtmlElement {
// other attributes

let attributes = {
let normal_attrs = attributes.iter().map(|Prop { label, value, is_forced_attribute, .. }| {
(label.to_lit_str(), value.optimize_literals_tagged(), *is_forced_attribute)
});
let boolean_attrs = booleans.iter().filter_map(|Prop { label, value, is_forced_attribute, .. }| {
let key = label.to_lit_str();
Some((
key.clone(),
match value {
Expr::Lit(e) => match &e.lit {
Lit::Bool(b) => Value::Static(if b.value {
quote! { #key }
} else {
return None;
}),
_ => Value::Dynamic(quote_spanned! {value.span()=> {
::yew::utils::__ensure_type::<::std::primitive::bool>(#value);
#key
}}),
},
expr => Value::Dynamic(
quote_spanned! {expr.span().resolved_at(Span::call_site())=>
if #expr {
::std::option::Option::Some(
::yew::virtual_dom::AttrValue::Static(#key)
)
let normal_attrs = attributes.iter().map(
|Prop {
label,
value,
is_forced_attribute,
..
}| {
(
label.to_lit_str(),
value.optimize_literals_tagged(),
*is_forced_attribute,
)
},
);
let boolean_attrs = booleans.iter().filter_map(
|Prop {
label,
value,
is_forced_attribute,
..
}| {
let key = label.to_lit_str();
Some((
key.clone(),
match value {
Expr::Lit(e) => match &e.lit {
Lit::Bool(b) => Value::Static(if b.value {
quote! { #key }
} else {
::std::option::Option::None
}
return None;
}),
_ => Value::Dynamic(quote_spanned! {value.span()=> {
::yew::utils::__ensure_type::<::std::primitive::bool>(#value);
#key
}}),
},
),
},
*is_forced_attribute
))
});
expr => Value::Dynamic(
quote_spanned! {expr.span().resolved_at(Span::call_site())=>
if #expr {
::std::option::Option::Some(
::yew::virtual_dom::AttrValue::Static(#key)
)
} else {
::std::option::Option::None
}
},
),
},
*is_forced_attribute,
))
},
);
let class_attr = classes.as_ref().and_then(|classes| match classes {
ClassesForm::Tuple(classes) => {
let span = classes.span();
Expand Down Expand Up @@ -259,7 +277,7 @@ impl ToTokens for HtmlElement {
quote! { ::yew::virtual_dom::ApplyAttributeAs::Property }
};
let value = wrap_attr_value(v);
quote! { ::std::option::Option::map(#value, |it| (it, ::yew::virtual_dom::ApplyAttributeAs::Property)) }
quote! { ::std::option::Option::map(#value, |it| (it, #apply_as)) }
});
quote! {
::yew::virtual_dom::Attributes::Dynamic{
Expand Down
17 changes: 14 additions & 3 deletions packages/yew-macro/src/props/prop.rs
Expand Up @@ -35,7 +35,10 @@ impl Prop {
/// Parse a prop using the shorthand syntax `{value}`, short for `value={value}`
/// This only allows for labels with no hyphens, as it would otherwise create
/// an ambiguity in the syntax
fn parse_shorthand_prop_assignment(input: ParseStream, is_forced_attribute: bool) -> syn::Result<Self> {
fn parse_shorthand_prop_assignment(
input: ParseStream,
is_forced_attribute: bool,
) -> syn::Result<Self> {
let value;
let _brace = braced!(value in input);
let expr = value.parse::<Expr>()?;
Expand All @@ -61,7 +64,11 @@ impl Prop {
));
}?;

Ok(Self { label, value: expr, is_forced_attribute })
Ok(Self {
label,
value: expr,
is_forced_attribute,
})
}

/// Parse a prop of the form `label={value}`
Expand All @@ -85,7 +92,11 @@ impl Prop {
}

let value = parse_prop_value(input)?;
Ok(Self { label, value, is_forced_attribute })
Ok(Self {
label,
value,
is_forced_attribute,
})
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/yew-macro/src/props/prop_macro.rs
Expand Up @@ -61,7 +61,11 @@ impl Parse for PropValue {
impl From<PropValue> for Prop {
fn from(prop_value: PropValue) -> Prop {
let PropValue { label, value } = prop_value;
Prop { label, value, is_forced_attribute: false }
Prop {
label,
value,
is_forced_attribute: false,
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions packages/yew/src/dom_bundle/btag/attributes.rs
Expand Up @@ -295,11 +295,13 @@ impl Apply for Attributes {
#[cfg(test)]
mod tests {
use std::time::Duration;

use gloo::utils::document;
use js_sys::Reflect;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use crate::{html, Html, function_component};

use super::*;
use crate::{function_component, html, Html};

wasm_bindgen_test_configure!(run_in_browser);

Expand Down Expand Up @@ -345,7 +347,11 @@ mod tests {
]);
let (element, btree) = create_element();
attrs.apply(&btree, &element);
assert_eq!(element.outer_html(), "<a href=\"https://example.com/\"></a>", "should be set as attribute");
assert_eq!(
element.outer_html(),
"<a href=\"https://example.com/\"></a>",
"should be set as attribute"
);
assert_eq!(
Reflect::get(element.as_ref(), &JsValue::from_str("alt"))
.expect("no alt")
Expand All @@ -358,13 +364,10 @@ mod tests {

#[test]
fn class_is_always_attrs() {
let attrs = Attributes::Static(&[
("class", "thing", ApplyAttributeAs::Attribute),
]);
let attrs = Attributes::Static(&[("class", "thing", ApplyAttributeAs::Attribute)]);

let (element, btree) = create_element();
attrs.apply(&btree, &element);
assert_eq!(element.get_attribute("id").unwrap(), "foo");
assert_eq!(element.get_attribute("class").unwrap(), "thing");
}

Expand All @@ -376,8 +379,8 @@ mod tests {
}

let output = gloo::utils::document().get_element_by_id("output").unwrap();
yew::Renderer::<Comp>::with_root(output.clone())
.render();
yew::Renderer::<Comp>::with_root(output.clone()).render();

gloo::timers::future::sleep(Duration::from_secs(1)).await;
let element = output.query_selector("a").unwrap().unwrap();
assert_eq!(element.get_attribute("alt").unwrap(), "abc");
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/dom_bundle/btag/mod.rs
Expand Up @@ -386,7 +386,7 @@ mod feat_hydration {
#[cfg(test)]
mod tests {
use gloo::utils::document;
use wasm_bindgen::{JsCast};
use wasm_bindgen::JsCast;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use web_sys::HtmlInputElement as InputElement;

Expand Down

0 comments on commit 9b418bf

Please sign in to comment.