-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomo.js
212 lines (165 loc) · 5.69 KB
/
domo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// domo.js 0.5.7
// (c) 2012 Jed Schmidt
// domo.js is distributed under the MIT license.
// For more details, see http://domo-js.com
!function() {
// Determine the global object.
var global = Function("return this")()
// Valid HTML5 tag names used to generate DOM functions.
var tags = [
"A", "ABBR", "ACRONYM", "ADDRESS", "AREA", "ARTICLE", "ASIDE", "AUDIO",
"B", "BDI", "BDO", "BIG", "BLOCKQUOTE", "BODY", "BR", "BUTTON",
"CANVAS", "CAPTION", "CITE", "CODE", "COL", "COLGROUP", "COMMAND",
"DATALIST", "DD", "DEL", "DETAILS", "DFN", "DIV", "DL", "DT", "EM",
"EMBED", "FIELDSET", "FIGCAPTION", "FIGURE", "FOOTER", "FORM", "FRAME",
"FRAMESET", "H1", "H2", "H3", "H4", "H5", "H6", "HEAD", "HEADER",
"HGROUP", "HR", "HTML", "I", "IFRAME", "IMG", "INPUT", "INS", "KBD",
"KEYGEN", "LABEL", "LEGEND", "LI", "LINK", "MAP", "MARK", "META",
"METER", "NAV", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP", "OPTION",
"OUTPUT", "P", "PARAM", "PRE", "PROGRESS", "Q", "RP", "RT", "RUBY",
"SAMP", "SCRIPT", "SECTION", "SELECT", "SMALL", "SOURCE", "SPAN",
"SPLIT", "STRONG", "STYLE", "SUB", "SUMMARY", "SUP", "TABLE", "TBODY",
"TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TIME", "TITLE", "TR",
"TRACK", "TT", "UL", "VAR", "VIDEO", "WBR"
]
// Turn a camelCase string into a hyphenated one.
// Used for CSS property names and DOM element attributes.
function hyphenify(text) {
return text.replace(/[A-Z]/g, "-$&").toLowerCase()
}
// Cache select Array/Object methods
var shift = Array.prototype.shift
var unshift = Array.prototype.unshift
var concat = Array.prototype.concat
var has = Object.prototype.hasOwnProperty
// Export the Domo constructor for a CommonJS environment,
// or create a new Domo namespace otherwise.
typeof module == "object"
? module.exports = Domo
: new Domo(global.document).global(true)
// Create a new domo namespace, scoped to the given document.
function Domo(document) {
if (!document) throw new Error("No document provided.")
this.domo = this
// Create a DOM comment
this.COMMENT = function(nodeValue) {
return document.createComment(nodeValue)
}
// Create a DOM text node
this.TEXT = function(nodeValue) {
return document.createTextNode(nodeValue)
}
// Create a DOM fragment
this.FRAGMENT = function() {
var fragment = document.createDocumentFragment()
var childNodes = concat.apply([], arguments)
var length = childNodes.length
var i = 0
var child
while (i < length) {
child = childNodes[i++]
while (typeof child == "function") child = child()
if (child == null) child = this.COMMENT(child)
else if (!child.nodeType) child = this.TEXT(child)
fragment.appendChild(child)
}
return fragment
}
// Create a DOM element
this.ELEMENT = function() {
var childNodes = concat.apply([], arguments)
var nodeName = childNodes.shift()
var element = document.createElement(nodeName)
var attributes = childNodes[0]
if (attributes) {
if (typeof attributes == "object" && !attributes.nodeType) {
for (var name in attributes) if (has.call(attributes, name)) {
element.setAttribute(hyphenify(name), attributes[name])
}
childNodes.shift()
}
}
if ( typeof element.appendChild !== 'function' && element.styleSheet ) {
element.styleSheet.cssText = childNodes.join('')
}
else {
element.appendChild(
this.FRAGMENT.apply(this, childNodes)
)
}
switch (nodeName) {
case "HTML":
case "HEAD":
case "BODY":
var replaced = document.getElementsByTagName(nodeName)[0]
if (replaced) replaced.parentNode.replaceChild(element, replaced)
}
return element
}
// Convenience functions to create each HTML5 element
var i = tags.length
while (i--) !function(domo, nodeName) {
domo[nodeName] =
domo[nodeName.toLowerCase()] =
function() {
unshift.call(arguments, nodeName)
return domo.ELEMENT.apply(domo, arguments)
}
}(this, tags[i])
// Create a CSS style rule
this.STYLE.on = function() {
var selector = String(shift.call(arguments))
var rules = concat.apply([], arguments)
var css = selector + "{"
var i = 0
var l = rules.length
var key
var block
while (i < l) {
block = rules[i++]
switch (typeof block) {
case "object":
for (key in block) {
css += hyphenify(key) + ":" + block[key]
if (typeof block[key] == "number") css += "px"
css += ";"
}
break
case "string":
css = selector + " " + block + css
break
}
}
css += "}\n"
return css
}
// Pollute the global scope for convenience.
this.global = function(on) {
var values = this.global.values
var key
var code
if (on !== false) {
global.domo = this
for (key in this) {
code = key.charCodeAt(0)
if (code < 65 || code > 90) continue
if (this[key] == global[key]) continue
if (key in global) values[key] = global[key]
global[key] = this[key]
}
}
else {
delete global.domo
for (key in this) {
if (key in values) {
if (global[key] == this[key]) global[key] = values[key]
}
else delete global[key]
}
}
return this
}
// A place to store previous global properties
this.global.values = {}
}
}()