diff --git a/packages/core/src/diff/node.rs b/packages/core/src/diff/node.rs index e239fcb48f..4d282573a4 100644 --- a/packages/core/src/diff/node.rs +++ b/packages/core/src/diff/node.rs @@ -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; @@ -413,6 +413,104 @@ impl VNode { } } + fn merge_dynamic_attributes(&self) -> Box<[Box<[Attribute]>]> { + 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::>(); + + // Merge style static attributes 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 mut path_iterator = path.iter(); + path_iterator.next(); + let mut element = self.template.roots[path[0] as usize]; + for next_index in path_iterator { + if let TemplateNode::Element { children, .. } = element { + element = children[*next_index as usize] + } else { + unreachable!("All nodes in the attr_paths should be TemplateNode::Element"); + } + } + + let mut static_styles = String::from(""); + if let TemplateNode::Element { attrs, .. } = element { + attrs.iter().for_each(|attribute| { + if let TemplateAttribute::Static { + name, + value, + namespace, + } = attribute + { + if *name == "style" { + static_styles = format!("{static_styles} {value}"); + } else if *namespace == Some("style") { + static_styles = + format!("{static_styles} {name}: {value};"); + } + } + }); + } + static_styles = static_styles.trim().to_string(); + + // 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::>() + .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::>() + }) + .collect::]>>() + } + pub(super) fn diff_attributes( &self, new: &VNode, @@ -421,9 +519,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(); @@ -803,10 +901,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 diff --git a/packages/core/tests/many_roots.rs b/packages/core/tests/many_roots.rs index c954df52bb..28c93a6f95 100644 --- a/packages/core/tests/many_roots.rs +++ b/packages/core/tests/many_roots.rs @@ -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 diff --git a/packages/interpreter/src/js/common.js b/packages/interpreter/src/js/common.js index 3bbd96df38..626ef0f390 100644 --- a/packages/interpreter/src/js/common.js +++ b/packages/interpreter/src/js/common.js @@ -1 +1 @@ -function setAttributeInner(node,field,value,ns){if(ns==="style"){node.style.setProperty(field,value);return}if(ns){node.setAttributeNS(ns,field,value);return}switch(field){case"value":if(node.tagName==="OPTION")setAttributeDefault(node,field,value);else if(node.value!==value)node.value=value;break;case"initial_value":node.defaultValue=value;break;case"checked":node.checked=truthy(value);break;case"initial_checked":node.defaultChecked=truthy(value);break;case"selected":node.selected=truthy(value);break;case"initial_selected":node.defaultSelected=truthy(value);break;case"dangerous_inner_html":node.innerHTML=value;break;case"style":let existingStyles={};for(let i=0;i{if(contents[key])contents[key].push(value);else contents[key]=[value]}),{valid:form.checkValidity(),values:contents}}export{setAttributeInner,retrieveFormValues}; +function setAttributeInner(node,field,value,ns){if(ns){node.setAttributeNS(ns,field,value);return}switch(field){case"value":if(node.tagName==="OPTION")setAttributeDefault(node,field,value);else if(node.value!==value)node.value=value;break;case"initial_value":node.defaultValue=value;break;case"checked":node.checked=truthy(value);break;case"initial_checked":node.defaultChecked=truthy(value);break;case"selected":node.selected=truthy(value);break;case"initial_selected":node.defaultSelected=truthy(value);break;case"dangerous_inner_html":node.innerHTML=value;break;case"style":node.setAttribute(field,value);break;case"multiple":if(setAttributeDefault(node,field,value),node.options!==null&&node.options!==void 0){let options=node.options;for(let option of options)option.selected=option.defaultSelected}break;default:setAttributeDefault(node,field,value)}}function setAttributeDefault(node,field,value){if(!truthy(value)&&isBoolAttr(field))node.removeAttribute(field);else node.setAttribute(field,value)}function truthy(val){return val==="true"||val===!0}function isBoolAttr(field){switch(field){case"allowfullscreen":case"allowpaymentrequest":case"async":case"autofocus":case"autoplay":case"checked":case"controls":case"default":case"defer":case"disabled":case"formnovalidate":case"hidden":case"ismap":case"itemscope":case"loop":case"multiple":case"muted":case"nomodule":case"novalidate":case"open":case"playsinline":case"readonly":case"required":case"reversed":case"selected":case"truespeed":case"webkitdirectory":return!0;default:return!1}}function retrieveFormValues(form){let formData=new FormData(form),contents={};return formData.forEach((value,key)=>{if(contents[key])contents[key].push(value);else contents[key]=[value]}),{valid:form.checkValidity(),values:contents}}export{setAttributeInner,retrieveFormValues}; diff --git a/packages/interpreter/src/js/core.js b/packages/interpreter/src/js/core.js index 0d7269f55b..f5934d2060 100644 --- a/packages/interpreter/src/js/core.js +++ b/packages/interpreter/src/js/core.js @@ -1 +1 @@ -function setAttributeInner(node,field,value,ns){if(ns==="style"){node.style.setProperty(field,value);return}if(ns){node.setAttributeNS(ns,field,value);return}switch(field){case"value":if(node.tagName==="OPTION")setAttributeDefault(node,field,value);else if(node.value!==value)node.value=value;break;case"initial_value":node.defaultValue=value;break;case"checked":node.checked=truthy(value);break;case"initial_checked":node.defaultChecked=truthy(value);break;case"selected":node.selected=truthy(value);break;case"initial_selected":node.defaultSelected=truthy(value);break;case"dangerous_inner_html":node.innerHTML=value;break;case"style":let existingStyles={};for(let i=0;i{for(let entry of entries)this.handleResizeEvent(entry)});this.resizeObserver.observe(element)}removeResizeObserver(element){if(this.resizeObserver)this.resizeObserver.unobserve(element)}handleIntersectionEvent(entry){let target=entry.target,event=new CustomEvent("visible",{bubbles:!1,detail:entry});target.dispatchEvent(event)}createIntersectionObserver(element){if(!this.intersectionObserver)this.intersectionObserver=new IntersectionObserver((entries)=>{for(let entry of entries)this.handleIntersectionEvent(entry)});this.intersectionObserver.observe(element)}removeIntersectionObserver(element){if(this.intersectionObserver)this.intersectionObserver.unobserve(element)}createListener(event_name,element,bubbles){if(event_name=="resize")this.createResizeObserver(element);else if(event_name=="visible")this.createIntersectionObserver(element);if(bubbles)if(this.global[event_name]===void 0)this.global[event_name]={active:1,callback:this.handler},this.root.addEventListener(event_name,this.handler);else this.global[event_name].active++;else{let id=element.getAttribute("data-dioxus-id");if(!this.local[id])this.local[id]={};element.addEventListener(event_name,this.handler)}}removeListener(element,event_name,bubbles){if(event_name=="resize")this.removeResizeObserver(element);else if(event_name=="visible")this.removeIntersectionObserver(element);else if(bubbles)this.removeBubblingListener(event_name);else this.removeNonBubblingListener(element,event_name)}removeBubblingListener(event_name){if(this.global[event_name].active--,this.global[event_name].active===0)this.root.removeEventListener(event_name,this.global[event_name].callback),delete this.global[event_name]}removeNonBubblingListener(element,event_name){let id=element.getAttribute("data-dioxus-id");if(delete this.local[id][event_name],Object.keys(this.local[id]).length===0)delete this.local[id];element.removeEventListener(event_name,this.handler)}removeAllNonBubblingListeners(element){let id=element.getAttribute("data-dioxus-id");delete this.local[id]}getNode(id){return this.nodes[id]}pushRoot(node){this.stack.push(node)}appendChildren(id,many){let root=this.nodes[id],els=this.stack.splice(this.stack.length-many);for(let k=0;k0;end--)node=node.nextSibling}return node}saveTemplate(nodes,tmpl_id){this.templates[tmpl_id]=nodes}hydrate_node(hydrateNode,ids){let split=hydrateNode.getAttribute("data-node-hydration").split(","),id=ids[parseInt(split[0])];if(this.nodes[id]=hydrateNode,split.length>1){hydrateNode.listening=split.length-1,hydrateNode.setAttribute("data-dioxus-id",id.toString());for(let j=1;j{if(!treeWalker.nextNode())return!1;return treeWalker.currentNode!==nextSibling};while(treeWalker.currentNode){let currentNode=treeWalker.currentNode;if(currentNode.nodeType===Node.COMMENT_NODE){let id=currentNode.textContent,placeholderSplit=id.split("placeholder");if(placeholderSplit.length>1){if(this.nodes[ids[parseInt(placeholderSplit[1])]]=currentNode,!continueToNextNode())break;continue}let textNodeSplit=id.split("node-id");if(textNodeSplit.length>1){let next=currentNode.nextSibling;currentNode.remove();let commentAfterText,textNode;if(next.nodeType===Node.COMMENT_NODE){let newText=next.parentElement.insertBefore(document.createTextNode(""),next);commentAfterText=next,textNode=newText}else textNode=next,commentAfterText=textNode.nextSibling;treeWalker.currentNode=commentAfterText,this.nodes[ids[parseInt(textNodeSplit[1])]]=textNode;let exit=currentNode===under||!continueToNextNode();if(commentAfterText.remove(),exit)break;continue}}if(!continueToNextNode())break}}}setAttributeInner(node,field,value,ns){setAttributeInner(node,field,value,ns)}}export{BaseInterpreter}; +function setAttributeInner(node,field,value,ns){if(ns){node.setAttributeNS(ns,field,value);return}switch(field){case"value":if(node.tagName==="OPTION")setAttributeDefault(node,field,value);else if(node.value!==value)node.value=value;break;case"initial_value":node.defaultValue=value;break;case"checked":node.checked=truthy(value);break;case"initial_checked":node.defaultChecked=truthy(value);break;case"selected":node.selected=truthy(value);break;case"initial_selected":node.defaultSelected=truthy(value);break;case"dangerous_inner_html":node.innerHTML=value;break;case"style":node.setAttribute(field,value);break;case"multiple":if(setAttributeDefault(node,field,value),node.options!==null&&node.options!==void 0){let options=node.options;for(let option of options)option.selected=option.defaultSelected}break;default:setAttributeDefault(node,field,value)}}function setAttributeDefault(node,field,value){if(!truthy(value)&&isBoolAttr(field))node.removeAttribute(field);else node.setAttribute(field,value)}function truthy(val){return val==="true"||val===!0}function isBoolAttr(field){switch(field){case"allowfullscreen":case"allowpaymentrequest":case"async":case"autofocus":case"autoplay":case"checked":case"controls":case"default":case"defer":case"disabled":case"formnovalidate":case"hidden":case"ismap":case"itemscope":case"loop":case"multiple":case"muted":case"nomodule":case"novalidate":case"open":case"playsinline":case"readonly":case"required":case"reversed":case"selected":case"truespeed":case"webkitdirectory":return!0;default:return!1}}class BaseInterpreter{global;local;root;handler;resizeObserver;intersectionObserver;nodes;stack;templates;m;constructor(){}initialize(root,handler=null){this.global={},this.local={},this.root=root,this.nodes=[root],this.stack=[root],this.templates={},this.handler=handler,root.setAttribute("data-dioxus-id","0")}handleResizeEvent(entry){let target=entry.target,event=new CustomEvent("resize",{bubbles:!1,detail:entry});target.dispatchEvent(event)}createResizeObserver(element){if(!this.resizeObserver)this.resizeObserver=new ResizeObserver((entries)=>{for(let entry of entries)this.handleResizeEvent(entry)});this.resizeObserver.observe(element)}removeResizeObserver(element){if(this.resizeObserver)this.resizeObserver.unobserve(element)}handleIntersectionEvent(entry){let target=entry.target,event=new CustomEvent("visible",{bubbles:!1,detail:entry});target.dispatchEvent(event)}createIntersectionObserver(element){if(!this.intersectionObserver)this.intersectionObserver=new IntersectionObserver((entries)=>{for(let entry of entries)this.handleIntersectionEvent(entry)});this.intersectionObserver.observe(element)}removeIntersectionObserver(element){if(this.intersectionObserver)this.intersectionObserver.unobserve(element)}createListener(event_name,element,bubbles){if(event_name=="resize")this.createResizeObserver(element);else if(event_name=="visible")this.createIntersectionObserver(element);if(bubbles)if(this.global[event_name]===void 0)this.global[event_name]={active:1,callback:this.handler},this.root.addEventListener(event_name,this.handler);else this.global[event_name].active++;else{let id=element.getAttribute("data-dioxus-id");if(!this.local[id])this.local[id]={};element.addEventListener(event_name,this.handler)}}removeListener(element,event_name,bubbles){if(event_name=="resize")this.removeResizeObserver(element);else if(event_name=="visible")this.removeIntersectionObserver(element);else if(bubbles)this.removeBubblingListener(event_name);else this.removeNonBubblingListener(element,event_name)}removeBubblingListener(event_name){if(this.global[event_name].active--,this.global[event_name].active===0)this.root.removeEventListener(event_name,this.global[event_name].callback),delete this.global[event_name]}removeNonBubblingListener(element,event_name){let id=element.getAttribute("data-dioxus-id");if(delete this.local[id][event_name],Object.keys(this.local[id]).length===0)delete this.local[id];element.removeEventListener(event_name,this.handler)}removeAllNonBubblingListeners(element){let id=element.getAttribute("data-dioxus-id");delete this.local[id]}getNode(id){return this.nodes[id]}pushRoot(node){this.stack.push(node)}appendChildren(id,many){let root=this.nodes[id],els=this.stack.splice(this.stack.length-many);for(let k=0;k0;end--)node=node.nextSibling}return node}saveTemplate(nodes,tmpl_id){this.templates[tmpl_id]=nodes}hydrate_node(hydrateNode,ids){let split=hydrateNode.getAttribute("data-node-hydration").split(","),id=ids[parseInt(split[0])];if(this.nodes[id]=hydrateNode,split.length>1){hydrateNode.listening=split.length-1,hydrateNode.setAttribute("data-dioxus-id",id.toString());for(let j=1;j{if(!treeWalker.nextNode())return!1;return treeWalker.currentNode!==nextSibling};while(treeWalker.currentNode){let currentNode=treeWalker.currentNode;if(currentNode.nodeType===Node.COMMENT_NODE){let id=currentNode.textContent,placeholderSplit=id.split("placeholder");if(placeholderSplit.length>1){if(this.nodes[ids[parseInt(placeholderSplit[1])]]=currentNode,!continueToNextNode())break;continue}let textNodeSplit=id.split("node-id");if(textNodeSplit.length>1){let next=currentNode.nextSibling;currentNode.remove();let commentAfterText,textNode;if(next.nodeType===Node.COMMENT_NODE){let newText=next.parentElement.insertBefore(document.createTextNode(""),next);commentAfterText=next,textNode=newText}else textNode=next,commentAfterText=textNode.nextSibling;treeWalker.currentNode=commentAfterText,this.nodes[ids[parseInt(textNodeSplit[1])]]=textNode;let exit=currentNode===under||!continueToNextNode();if(commentAfterText.remove(),exit)break;continue}}if(!continueToNextNode())break}}}setAttributeInner(node,field,value,ns){setAttributeInner(node,field,value,ns)}}export{BaseInterpreter}; diff --git a/packages/interpreter/src/js/hash.txt b/packages/interpreter/src/js/hash.txt index 407f37e412..fed7ae98fd 100644 --- a/packages/interpreter/src/js/hash.txt +++ b/packages/interpreter/src/js/hash.txt @@ -1 +1 @@ -[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 14692042304579321130, 5052021921702764563, 7041565893588933645, 11339769846046015954] \ No newline at end of file +[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 14692042304579321130, 5052021921702764563, 7041565893588933645, 9730830276431182066] \ No newline at end of file diff --git a/packages/interpreter/src/ts/set_attribute.ts b/packages/interpreter/src/ts/set_attribute.ts index 093c9f79a0..5f0dad5a34 100644 --- a/packages/interpreter/src/ts/set_attribute.ts +++ b/packages/interpreter/src/ts/set_attribute.ts @@ -7,12 +7,6 @@ export function setAttributeInner( value: string, ns: string ) { - // we support a single namespace by default: style - if (ns === "style") { - node.style.setProperty(field, value); - return; - } - // If there's a namespace, use setAttributeNS (svg, mathml, etc.) if (!!ns) { node.setAttributeNS(ns, field, value); @@ -65,22 +59,8 @@ export function setAttributeInner( break; case "style": - // Save the existing styles - const existingStyles: Record = {}; - - for (let i = 0; i < node.style.length; i++) { - const prop = node.style[i]; - existingStyles[prop] = node.style.getPropertyValue(prop); - } // Override all styles node.setAttribute(field, value); - // Restore the old styles - for (const prop in existingStyles) { - // If it wasn't overridden, restore it - if (!node.style.getPropertyValue(prop)) { - node.style.setProperty(prop, existingStyles[prop]); - } - } break; case "multiple": diff --git a/packages/interpreter/src/write_native_mutations.rs b/packages/interpreter/src/write_native_mutations.rs index afb5d33e03..2fa758cd52 100644 --- a/packages/interpreter/src/write_native_mutations.rs +++ b/packages/interpreter/src/write_native_mutations.rs @@ -53,6 +53,7 @@ impl MutationState { None => self.channel.create_element(tag), } // Set attributes on the current node + let mut style_string = String::from(""); for attr in *attrs { if let TemplateAttribute::Static { name, @@ -60,10 +61,23 @@ impl MutationState { namespace, } = attr { - self.channel - .set_top_attribute(name, value, namespace.unwrap_or_default()) + if *name == "style" { + style_string = format!("{style_string} {value}"); + } else if *namespace == Some("style") { + style_string = format!("{style_string} {name}: {value};"); + } else { + self.channel.set_top_attribute( + name, + value, + namespace.unwrap_or_default(), + ) + } } } + if !style_string.is_empty() { + self.channel + .set_top_attribute("style", style_string.trim(), "") + } // Add each child to the stack for child in *children { self.create_template_node(child); diff --git a/packages/web/src/mutations.rs b/packages/web/src/mutations.rs index ac511abb2d..ac432c2fb9 100644 --- a/packages/web/src/mutations.rs +++ b/packages/web/src/mutations.rs @@ -22,6 +22,7 @@ impl WebsysDom { Some(ns) => self.document.create_element_ns(Some(ns), tag).unwrap(), None => self.document.create_element(tag).unwrap(), }; + let mut style_string = String::from(""); for attr in *attrs { if let TemplateAttribute::Static { name, @@ -29,14 +30,28 @@ impl WebsysDom { namespace, } = attr { - minimal_bindings::setAttributeInner( - el.clone().into(), - name, - JsValue::from_str(value), - *namespace, - ); + if *name == "style" { + style_string = format!("{style_string} {value}"); + } else if *namespace == Some("style") { + style_string = format!("{style_string} {name}: {value};"); + } else { + minimal_bindings::setAttributeInner( + el.clone().into(), + name, + JsValue::from_str(value), + *namespace, + ); + } } } + if !style_string.is_empty() { + minimal_bindings::setAttributeInner( + el.clone().into(), + "style", + JsValue::from_str(style_string.trim()), + None, + ); + } for child in *children { let _ = el.append_child(&self.create_template_node(child)); }