Skip to content

updateStyle(): Make it possible to update element styles for the same style object #3023

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

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 4 additions & 4 deletions render/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,16 +749,16 @@ module.exports = function() {

//style
function updateStyle(element, old, style) {
if (old === style) {
// Styles are equivalent, do nothing.
} else if (style == null) {
// The case `old` and `style` are the same string has already skipped in setAttr().
if (style == null) {
// New style is missing, just clear it.
element.style = ""
} else if (typeof style !== "object") {
// New style is a string, let engine deal with patching.
element.style = style
} else if (old == null || typeof old !== "object") {
} else if (old == null || typeof old !== "object" || old === style) {
// `old` is missing or a string, `style` is an object.
// `old` and `style` are the same object (in this case, conciliation between objects does not work well.)
element.style = ""
// Add new style properties
for (var key in style) {
Expand Down
13 changes: 13 additions & 0 deletions render/tests/test-updateElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ o.spec("updateElement", function() {

o(updated.dom.style.color).equals("red")
})
o("re-render element styles for the same style object", function() {
var style = {color: "gold"}
var vnode = m("a", {style: style})

render(root, vnode)

// modify the same object
style.color = "red"
var updated = m("a", {style: style})
render(root, updated)

o(updated.dom.style.color).equals("red")
})
o("setting style to `null` removes all styles", function() {
var vnode = m("p", {style: "background-color: red"})
var updated = m("p", {style: null})
Expand Down