Skip to content

fix: fix merge style func #11971

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 6 commits into from
Jun 9, 2024
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/yellow-rockets-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: apply style directives to element with empty style attribute
36 changes: 18 additions & 18 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,28 +264,28 @@ export function add_styles(style_object) {
}

/**
* @param {string} style_attribute
* @param {Record<string, string>} style_directive
* @param {string} attribute
* @param {Record<string, string>} styles
*/
export function merge_styles(style_attribute, style_directive) {
export function merge_styles(attribute, styles) {
/** @type {Record<string, string>} */
const style_object = {};
for (const individual_style of style_attribute.split(';')) {
const colon_index = individual_style.indexOf(':');
const name = individual_style.slice(0, colon_index).trim();
const value = individual_style.slice(colon_index + 1).trim();
if (!name) continue;
style_object[name] = value;
}
for (const name in style_directive) {
const value = style_directive[name];
if (value) {
style_object[name] = value;
} else {
delete style_object[name];
var merged = {};

if (attribute) {
for (var declaration of attribute.split(';')) {
var i = declaration.indexOf(':');
var name = declaration.slice(0, i).trim();
var value = declaration.slice(i + 1).trim();

if (name !== '') merged[name] = value;
}
}
return style_object;

for (name in styles) {
merged[name] = styles[name];
}

return merged;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ok, test } from '../../test';

export default test({
html: `
<p style="color: red;">red</p>
`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p style:color="red" style={null}>red</p>
Loading