Skip to content

Commit 6c85339

Browse files
committed
remove if blocks around attribute updates
1 parent 7e584e4 commit 6c85339

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

.changeset/lovely-houses-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: reliably remove undefined attributes during hydration

.changeset/stale-fans-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
feat: shorter compiler output for attribute updates

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,11 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
519519
b.thunk(singular)
520520
)
521521
),
522-
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, singular)),
523-
skip_condition: true
522+
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, singular))
524523
};
525524
}
526525
return {
527-
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, value)),
528-
skip_condition: true
526+
grouped: b.stmt(b.call(is_svg ? '$.svg_class_name' : '$.class_name', node_id, value))
529527
};
530528
} else if (!DOMProperties.includes(name)) {
531529
if (singular) {
@@ -564,15 +562,12 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
564562
};
565563

566564
if (attribute.metadata.dynamic) {
567-
const id = state.scope.generate(`${node_id.name}_${name}`);
568-
serialize_update_assignment(
569-
state,
570-
id,
571-
name === 'class' ? b.literal('') : undefined,
572-
grouped_value,
573-
assign(b.id(id), value),
574-
contains_call_expression
575-
);
565+
const { grouped, singular } = assign(grouped_value, value);
566+
if (contains_call_expression && singular) {
567+
state.init.push(singular);
568+
} else {
569+
state.update.push({ singular, grouped });
570+
}
576571
return true;
577572
} else {
578573
state.init.push(assign(grouped_value).grouped);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!--ssr:0--><div></div><!--ssr:0-->
1+
<div></div><div></div><div></div>

packages/svelte/tests/hydration/samples/element-attribute-removed/main.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22
export let id;
33
</script>
44

5+
<!-- single update -->
56
<div id={id}></div>
7+
8+
{#if true}
9+
<!-- grouped updates -->
10+
<div id={id}></div>
11+
<div id={id}></div>
12+
{/if}

packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export default function Main($$anchor, $$props) {
1313
let y = () => 'test';
1414
var fragment = $.open_frag($$anchor, frag, false);
1515
var div = $.first_child(fragment);
16-
var div_foobar;
1716
var svg = $.sibling($.sibling(div, true));
18-
var svg_viewBox;
1917
var custom_element = $.sibling($.sibling(svg, true));
2018
var custom_element_fooBar;
2119
var div_1 = $.sibling($.sibling(custom_element, true));
@@ -31,13 +29,8 @@ export default function Main($$anchor, $$props) {
3129
$.set_custom_element_data_effect(custom_element_1, "fooBar", y);
3230

3331
$.render_effect(() => {
34-
if (div_foobar !== (div_foobar = x)) {
35-
$.attr(div, "foobar", div_foobar);
36-
}
37-
38-
if (svg_viewBox !== (svg_viewBox = x)) {
39-
$.attr(svg, "viewBox", svg_viewBox);
40-
}
32+
$.attr(div, "foobar", x);
33+
$.attr(svg, "viewBox", x);
4134

4235
if (custom_element_fooBar !== (custom_element_fooBar = x)) {
4336
$.set_custom_element_data(custom_element, "fooBar", custom_element_fooBar);

0 commit comments

Comments
 (0)