|
1 | | -'use strict' |
2 | | - |
3 | | -var hyperx = require('hyperx') |
4 | | -var appendChild = require('./append-child') |
5 | | -var SVG_TAGS = require('./svg-tags') |
6 | | -var BOOL_PROPS = require('./bool-props') |
7 | | -// Props that need to be set directly rather than with el.setAttribute() |
8 | | -var DIRECT_PROPS = require('./direct-props') |
9 | | - |
10 | | -var SVGNS = 'http://www.w3.org/2000/svg' |
11 | | -var XLINKNS = 'http://www.w3.org/1999/xlink' |
12 | | - |
13 | | -var COMMENT_TAG = '!--' |
14 | | - |
15 | | -function nanoHtmlCreateElement (tag, props, children) { |
16 | | - var el |
17 | | - |
18 | | - // If an svg tag, it needs a namespace |
19 | | - if (SVG_TAGS.indexOf(tag) !== -1) { |
20 | | - props.namespace = SVGNS |
21 | | - } |
22 | | - |
23 | | - // If we are using a namespace |
24 | | - var ns = false |
25 | | - if (props.namespace) { |
26 | | - ns = props.namespace |
27 | | - delete props.namespace |
28 | | - } |
29 | | - |
30 | | - // If we are extending a builtin element |
31 | | - var isCustomElement = false |
32 | | - if (props.is) { |
33 | | - isCustomElement = props.is |
34 | | - delete props.is |
35 | | - } |
36 | | - |
37 | | - // Create the element |
38 | | - if (ns) { |
39 | | - if (isCustomElement) { |
40 | | - el = document.createElementNS(ns, tag, { is: isCustomElement }) |
41 | | - } else { |
42 | | - el = document.createElementNS(ns, tag) |
43 | | - } |
44 | | - } else if (tag === COMMENT_TAG) { |
45 | | - return document.createComment(props.comment) |
46 | | - } else if (isCustomElement) { |
47 | | - el = document.createElement(tag, { is: isCustomElement }) |
48 | | - } else { |
49 | | - el = document.createElement(tag) |
50 | | - } |
51 | | - |
52 | | - // Create the properties |
53 | | - for (var p in props) { |
54 | | - if (props.hasOwnProperty(p)) { |
55 | | - var key = p.toLowerCase() |
56 | | - var val = props[p] |
57 | | - // Normalize className |
58 | | - if (key === 'classname') { |
59 | | - key = 'class' |
60 | | - p = 'class' |
61 | | - } |
62 | | - // The for attribute gets transformed to htmlFor, but we just set as for |
63 | | - if (p === 'htmlFor') { |
64 | | - p = 'for' |
65 | | - } |
66 | | - // If a property is boolean, set itself to the key |
67 | | - if (BOOL_PROPS.indexOf(key) !== -1) { |
68 | | - if (String(val) === 'true') val = key |
69 | | - else if (String(val) === 'false') continue |
70 | | - } |
71 | | - // If a property prefers being set directly vs setAttribute |
72 | | - if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) { |
73 | | - el[p] = val |
74 | | - } else { |
75 | | - if (ns) { |
76 | | - if (p === 'xlink:href') { |
77 | | - el.setAttributeNS(XLINKNS, p, val) |
78 | | - } else if (/^xmlns($|:)/i.test(p)) { |
79 | | - // skip xmlns definitions |
80 | | - } else { |
81 | | - el.setAttributeNS(null, p, val) |
82 | | - } |
83 | | - } else { |
84 | | - el.setAttribute(p, val) |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - appendChild(el, children) |
91 | | - return el |
92 | | -} |
93 | | - |
94 | | -function createFragment (nodes) { |
95 | | - var fragment = document.createDocumentFragment() |
96 | | - for (var i = 0; i < nodes.length; i++) { |
97 | | - if (nodes[i] == null) continue |
98 | | - if (Array.isArray(nodes[i])) { |
99 | | - fragment.appendChild(createFragment(nodes[i])) |
100 | | - } else { |
101 | | - if (typeof nodes[i] === 'string') nodes[i] = document.createTextNode(nodes[i]) |
102 | | - fragment.appendChild(nodes[i]) |
103 | | - } |
104 | | - } |
105 | | - return fragment |
106 | | -} |
107 | | - |
108 | | -module.exports = hyperx(nanoHtmlCreateElement, { |
109 | | - comments: true, |
110 | | - createFragment: createFragment |
111 | | -}) |
112 | | -module.exports.default = module.exports |
113 | | -module.exports.createElement = nanoHtmlCreateElement |
| 1 | +module.exports = require('./dom')(document) |
0 commit comments