Skip to content

Commit 088632b

Browse files
azat-ioRich-Harris
andauthored
fix: fix merge style func (#11971)
* fix: fix merge style func * add test, do some drive-by tidying * changeset * lint * lint, simplify * oops, order matters for tests --------- Co-authored-by: Rich Harris <[email protected]>
1 parent c9202a8 commit 088632b

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

.changeset/yellow-rockets-sit.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: apply style directives to element with empty style attribute

packages/svelte/src/internal/server/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,28 @@ export function add_styles(style_object) {
264264
}
265265

266266
/**
267-
* @param {string} style_attribute
268-
* @param {Record<string, string>} style_directive
267+
* @param {string} attribute
268+
* @param {Record<string, string>} styles
269269
*/
270-
export function merge_styles(style_attribute, style_directive) {
270+
export function merge_styles(attribute, styles) {
271271
/** @type {Record<string, string>} */
272-
const style_object = {};
273-
for (const individual_style of style_attribute.split(';')) {
274-
const colon_index = individual_style.indexOf(':');
275-
const name = individual_style.slice(0, colon_index).trim();
276-
const value = individual_style.slice(colon_index + 1).trim();
277-
if (!name) continue;
278-
style_object[name] = value;
279-
}
280-
for (const name in style_directive) {
281-
const value = style_directive[name];
282-
if (value) {
283-
style_object[name] = value;
284-
} else {
285-
delete style_object[name];
272+
var merged = {};
273+
274+
if (attribute) {
275+
for (var declaration of attribute.split(';')) {
276+
var i = declaration.indexOf(':');
277+
var name = declaration.slice(0, i).trim();
278+
var value = declaration.slice(i + 1).trim();
279+
280+
if (name !== '') merged[name] = value;
286281
}
287282
}
288-
return style_object;
283+
284+
for (name in styles) {
285+
merged[name] = styles[name];
286+
}
287+
288+
return merged;
289289
}
290290

291291
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ok, test } from '../../test';
2+
3+
export default test({
4+
html: `
5+
<p style="color: red;">red</p>
6+
`
7+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p style:color="red" style={null}>red</p>

0 commit comments

Comments
 (0)