-
Notifications
You must be signed in to change notification settings - Fork 49
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
Feature/add custom element built in support #137
Changes from all commits
350ff77
13d2694
18421f5
9cb47b5
9431f91
57ca34f
ff2e98a
f616617
ce5b1e0
e76dc7b
13fe106
6f7ceb0
80fde2a
25f89c1
edaac74
312b4f5
e0bb3c1
0f9bd60
3e4366b
586f59a
9817b36
cdc4895
0572d53
8d77ec0
d4757f8
597ad14
78728e6
0bc5662
183c90c
da5385e
69dbdad
5a6478b
8186c6d
bc07060
f0ca21a
e6b7ee4
2f70a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,24 @@ function nanoHtmlCreateElement (tag, props, children) { | |
delete props.namespace | ||
} | ||
|
||
// If we are extending a builtin element | ||
var isCustomElement = false | ||
if (props.is) { | ||
isCustomElement = props.is | ||
delete props.is | ||
} | ||
|
||
// Create the element | ||
if (ns) { | ||
el = document.createElementNS(ns, tag) | ||
if (isCustomElement) { | ||
el = document.createElementNS(ns, tag, { is: isCustomElement }) | ||
} else { | ||
el = document.createElementNS(ns, tag) | ||
} | ||
} else if (tag === COMMENT_TAG) { | ||
return document.createComment(props.comment) | ||
} else if (isCustomElement) { | ||
el = document.createElement(tag, { is: isCustomElement }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please see the second optional |
||
} else { | ||
el = document.createElement(tag) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var _div, | ||
_appendChild = require('nanohtml/lib/append-child'); | ||
|
||
_div = document.createElement('div', { | ||
is: 'my-div' | ||
}), _appendChild(_div, ['\n Hello world\n ']), _div; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import html from 'nanohtml' | ||
|
||
html` | ||
<div is="my-div"> | ||
Hello world | ||
</div> | ||
` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,3 +180,27 @@ test('allow objects to be passed', function (t) { | |
t.ok(result.outerHTML.indexOf('<div foo="bar">hey</div>') !== -1, 'contains foo="bar"') | ||
t.end() | ||
}) | ||
|
||
test('supports extended build-in elements', function (t) { | ||
t.plan(1) | ||
|
||
var originalCreateElement = document.createElement | ||
var optionsArg | ||
|
||
// this iife is a must to avoid illegal invocation type errors, caused by transformed nanohtml tests | ||
(function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If tape would have |
||
document.createElement = function () { | ||
optionsArg = arguments[1] | ||
return originalCreateElement.apply(this, arguments) | ||
} | ||
})() | ||
|
||
;html`<div is="my-div"></div>` | ||
|
||
t.ok(typeof optionsArg === 'object' && optionsArg.is === 'my-div', 'properly passes optional extends object') | ||
|
||
// revert to original prototype method | ||
delete document.createElement | ||
goto-bus-stop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
t.end() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please see third optional
options
parameterhttps://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS#Syntax