~repos /tide-jsx

#rust#proc-macro#jsx

git clone https://pyrossh.dev/repos/tide-jsx.git

Tide + JSX


534f9b73 vpzomtrrfrt

5 years ago
Use Cow for element attribute values (#21)
render/src/simple_element.rs CHANGED
@@ -1,9 +1,10 @@
1
1
  use crate::html_escaping::escape_html;
2
2
  use crate::Render;
3
+ use std::borrow::Cow;
3
4
  use std::collections::HashMap;
4
5
  use std::fmt::{Result, Write};
5
6
 
6
- type Attributes<'a> = Option<HashMap<&'a str, &'a str>>;
7
+ type Attributes<'a> = Option<HashMap<&'a str, Cow<'a, str>>>;
7
8
 
8
9
  /// Simple HTML element tag
9
10
  #[derive(Debug)]
@@ -20,7 +21,7 @@ fn write_attributes<'a, W: Write>(maybe_attributes: Attributes<'a>, writer: &mut
20
21
  Some(mut attributes) => {
21
22
  for (key, value) in attributes.drain() {
22
23
  write!(writer, " {}=\"", key)?;
23
- escape_html(value, writer)?;
24
+ escape_html(&value, writer)?;
24
25
  write!(writer, "\"")?;
25
26
  }
26
27
  Ok(())
render_macros/src/element_attributes.rs CHANGED
@@ -131,13 +131,13 @@ impl<'a> ToTokens for SimpleElementAttributes<'a> {
131
131
  let value = attribute.value_tokens();
132
132
 
133
133
  quote! {
134
- hm.insert(#ident, #value);
134
+ hm.insert(#ident, ::std::borrow::Cow::from(#value));
135
135
  }
136
136
  })
137
137
  .collect();
138
138
 
139
139
  let hashmap_declaration = quote! {{
140
- let mut hm = std::collections::HashMap::<&str, &str>::new();
140
+ let mut hm = std::collections::HashMap::<&str, ::std::borrow::Cow<'_, str>>::new();
141
141
  #(#attrs)*
142
142
  Some(hm)
143
143
  }};
render_tests/src/lib.rs CHANGED
@@ -98,6 +98,26 @@ fn some_none() {
98
98
  assert_eq!(html! { <Answer a={44} /> }, "");
99
99
  }
100
100
 
101
+ #[test]
102
+ fn owned_string() {
103
+ use pretty_assertions::assert_eq;
104
+ use render::{component, html, rsx};
105
+
106
+ #[component]
107
+ fn Welcome<'kind, 'name>(kind: &'kind str, name: &'name str) {
108
+ rsx! {
109
+ <h1 class={format!("{}-title", kind)}>
110
+ {format!("Hello, {}", name)}
111
+ </h1>
112
+ }
113
+ }
114
+
115
+ assert_eq!(
116
+ html! { <Welcome kind={"alien"} name={"Yoda"} /> },
117
+ r#"<h1 class="alien-title">Hello, Yoda</h1>"#
118
+ );
119
+ }
120
+
101
121
  mod kaki {
102
122
  // A simple HTML 5 doctype declaration
103
123
  use render::html::HTML5Doctype;