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

add attr-foo support, related to #6 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var TypedError = require("error/typed")
var parseTag = require("./parse-tag.js")
var softSetHook = require("./hooks/soft-set-hook.js")
var dataSetHook = require("./hooks/data-set-hook.js")
var attributeHook = require("./hooks/attribute-hook.js")
var evHook = require("./hooks/ev-hook.js")

var UnexpectedVirtualElement = TypedError({
Expand Down Expand Up @@ -78,6 +79,11 @@ function h(tagName, properties, children) {
if (propName.substr(0, 3) === "ev-") {
props[propName] = evHook(value)
}

// add attr-foo support
if (propName.substr(0, 5) === "attr-") {
props[propName.substr(5)] = attributeHook(value)
}
}

if (children !== undefined && children !== null) {
Expand Down
24 changes: 24 additions & 0 deletions test/h.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,27 @@ test("h with two ids", function (assert) {

assert.end()
})

test("h with attr-", function (assert) {
var node = h("div", { "attr-foo": "bar" })

assert.equal(node.properties["attr-foo"], "bar")
assert.ok(node.properties["foo"])

var hook = node.properties["foo"]

var elem = {
setAttributeNS: function (ns, name, value) {
assert.equal(ns, null)
assert.equal(name, "foo")
assert.equal(value, "bar")

this.attributes = {}
this.attributes[name] = value
}
}
hook.hook(elem, "foo")
assert.equal(elem.attributes.foo, "bar")

assert.end()
})