Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Improves merging logic for style attributes
  • Loading branch information
jjvn84 committed Aug 5, 2025
commit 7ba52793cacca06c8dab6e9970ca71b4f08e2d70
106 changes: 102 additions & 4 deletions packages/core/src/diff/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::innerlude::MountId;
use crate::{Attribute, AttributeValue, DynamicNode::*};
use crate::{Attribute, AttributeValue, DynamicNode::*, TemplateAttribute};
use crate::{VNode, VirtualDom, WriteMutations};
use core::iter::Peekable;

Expand Down Expand Up @@ -413,6 +413,103 @@ impl VNode {
}
}

fn merge_dynamic_attributes(&self) -> Box<[Box<[Attribute]>]> {
let static_style_strings = self
.template
.roots
.iter()
.map(|root| {
let mut style_string = String::from("");
if let TemplateNode::Element { attrs, .. } = root {
attrs.iter().for_each(|attribute| {
if let TemplateAttribute::Static {
name,
value,
namespace,
} = attribute
{
if *name == "style" {
style_string = format!("{style_string} {value}");
} else if *namespace == Some("style") {
style_string = format!("{style_string} {name}: {value};");
}
}
});
}
style_string.trim().to_string()
})
.collect::<Vec<String>>();

let dynamic_style_strings = self
.dynamic_attrs
.iter()
.map(|attributes| {
let mut style_string = String::from("");
attributes.iter().for_each(|attribute| {
if let AttributeValue::Text(attribute_value) = &attribute.value {
if attribute.name == "style" {
style_string = format!("{style_string} {attribute_value}");
} else if attribute.namespace == Some("style") {
style_string = format!(
"{} {}: {};",
style_string, attribute.name, attribute_value
);
}
}
});
style_string.trim().to_string()
})
.collect::<Vec<String>>();

// Merge style static attribures with style dynamic attributes
self.dynamic_attrs
.iter()
.enumerate()
.map(|(idx, attributes)| {
let path = self.template.attr_paths[idx];
attributes
.iter()
.map(|attribute| {
// Only merge if the current attribute is a style attribute
if attribute.name == "style" || attribute.namespace == Some("style") {
// Get the static styles for the corresponding element
let static_styles = match static_style_strings.get(path[0] as usize) {
Some(static_style) => static_style,
None => &String::from(""),
};
// There might be more than one dynamic style for the same element
let dynamic_styles = dynamic_style_strings
.iter()
.enumerate()
.filter_map(|(current_idx, style_string)| {
if path == self.template.attr_paths[current_idx] {
Some(String::from(style_string))
} else {
None
}
})
.collect::<Vec<String>>()
.join(" ");

Attribute {
name: "style",
value: AttributeValue::Text(
format!("{static_styles} {dynamic_styles}")
.trim()
.to_string(),
),
namespace: None,
volatile: attribute.volatile,
}
} else {
attribute.clone() // If this is not a style attribute, just pass it along
}
})
.collect::<Box<[Attribute]>>()
})
.collect::<Box<[Box<[Attribute]>]>>()
}

pub(super) fn diff_attributes(
&self,
new: &VNode,
Expand All @@ -421,9 +518,9 @@ impl VNode {
) {
let mount_id = new.mount.get();
for (idx, (old_attrs, new_attrs)) in self
.dynamic_attrs
.merge_dynamic_attributes()
.iter()
.zip(new.dynamic_attrs.iter())
.zip(new.merge_dynamic_attributes().iter())
.enumerate()
{
let mut old_attributes_iter = old_attrs.iter().peekable();
Expand Down Expand Up @@ -803,10 +900,11 @@ impl VNode {
let mut last_path = None;
// Only take nodes that are under this root node
let from_root_node = |(_, path): &(usize, &[u8])| path.first() == Some(&root_idx);
let merged_dyamic_attributes = &self.merge_dynamic_attributes();
while let Some((attribute_idx, attribute_path)) =
dynamic_attrbiutes_iter.next_if(from_root_node)
{
let attribute = &self.dynamic_attrs[attribute_idx];
let attribute = &merged_dyamic_attributes[attribute_idx];

let id = match last_path {
// If the last path was exactly the same, we can reuse the id
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/many_roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ fn many_roots() {
// Set the width attribute first
AssignId { path: &[2], id: ElementId(2,) },
SetAttribute {
name: "width",
ns: Some("style",),
value: AttributeValue::Text("100%".to_string()),
name: "style",
ns: None,
value: AttributeValue::Text("width: 100%;".to_string()),
id: ElementId(2,),
},
// Load MyOutlet next
Expand Down
2 changes: 1 addition & 1 deletion packages/interpreter/src/js/common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading