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

+15 -1
View File
@@ -23,6 +23,8 @@ pub enum Segment {
// This will be true if there are static styles
inside_style_tag: bool,
},
/// A marker for where to insert a dynamic inner html
InnerHtmlMarker,
}
impl std::fmt::Write for StringChain {
@@ -69,6 +71,9 @@ impl StringCache {
write!(chain, "<{tag}")?;
// we need to collect the styles and write them at the end
let mut styles = Vec::new();
// we need to collect the inner html and write it at the end
let mut inner_html = None;
// we need to keep track of if we have dynamic attrs to know if we need to insert a style and inner_html marker
let mut has_dynamic_attrs = false;
for attr in *attrs {
match attr {
@@ -77,7 +82,9 @@ impl StringCache {
value,
namespace,
} => {
if let Some("style") = namespace {
if *name == "dangerous_inner_html" {
inner_html = Some(value);
} else if let Some("style") = namespace {
styles.push((name, value));
} else {
write!(chain, " {name}=\"{value}\"")?;
@@ -110,6 +117,13 @@ impl StringCache {
write!(chain, "/>")?;
} else {
write!(chain, ">")?;
// Write the static inner html, or insert a marker if dynamic inner html is possible
if let Some(inner_html) = inner_html {
chain.write_str(inner_html)?;
} else if has_dynamic_attrs {
chain.segments.push(Segment::InnerHtmlMarker);
}
for child in *children {
Self::recurse(child, cur_path, root_idx, chain)?;
}