Skip to content
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

Handle shared attributes object in hyperscript #1942

Merged
merged 2 commits into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#### Bug fixes:

- core: don't call `onremove` on the children of components that return null from the view [#1921](https://github.com/MithrilJS/mithril.js/issues/1921) [octavore](https://github.com/octavore) ([#1922](https://github.com/MithrilJS/mithril.js/pull/1922))
- hypertext: correct handling of shared attributes object passed to `m()`. Will copy attributes when it's necessary [#1941](https://github.com/MithrilJS/mithril.js/issues/1941) [s-ilya](https://github.com/s-ilya) ([#1942](https://github.com/MithrilJS/mithril.js/pull/1942))

---

Expand Down
12 changes: 12 additions & 0 deletions render/hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ function execSelector(state, attrs, children) {
var hasAttrs = false, childList, text
var className = attrs.className || attrs.class

if (Object.keys(state.attrs).length && Object.keys(attrs).length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self for later (if nobody beats me to it): introduce a more optimized helper in this file to check if an object has any properties.

function isEmpty(object) {
    for (var key of object) if (hasOwn.call(object, key)) return true
    return false
}

(Nothing mandatory on your part @s-ilya)

var newAttrs = {}

for(var key in attrs) {
if (hasOwn.call(attrs, key)) {
newAttrs[key] = attrs[key]
}
}

attrs = newAttrs
}

for (var key in state.attrs) {
if (hasOwn.call(state.attrs, key)) {
attrs[key] = state.attrs[key]
Expand Down
17 changes: 17 additions & 0 deletions render/tests/test-hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,23 @@ o.spec("hyperscript", function() {
o(vnode.children[0].tag).equals("i")
o(vnode.children[1].tag).equals("s")
})
o("handles shared attrs", function() {
var attrs = {a: "b"}

var nodeA = m(".a", attrs)
var nodeB = m(".b", attrs)

o(nodeA.attrs.className).equals("a")
o(nodeA.attrs.a).equals("b")

o(nodeB.attrs.className).equals("b")
o(nodeB.attrs.a).equals("b")
})
o("doesnt modify passed attributes object", function() {
var attrs = {a: "b"}
m(".a", attrs)
o(attrs).deepEquals({a: "b"})
})
o("handles fragment children without attr unwrapped", function() {
var vnode = m("div", [m("i")], [m("s")])

Expand Down