fix dangerous_inner_html with SSR

This commit is contained in:
Evan Almloff committed 2023-04-07 09:35:32 -05:00
1 parent 61d100e1f8
commit fe75138e42
3 files changed
+62 -3

No files matched your search

+21 -2
View File
@@ -70,6 +70,8 @@ impl Renderer {
.or_insert_with(|| Rc::new(StringCache::from_template(template).unwrap()))
.clone();
let mut inner_html = None;
// We need to keep track of the dynamic styles so we can insert them into the right place
let mut accumulated_dynamic_styles = Vec::new();
@@ -77,7 +79,9 @@ impl Renderer {
match segment {
Segment::Attr(idx) => {
let attr = &template.dynamic_attrs[*idx];
if attr.namespace == Some("style") {
if attr.name == "dangerous_inner_html" {
inner_html = Some(attr);
} else if attr.namespace == Some("style") {
accumulated_dynamic_styles.push(attr);
} else {
match attr.value {
@@ -165,6 +169,19 @@ impl Renderer {
accumulated_dynamic_styles.clear();
}
}
Segment::InnerHtmlMarker => {
if let Some(inner_html) = inner_html.take() {
let inner_html = &inner_html.value;
match inner_html {
AttributeValue::Text(value) => write!(buf, "{}", value)?,
AttributeValue::Bool(value) => write!(buf, "{}", value)?,
AttributeValue::Float(f) => write!(buf, "{}", f)?,
AttributeValue::Int(i) => write!(buf, "{}", i)?,
_ => {}
}
}
}
}
}
@@ -208,7 +225,9 @@ fn to_string_works() {
StyleMarker {
inside_style_tag: false,
},
PreRendered(">Hello world 1 -->".into(),),
PreRendered(">".into()),
InnerHtmlMarker,
PreRendered("Hello world 1 -->".into(),),
Node(0,),
PreRendered(
"&lt;-- Hello world 2<div>nest 1</div><div></div><div>nest 2</div>".into(),