-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dom.js to allow custom document object (#165)
* Add dom.js to allow custom document object * fix test by keeping require() * node version check * fix standardjs errors * unconditional html.js * Also detect ./html in the babel plugin
- Loading branch information
1 parent
a79e024
commit bfb3aef
Showing
14 changed files
with
180 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/dom') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1 @@ | ||
'use strict' | ||
|
||
var hyperx = require('hyperx') | ||
var appendChild = require('./append-child') | ||
var SVG_TAGS = require('./svg-tags') | ||
var BOOL_PROPS = require('./bool-props') | ||
// Props that need to be set directly rather than with el.setAttribute() | ||
var DIRECT_PROPS = require('./direct-props') | ||
|
||
var SVGNS = 'http://www.w3.org/2000/svg' | ||
var XLINKNS = 'http://www.w3.org/1999/xlink' | ||
|
||
var COMMENT_TAG = '!--' | ||
|
||
function nanoHtmlCreateElement (tag, props, children) { | ||
var el | ||
|
||
// If an svg tag, it needs a namespace | ||
if (SVG_TAGS.indexOf(tag) !== -1) { | ||
props.namespace = SVGNS | ||
} | ||
|
||
// If we are using a namespace | ||
var ns = false | ||
if (props.namespace) { | ||
ns = props.namespace | ||
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) { | ||
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 }) | ||
} else { | ||
el = document.createElement(tag) | ||
} | ||
|
||
// Create the properties | ||
for (var p in props) { | ||
if (props.hasOwnProperty(p)) { | ||
var key = p.toLowerCase() | ||
var val = props[p] | ||
// Normalize className | ||
if (key === 'classname') { | ||
key = 'class' | ||
p = 'class' | ||
} | ||
// The for attribute gets transformed to htmlFor, but we just set as for | ||
if (p === 'htmlFor') { | ||
p = 'for' | ||
} | ||
// If a property is boolean, set itself to the key | ||
if (BOOL_PROPS.indexOf(key) !== -1) { | ||
if (String(val) === 'true') val = key | ||
else if (String(val) === 'false') continue | ||
} | ||
// If a property prefers being set directly vs setAttribute | ||
if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) { | ||
el[p] = val | ||
} else { | ||
if (ns) { | ||
if (p === 'xlink:href') { | ||
el.setAttributeNS(XLINKNS, p, val) | ||
} else if (/^xmlns($|:)/i.test(p)) { | ||
// skip xmlns definitions | ||
} else { | ||
el.setAttributeNS(null, p, val) | ||
} | ||
} else { | ||
el.setAttribute(p, val) | ||
} | ||
} | ||
} | ||
} | ||
|
||
appendChild(el, children) | ||
return el | ||
} | ||
|
||
function createFragment (nodes) { | ||
var fragment = document.createDocumentFragment() | ||
for (var i = 0; i < nodes.length; i++) { | ||
if (nodes[i] == null) continue | ||
if (Array.isArray(nodes[i])) { | ||
fragment.appendChild(createFragment(nodes[i])) | ||
} else { | ||
if (typeof nodes[i] === 'string') nodes[i] = document.createTextNode(nodes[i]) | ||
fragment.appendChild(nodes[i]) | ||
} | ||
} | ||
return fragment | ||
} | ||
|
||
module.exports = hyperx(nanoHtmlCreateElement, { | ||
comments: true, | ||
createFragment: createFragment | ||
}) | ||
module.exports.default = module.exports | ||
module.exports.createElement = nanoHtmlCreateElement | ||
module.exports = require('./dom')(document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
'use strict' | ||
|
||
var hyperx = require('hyperx') | ||
var appendChild = require('./append-child') | ||
var SVG_TAGS = require('./svg-tags') | ||
var BOOL_PROPS = require('./bool-props') | ||
// Props that need to be set directly rather than with el.setAttribute() | ||
var DIRECT_PROPS = require('./direct-props') | ||
|
||
var SVGNS = 'http://www.w3.org/2000/svg' | ||
var XLINKNS = 'http://www.w3.org/1999/xlink' | ||
|
||
var COMMENT_TAG = '!--' | ||
|
||
module.exports = function (document) { | ||
function nanoHtmlCreateElement (tag, props, children) { | ||
var el | ||
|
||
// If an svg tag, it needs a namespace | ||
if (SVG_TAGS.indexOf(tag) !== -1) { | ||
props.namespace = SVGNS | ||
} | ||
|
||
// If we are using a namespace | ||
var ns = false | ||
if (props.namespace) { | ||
ns = props.namespace | ||
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) { | ||
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 }) | ||
} else { | ||
el = document.createElement(tag) | ||
} | ||
|
||
// Create the properties | ||
for (var p in props) { | ||
if (props.hasOwnProperty(p)) { | ||
var key = p.toLowerCase() | ||
var val = props[p] | ||
// Normalize className | ||
if (key === 'classname') { | ||
key = 'class' | ||
p = 'class' | ||
} | ||
// The for attribute gets transformed to htmlFor, but we just set as for | ||
if (p === 'htmlFor') { | ||
p = 'for' | ||
} | ||
// If a property is boolean, set itself to the key | ||
if (BOOL_PROPS.indexOf(key) !== -1) { | ||
if (String(val) === 'true') val = key | ||
else if (String(val) === 'false') continue | ||
} | ||
// If a property prefers being set directly vs setAttribute | ||
if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) { | ||
el[p] = val | ||
} else { | ||
if (ns) { | ||
if (p === 'xlink:href') { | ||
el.setAttributeNS(XLINKNS, p, val) | ||
} else if (/^xmlns($|:)/i.test(p)) { | ||
// skip xmlns definitions | ||
} else { | ||
el.setAttributeNS(null, p, val) | ||
} | ||
} else { | ||
el.setAttribute(p, val) | ||
} | ||
} | ||
} | ||
} | ||
|
||
appendChild(el, children) | ||
return el | ||
} | ||
|
||
function createFragment (nodes) { | ||
var fragment = document.createDocumentFragment() | ||
for (var i = 0; i < nodes.length; i++) { | ||
if (nodes[i] == null) continue | ||
if (Array.isArray(nodes[i])) { | ||
fragment.appendChild(createFragment(nodes[i])) | ||
} else { | ||
if (typeof nodes[i] === 'string') nodes[i] = document.createTextNode(nodes[i]) | ||
fragment.appendChild(nodes[i]) | ||
} | ||
} | ||
return fragment | ||
} | ||
|
||
var exports = hyperx(nanoHtmlCreateElement, { | ||
comments: true, | ||
createFragment: createFragment | ||
}) | ||
exports.default = exports | ||
exports.createComment = nanoHtmlCreateElement | ||
return exports | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var doc = new (require('js' + 'dom').JSDOM)().window.document | ||
|
||
module.exports.document = doc | ||
module.exports.html = require('../../dom')(doc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters