Skip to content

Commit 18bfb9b

Browse files
committed
feature: support string value in style attribute; close #756
1 parent 2df3d0c commit 18bfb9b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/index.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,17 @@ export function app(state, actions, view, container) {
159159
function updateAttribute(element, name, value, oldValue, isSvg) {
160160
if (name === "key") {
161161
} else if (name === "style") {
162-
for (var i in clone(oldValue, value)) {
163-
var style = value == null || value[i] == null ? "" : value[i]
164-
if (i[0] === "-") {
165-
element[name].setProperty(i, style)
166-
} else {
167-
element[name][i] = style
162+
if (typeof value === "string") {
163+
element.style.cssText = value
164+
} else {
165+
if (typeof oldValue === "string") oldValue = element.style.cssText = ""
166+
for (var i in clone(oldValue, value)) {
167+
var style = value == null || value[i] == null ? "" : value[i]
168+
if (i[0] === "-") {
169+
element.style.setProperty(i, style)
170+
} else {
171+
element.style[i] = style
172+
}
168173
}
169174
}
170175
} else {

test/dom.test.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,25 @@ testVdomToHtml("mixed keyed/non-keyed", [
477477

478478
testVdomToHtml("styles", [
479479
{
480-
vdom: <div />,
481-
html: `<div></div>`
480+
vdom: <div style="color: red;" />,
481+
html: `<div style="color: red;"></div>`
482482
},
483483
{
484484
vdom: <div style={{ color: "red", fontSize: "1em", "--foo": "red" }} />,
485485
html: `<div style="color: red; font-size: 1em;"></div>`
486486
},
487+
488+
{
489+
vdom: <div style={{ color: "blue", display: "flex", "--foo": "blue" }} />,
490+
html: `<div style="color: blue; display: flex;"></div>`
491+
},
492+
{
493+
vdom: <div style="background-color: blue;" />,
494+
html: `<div style="background-color: blue;"></div>`
495+
},
487496
{
488-
vdom: <div style={{ color: "blue", float: "left", "--foo": "blue" }} />,
489-
html: `<div style="color: blue; float: left;"></div>`
497+
vdom: <div style={null} />,
498+
html: `<div style=""></div>`
490499
},
491500
{
492501
vdom: <div style="" />,

0 commit comments

Comments
 (0)