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 <rich.harris@vercel.com>
pull/11979/head
Azat S 1 year ago committed by GitHub
parent c9202a8896
commit 088632b06d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: apply style directives to element with empty style attribute

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

@ -0,0 +1,7 @@
import { ok, test } from '../../test';
export default test({
html: `
<p style="color: red;">red</p>
`
});
Loading…
Cancel
Save