Skip to content

fix: spreading style is not consistent with attribute #15323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/real-cameras-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: always use `setAttribute` when setting `style`
17 changes: 9 additions & 8 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export function set_custom_element_data(node, prop, value) {
// or effect
var previous_reaction = active_reaction;
var previous_effect = active_effect;

// If we're hydrating but the custom element is from Svelte, and it already scaffolded,
// then it might run block logic in hydration mode, which we have to prevent.
let was_hydrating = hydrating;
Expand All @@ -222,17 +223,20 @@ export function set_custom_element_data(node, prop, value) {

set_active_reaction(null);
set_active_effect(null);

try {
if (
// `style` should use `set_attribute` rather than the setter
prop !== 'style' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is necessary, was it reall inconsistent before? Also see my question in the related issue

// Don't compute setters for custom elements while they aren't registered yet,
// because during their upgrade/instantiation they might add more setters.
// Instead, fall back to a simple "an object, then set as property" heuristic.
setters_cache.has(node.nodeName) ||
(setters_cache.has(node.nodeName) ||
// customElements may not be available in browser extension contexts
!customElements ||
customElements.get(node.tagName.toLowerCase())
? get_setters(node).includes(prop)
: value && typeof value === 'object'
: value && typeof value === 'object')
) {
// @ts-expect-error
node[prop] = value;
Expand Down Expand Up @@ -375,8 +379,9 @@ export function set_attributes(
// @ts-ignore
element[`__${event_name}`] = undefined;
}
} else if (key === 'style' && value != null) {
element.style.cssText = value + '';
} else if (key === 'style') {
// avoid using the setter
set_attribute(element, key, value);
} else if (key === 'autofocus') {
autofocus(/** @type {HTMLElement} */ (element), Boolean(value));
} else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) {
Expand Down Expand Up @@ -425,10 +430,6 @@ export function set_attributes(
set_attribute(element, name, value);
}
}
if (key === 'style' && '__styles' in element) {
// reset styles to force style: directive to update
element.__styles = {};
}
}

if (is_hydrating_custom_element) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

const style_1 = 'invalid-key:0; margin:4px;;color: green ;color:blue ';
const style_2 = ' other-key : 0 ; padding:2px; COLOR:green; color: blue';

// https://github.com/sveltejs/svelte/issues/15309
export default test({
props: {
style: style_1
},

html: `
<div style="${style_1}"></div>
<div style="${style_1}"></div>

<custom-element style="${style_1}"></custom-element>
<custom-element style="${style_1}"></custom-element>
`,

async test({ assert, target, component }) {
component.style = style_2;
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<div style="${style_2}"></div>
<div style="${style_2}"></div>

<custom-element style="${style_2}"></custom-element>
<custom-element style="${style_2}"></custom-element>
`
);

component.style = '';
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<div style=""></div>
<div style=""></div>

<custom-element style=""></custom-element>
<custom-element style=""></custom-element>
`
);

component.style = null;
flushSync();

assert.htmlEqual(
target.innerHTML,
`
<div></div>
<div></div>

<custom-element></custom-element>
<custom-element></custom-element>
`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
let { style } = $props();
</script>

<div {style}></div>
<div {...{style}}></div>

<custom-element {style}></custom-element>
<custom-element {...{style}}></custom-element>