Skip to content
Open
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
7 changes: 4 additions & 3 deletions packages/ssr/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! };
//!```

use crate::renderer::{BOOL_ATTRS, str_truthy};
use crate::renderer::{BOOL_ATTRS, ssr_attr_name, str_truthy};
use dioxus_core::{TemplateAttribute, TemplateNode, VNode};
use std::{fmt::Write, ops::AddAssign};

Expand Down Expand Up @@ -210,11 +210,12 @@ fn from_template_recursive(
value,
namespace,
} => {
if *name == "dangerous_inner_html" {
let name = ssr_attr_name(name);
if name == "dangerous_inner_html" {
inner_html = Some(value);
} else if let Some("style") = namespace {
styles.push((name, value));
} else if BOOL_ATTRS.contains(name) {
} else if BOOL_ATTRS.contains(&name) {
if str_truthy(value) {
write!(
chain,
Expand Down
15 changes: 13 additions & 2 deletions packages/ssr/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ impl Renderer {
Segment::Attr(idx) => {
let attrs = &*template.dynamic_attrs[*idx];
for attr in attrs {
let translated_name = ssr_attr_name(attr.name);
if attr.name == "dangerous_inner_html" {
inner_html = Some(attr);
} else if attr.namespace == Some("style") {
accumulated_dynamic_styles.push(attr);
} else if BOOL_ATTRS.contains(&attr.name) {
} else if BOOL_ATTRS.contains(&translated_name) {
if truthy(&attr.value) {
write_attribute(buf, attr)?;
}
Expand Down Expand Up @@ -482,11 +483,21 @@ pub(crate) fn truthy(value: &AttributeValue) -> bool {
}
}

/// Translate Dioxus virtual attribute names to their HTML equivalents for SSR.
pub(crate) fn ssr_attr_name(name: &str) -> &str {
match name {
"initial_value" => "value",
"initial_checked" => "checked",
"initial_selected" => "selected",
other => other,
}
}

pub(crate) fn write_attribute<W: Write + ?Sized>(
buf: &mut W,
attr: &Attribute,
) -> std::fmt::Result {
let name = &attr.name;
let name = ssr_attr_name(attr.name);
match &attr.value {
AttributeValue::Text(value) => write!(
buf,
Expand Down
46 changes: 46 additions & 0 deletions packages/ssr/tests/initial_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use dioxus::prelude::*;

#[test]
fn initial_value_renders_as_value() {
assert_eq!(
dioxus_ssr::render_element(rsx! {
input { initial_value: "hello" }
}),
r#"<input value="hello"/>"#
);
}

#[test]
fn initial_checked_renders_as_checked() {
assert_eq!(
dioxus_ssr::render_element(rsx! {
input { r#type: "checkbox", initial_checked: true }
}),
r#"<input type="checkbox" checked=true/>"#
);
}

#[test]
fn initial_selected_renders_as_selected() {
assert_eq!(
dioxus_ssr::render_element(rsx! {
option { initial_selected: true }
}),
r#"<option selected=true></option>"#
);
}

#[test]
fn dynamic_initial_value() {
fn app() -> Element {
let value = "dynamic";
rsx! {
input { initial_value: value }
}
}

let mut dom = VirtualDom::new(app);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extrapolate the function to segment to call the var_func after instantiating

dom.rebuild(&mut dioxus_core::NoOpMutations);

assert_eq!(dioxus_ssr::render(&dom), r#"<input value="dynamic"/>"#);
}
Loading