remove if blocks around attribute updates (#10917)

pull/10925/head
Rich Harris 9 months ago committed by GitHub
parent 7e584e40d7
commit 59ff650ccb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: reliably remove undefined attributes during hydration

@ -0,0 +1,5 @@
---
'svelte': patch
---
feat: shorter compiler output for attribute updates

@ -519,13 +519,11 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
b.thunk(singular) b.thunk(singular)
) )
), ),
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, singular)), grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, singular))
skip_condition: true
}; };
} }
return { return {
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, value)), grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, value))
skip_condition: true
}; };
} else if (!DOMProperties.includes(name)) { } else if (!DOMProperties.includes(name)) {
if (singular) { if (singular) {
@ -564,15 +562,12 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
}; };
if (attribute.metadata.dynamic) { if (attribute.metadata.dynamic) {
const id = state.scope.generate(`${node_id.name}_${name}`); const { grouped, singular } = assign(grouped_value, value);
serialize_update_assignment( if (contains_call_expression && singular) {
state, state.init.push(singular);
id, } else {
name === 'class' ? b.literal('') : undefined, state.update.push({ singular, grouped });
grouped_value, }
assign(b.id(id), value),
contains_call_expression
);
return true; return true;
} else { } else {
state.init.push(assign(grouped_value).grouped); state.init.push(assign(grouped_value).grouped);

@ -1 +1 @@
<!--ssr:0--><div></div><!--ssr:0--> <div></div><div></div><div></div>

@ -2,4 +2,11 @@
export let id; export let id;
</script> </script>
<!-- single update -->
<div id={id}></div> <div id={id}></div>
{#if true}
<!-- grouped updates -->
<div id={id}></div>
<div id={id}></div>
{/if}

@ -13,9 +13,7 @@ export default function Main($$anchor, $$props) {
let y = () => 'test'; let y = () => 'test';
var fragment = $.open_frag($$anchor, frag, false); var fragment = $.open_frag($$anchor, frag, false);
var div = $.first_child(fragment); var div = $.first_child(fragment);
var div_foobar;
var svg = $.sibling($.sibling(div, true)); var svg = $.sibling($.sibling(div, true));
var svg_viewBox;
var custom_element = $.sibling($.sibling(svg, true)); var custom_element = $.sibling($.sibling(svg, true));
var custom_element_fooBar; var custom_element_fooBar;
var div_1 = $.sibling($.sibling(custom_element, true)); var div_1 = $.sibling($.sibling(custom_element, true));
@ -31,13 +29,8 @@ export default function Main($$anchor, $$props) {
$.set_custom_element_data_effect(custom_element_1, "fooBar", y); $.set_custom_element_data_effect(custom_element_1, "fooBar", y);
$.render_effect(() => { $.render_effect(() => {
if (div_foobar !== (div_foobar = x)) { $.attr(div, "foobar", x);
$.attr(div, "foobar", div_foobar); $.attr(svg, "viewBox", x);
}
if (svg_viewBox !== (svg_viewBox = x)) {
$.attr(svg, "viewBox", svg_viewBox);
}
if (custom_element_fooBar !== (custom_element_fooBar = x)) { if (custom_element_fooBar !== (custom_element_fooBar = x)) {
$.set_custom_element_data(custom_element, "fooBar", custom_element_fooBar); $.set_custom_element_data(custom_element, "fooBar", custom_element_fooBar);

Loading…
Cancel
Save