-
Notifications
You must be signed in to change notification settings - Fork 15
/
rjs-dom.2.0.js
233 lines (198 loc) · 6.49 KB
/
rjs-dom.2.0.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Raine's Javascript Extensions for the DOM
*/
/** Creates an element from the given HTML s-expression.
* @param sexp[0] Tag name
* @param sexp[1] Attributes
* @param sexp[2] Children
*/
create = function(sexp) {
if(sexp === null) {
return null;
}
// validate arguments
if(sexp.length === 0) {
throw new Error("create -> Not enough arguments. You must at least specify a tag name.");
}
if(typeof sexp[0] !== "string") {
throw new Error("create -> Invalid tag name: {0} ({1})".format(sexp[0], typeof sexp[0]));
}
if(typeof sexp[1] !== "object" && sexp[1] !== undefined) {
console.error("create -> Invalid attributes");
console.error(sexp[1]);
throw new Error("create -> Invalid attributes: " + sexp[1]);
}
var tagNameString = sexp[0];
var attrs = sexp[1];
var children = sexp[2];
// create the element and parse its attributes and children
var element = tagNameString == "fragment" ?
document.createDocumentFragment() : parseTagName(tagNameString);
if(attrs) {
parseAttributes(element, attrs);
}
if(children) {
parseChildren(element);
}
return element;
/******************************************
* Parsing Functions
******************************************/
/** Parses the tagName for CSS selector syntax and returns a newly created element. */
function parseTagName(tagNameString) {
var tagName = tagNameString.split(/[#.]/)[0] || "div";
var selectors = tagNameString.substring(tagNameString.indexOf(/[#.]/)).match(/[#.][^#.]+/g) || [];
var el = document.createElement(tagName);
each(selectors, function(selector) {
if(selector[0] === "#") {
el.setAttribute("id", selector.substring(1));
}
else if(selector[0] === ".") {
addClass(el, selector.substring(1));
}
});
return el;
}
/** Parses the attributes and adds them to the element. */
function parseAttributes(element, attrs) {
if(!element || element.nodeType != 1) {
return;
}
var attrDisplayMap = {
className : "class",
htmlFor : "for"
};
for(attr in attrs) {
if(isValue(attrs[attr])) {
if(attr.toLowerCase() === "events") {
parseEvents(element, attrs);
}
else if(attr === "className" || attr === "class") {
addClass(element, attrs[attr]);
}
// allow style attribute value to be passed in as a dictionary
else if(attr === "style" && typeof(attrs[attr]) === "object") {
element.setAttribute("style", joinObj(attrs[attr], "; ", ":") + ";");
}
else {
element.setAttribute(attrDisplayMap[attr] || attr, attrs[attr]);
}
}
}
// default attributes (syntactic sugar)
if(element.tagName.toLowerCase() === "a" && !("href" in attrs) && !("name" in attrs)) {
element.setAttribute("href", "javascript:void(0);");
}
if(element.tagName.toLowerCase() === "img" && !("alt" in attrs)) {
element.setAttribute("alt", "");
}
}
/** Adds the given className to the elements class attribute. */
function addClass(element, className) {
element.setAttribute("class", element.hasAttribute("class") ? element.getAttribute("class") + " " + className : className);
};
/** Parses the element's children. */
function parseChildren(element) {
if(typeof children === "string" || typeof children === "number") {
element.appendChild(document.createTextNode(children));
}
else if(children instanceof Array) {
each(children, function(child) {
if(isValue(child)) {
element.appendChild(
// handles children as DomNodes, strings & numbers, and s-expression (recursive)
isDomNode(child) ? child :
typeof child === "string" || typeof child === "number" ? document.createTextNode(child) :
child instanceof Array ? create(child) : // RECURSION
(function() { throw new Error("Invalid child: " + child); })()
);
}
});
}
else {
throw new Error("Unrecognized child type");
}
}
/** Parses inline event attachment. */
function parseEvents(element, attrs) {
var events = [].concat(attrs[attr]);
each(events, function(eventObject) {
var eventArgs = {};
// if a function is given directly for a link, assume it is a click event
if(typeof eventObject === "function") {
if(element.tagName.toLowerCase() === "a") {
eventArgs.type = "click";
eventArgs.handler = eventObject;
}
else {
throw new Error("There is no default event handler type for {0} elements".format(element.tagName));
}
}
else {
eventArgs = eventObject;
if(tagName.toLowerCase() === "a") { // use 'click' as the default event for links
eventArgs.type = "click";
}
}
$(element).bind(eventArgs.type, eventArgs.handler);
});
}
/******************************************
* Helper Functions
******************************************/
/** Returns true if the object is not equal to null or undefined. */
function isValue(obj) {
return obj !== null && obj !== undefined;
}
/** Returns true if the given object seems to be a DomNode. */
function isDomNode(node) {
return typeof node.nodeType == "number";
}
};
/** Invokes a function on the given node and each of its child nodes (recursive). */
var traverse = function(node, f) {
if(node.nodeType === 1) {
f(node);
each(node.childNodes, function(child) {
traverse(child, f); // RECURSION
});
}
};
/** Removes all child nodes from the given element. */
removeChildNodes = function(element) {
while(element.hasChildNodes()) {
element.removeChild(element.lastChild);
}
};
/** Converts angled brackets to their html encoded value. */
encodeBrackets = function(str) {
return str
.replace(/</,"<")
.replace(/>/,">");
};
/** Creates an element from the given s-expression just like create but returns HTML text instead of a DOM element.
Note: Doesn't handle elements that cannot be added to a div.
*/
createHtml = function(sexp) {
return create(["div", {}, [sexp]]).innerHTML;
};
/** Array overrides. Pretty invasive but if it doesn't create problems makes an easy way to modify markup objects after creation. */
Array.prototype.addAttributes = function(obj) {
this[1] = merge(this[1], obj);
return this;
}
Array.prototype.addClass = function(className) {
var attrs = this[1];
attrs["class"] = "class" in attrs ? " " + className : className;
return this;
}
Array.prototype.setContent = function(content) {
this[2] = content;
return this;
}
/** Make query string parameters easily available. */
queryParams = unescape(location.search).substring(1).split("&").toDict(function(queryKeyValue) { return queryKeyValue.split("="); });
autozindexNumber = 10;
autozindex = function() {
return ++autozindexNumber;
}