Skip to content

Commit

Permalink
fixed SSR, added SSR test for precedence of value over defaultvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Oct 23, 2023
1 parent 7e3769e commit 9eddf43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ target/
*.iml
/.idea/
/.vscode/

# artifacts from npm
node_modules/
package.json
package-lock.json
52 changes: 29 additions & 23 deletions packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,14 @@ impl InputFields {
}
}

// Clippy doesn't recognise that `defaultvalue` is read in other files and in this very file when
// the "ssr" feature is enabled.
#[allow(unused)]
#[derive(Debug, Clone, Default)]
pub(crate) struct TextareaFields {
/// Contains the value of an
/// [TextAreaElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea).
pub(crate) value: Value<TextAreaElement>,
/// Contains the default value of
/// [TextAreaElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea).
#[allow(unused)] // unused only if both "csr" and "ssr" features are off
pub(crate) defaultvalue: Option<AttrValue>,
}

Expand Down Expand Up @@ -492,26 +490,16 @@ mod feat_ssr {
}
};

match &self.inner {
VTagInner::Input(InputFields { value, checked }) => {
if let Some(value) = value.as_deref() {
write_attr(w, "value", Some(value));
}

// Setting is as an attribute sets the `defaultChecked` property. Only emit this
// if it's explicitly set to checked.
if *checked == Some(true) {
write_attr(w, "checked", None);
}
if let VTagInner::Input(InputFields { value, checked }) = &self.inner {
if let Some(value) = value.as_deref() {
write_attr(w, "value", Some(value));
}

VTagInner::Textarea(TextareaFields { value, .. }) => {
if let Some(value) = value.as_deref() {
write_attr(w, "value", Some(value));
}
// Setting is as an attribute sets the `defaultChecked` property. Only emit this
// if it's explicitly set to checked.
if *checked == Some(true) {
write_attr(w, "checked", None);
}

_ => (),
}

for (k, v) in self.attributes.iter() {
Expand All @@ -522,8 +510,11 @@ mod feat_ssr {

match &self.inner {
VTagInner::Input(_) => {}
VTagInner::Textarea(TextareaFields { defaultvalue, .. }) => {
if let Some(def) = defaultvalue {
VTagInner::Textarea(TextareaFields {
value,
defaultvalue,
}) => {
if let Some(def) = value.as_ref().or(defaultvalue.as_ref()) {
VText::new(def.clone())
.render_into_stream(w, parent_scope, hydratable, VTagKind::Other)
.await;
Expand Down Expand Up @@ -637,7 +628,7 @@ mod ssr_tests {
.render()
.await;

assert_eq!(s, r#"<textarea value="teststring"></textarea>"#);
assert_eq!(s, r#"<textarea>teststring</textarea>"#);
}

#[test]
Expand All @@ -655,6 +646,21 @@ mod ssr_tests {
assert_eq!(s, r#"<textarea>teststring</textarea>"#);
}

#[test]
async fn test_value_precedence_over_defaultvalue() {
#[function_component]
fn Comp() -> Html {
html! { <textarea defaultvalue="defaultvalue" value="value" /> }
}

let s = ServerRenderer::<Comp>::new()
.hydratable(false)
.render()
.await;

assert_eq!(s, r#"<textarea>value</textarea>"#);
}

#[test]
async fn test_escaping_in_style_tag() {
#[function_component]
Expand Down

0 comments on commit 9eddf43

Please sign in to comment.