Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,7 @@ export function RegularElement(node, context) {

// class/style directives must be applied last since they could override class/style attributes
build_class_directives(class_directives, node_id, context, is_attributes_reactive);
build_style_directives(
style_directives,
node_id,
context,
is_attributes_reactive,
lookup.has('style') || has_spread
);
build_style_directives(style_directives, node_id, context, is_attributes_reactive);

// Apply the src and loading attributes for <img> elements after the element is appended to the document
if (node.name === 'img' && (has_spread || lookup.has('loading'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function SvelteElement(node, context) {

// class/style directives must be applied last since they could override class/style attributes
build_class_directives(class_directives, element_id, inner_context, is_attributes_reactive);
build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive, true);
build_style_directives(style_directives, element_id, inner_context, is_attributes_reactive);

const get_tag = b.thunk(/** @type {Expression} */ (context.visit(node.tag)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ export function build_set_attributes(
* @param {Identifier} element_id
* @param {ComponentContext} context
* @param {boolean} is_attributes_reactive
* @param {boolean} force_check Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute
*/
export function build_style_directives(
style_directives,
element_id,
context,
is_attributes_reactive,
force_check
is_attributes_reactive
) {
const state = context.state;

Expand All @@ -126,8 +124,7 @@ export function build_style_directives(
element_id,
b.literal(directive.name),
value,
/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined),
force_check ? b.true : undefined
/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined)
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export function set_attribute(element, attribute, value, skip_warning) {

if (attributes[attribute] === (attributes[attribute] = value)) return;

if (attribute === 'style' && '__styles' in element) {
// reset styles to force style: directive to update
element.__styles = {};
}

if (attribute === 'loading') {
// @ts-expect-error
element[LOADING_ATTR_SYMBOL] = value;
Expand Down Expand Up @@ -289,6 +294,10 @@ export function set_attributes(
}
}
}
if (key === 'style' && '__styles' in element) {
// reset styles to force style: directive to update
element.__styles = {};
}
}

// On the first run, ensure that events are added after bindings so
Expand Down
15 changes: 6 additions & 9 deletions packages/svelte/src/internal/client/dom/elements/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
* @param {string} key
* @param {string} value
* @param {boolean} [important]
* @param {boolean} [force_check] Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute
*/
export function set_style(dom, key, value, important, force_check) {
export function set_style(dom, key, value, important) {
// @ts-expect-error
var attributes = (dom.__attributes ??= {});
var style = dom.style;
var style_key = 'style-' + key;
var styles = (dom.__styles ??= {});

if (attributes[style_key] === value && (!force_check || style.getPropertyValue(key) === value)) {
if (styles[key] === value) {
return;
}

attributes[style_key] = value;
styles[key] = value;

if (value == null) {
style.removeProperty(key);
dom.style.removeProperty(key);
} else {
style.setProperty(key, value, important ? 'important' : '');
dom.style.setProperty(key, value, important ? 'important' : '');
}
}
2 changes: 2 additions & 0 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export function init_operations() {
// @ts-expect-error
element_prototype.__attributes = null;
// @ts-expect-error
element_prototype.__styles = null;
// @ts-expect-error
element_prototype.__e = undefined;

// @ts-expect-error
Expand Down