Commits

Stephen McKamey committed b6851f7

- re-ordering code blocks

  • Participants
  • Parent commits 69001c2

Comments (0)

Files changed (1)

 		FUN = 1,
 		ARY = 2,
 		OBJ = 3,
-		VAL = 4,
-
-	//attribute name mapping
-	ATTRMAP = {
-		rowspan : "rowSpan",
-		colspan : "colSpan",
-		cellpadding : "cellPadding",
-		cellspacing : "cellSpacing",
-		tabindex : "tabIndex",
-		accesskey : "accessKey",
-		hidefocus : "hideFocus",
-		usemap : "useMap",
-		maxlength : "maxLength",
-		readonly : "readOnly",
-		contenteditable : "contentEditable"
-		// can add more attributes here as needed
-	},
-
-	// attribute duplicates
-	ATTRDUP = {
-		enctype : "encoding",
-		onscroll : "DOMMouseScroll"
-		// can add more attributes here as needed
-	},
-
-	// event names
-	EVTS = (function(/*string[]*/ names) {
-		var evts = {};
-		while (names.length) {
-			var evt = names.shift();
-			evts["on"+evt.toLowerCase()] = evt;
-		}
-		return evts;
-	})("blur,change,click,dblclick,error,focus,keydown,keypress,keyup,load,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,resize,scroll,select,submit,unload".split(','));
+		VAL = 4;
 
 	/**
 	 * Determines the type of the value
 		}
 	}
 
-	/**
-	 * Appends a child to an element
-	 * 
-	 * @param {DOMElement} elem The parent element
-	 * @param {DOMElement} child The child
-	 */
-	function appendChild(elem, child) {
-		if (child) {
-			
-			if (elem.tagName && elem.tagName.toLowerCase() === "table" && elem.tBodies) {
-				if (!child.tagName) {
-					// must unwrap documentFragment for tables
-					if (child.nodeType === 11) {
-						while (child.firstChild) {
-							appendChild(elem, child.removeChild(child.firstChild));
-						}
-					}
-					return;
-				}
-				// in IE must explicitly nest TRs in TBODY
-				var childTag = child.tagName.toLowerCase();// child tagName
-				if (childTag && childTag !== "tbody" && childTag !== "thead") {
-					// insert in last tbody
-					var tBody = elem.tBodies.length > 0 ? elem.tBodies[elem.tBodies.length-1] : null;
-					if (!tBody) {
-						tBody = document.createElement(childTag === "th" ? "thead" : "tbody");
-						elem.appendChild(tBody);
-					}
-					tBody.appendChild(child);
-				} else if (elem.canHaveChildren !== false) {
-					elem.appendChild(child);
-				}
-			} else if (elem.tagName && elem.tagName.toLowerCase() === "style" && document.createStyleSheet) {
-				// IE requires this interface for styles
-				elem.cssText = child;
-			} else if (elem.canHaveChildren !== false) {
-				elem.appendChild(child);
-			} else if (elem.tagName && elem.tagName.toLowerCase() === "object" &&
-				child.tagName && child.tagName.toLowerCase() === "param") {
-					// IE-only path
-					try {
-						elem.appendChild(child);
-					} catch (ex1) {}
-					try {
-						if (elem.object) {
-							elem.object[child.name] = child.value;
-						}
-					} catch (ex2) {}
-			}
-		}
-	}
-
-	/**
-	 * Appends a child to an element
-	 * 
-	 * @param {DOMElement} elem The element
-	 * @param {string} name The event name
-	 * @param {function(Event)} handler The event handler
-	 */
-	function addHandler(elem, name, handler) {
-		if ("string" === typeof handler) {
-			/*jslint evil:true */
-			handler = new Function("event", handler);
-			/*jslint evil:false */
-		}
-
-		if ("function" !== typeof handler) {
-			return;
-		}
-
-		elem[name] = handler;
-	}
-
-	/**
-	 * Appends a child to an element
-	 * 
-	 * @param {DOMElement} elem The element
-	 * @param {Object} attr Attributes object
-	 * @returns {DOMElement}
-	 */
-	function addAttributes(elem, attr) {
-		if (attr.name && document.attachEvent) {
-			try {
-				// IE fix for not being able to programatically change the name attribute
-				var alt = document.createElement("<"+elem.tagName+" name='"+attr.name+"'>");
-				// fix for Opera 8.5 and Netscape 7.1 creating malformed elements
-				if (elem.tagName === alt.tagName) {
-					elem = alt;
-				}
-			} catch (ex) { }
-		}
-
-		// for each attributeName
-		for (var name in attr) {
-			if (attr.hasOwnProperty(name)) {
-				// attributeValue
-				var value = attr[name];
-				if (name && value) {
-					name = ATTRMAP[name.toLowerCase()] || name;
-					if (name === "style") {
-						if ("undefined" !== typeof elem.style.cssText) {
-							elem.style.cssText = value;
-						} else {
-							elem.style = value;
-						}
-					} else if (name === "class") {
-						elem.className = value;
-					} else if (EVTS[name]) {
-						addHandler(elem, name, value);
-
-						// also set duplicated events
-						if (ATTRDUP[name]) {
-							addHandler(elem, ATTRDUP[name], value);
-						}
-					} else if ("string" === typeof value || "number" === typeof value || "boolean" === typeof value) {
-						elem.setAttribute(name, value);
-
-						// also set duplicated attributes
-						if (ATTRDUP[name]) {
-							elem.setAttribute(ATTRDUP[name], value);
-						}
-					} else {
-
-						// allow direct setting of complex properties
-						elem[name] = value;
-
-						// also set duplicated attributes
-						if (ATTRDUP[name]) {
-							elem[ATTRDUP[name]] = value;
-						}
-					}
-				}
-			}
-		}
-		return elem;
-	}
-
-	/**
-	 * Tests a node for whitespace
-	 * 
-	 * @param {DOMElement} node The node
-	 * @returns {boolean}
-	 */
-	function isWhitespace(node) {
-		return node && (node.nodeType === 3) && (!node.nodeValue || !/\S/.exec(node.nodeValue));
-	}
-
-	/**
-	 * Removes leading and trailing whitespace nodes
-	 * 
-	 * @param {DOMElement} elem The node
-	 */
-	function trimWhitespace(elem) {
-		if (elem) {
-			while (isWhitespace(elem.firstChild)) {
-				// trim leading whitespace text nodes
-				elem.removeChild(elem.firstChild);
-			}
-			while (isWhitespace(elem.lastChild)) {
-				// trim trailing whitespace text nodes
-				elem.removeChild(elem.lastChild);
-			}
-		}
-	}
-
-	/**
-	 * Removes leading and trailing whitespace nodes
-	 * 
-	 * @param {string} value The node
-	 * @returns {DOMElement}
-	 */
-	function hydrate(value) {
-		var wrapper = document.createElement("div");
-		wrapper.innerHTML = value;
-
-		// trim extraneous whitespace
-		trimWhitespace(wrapper);
-
-		// eliminate wrapper for single nodes
-		if (wrapper.childNodes.length === 1) {
-			return wrapper.firstChild;
-		}
-
-		// create a document fragment to hold elements
-		var frag = document.createDocumentFragment ?
-			document.createDocumentFragment() :
-			document.createElement("");
-
-		while (wrapper.firstChild) {
-			frag.appendChild(wrapper.firstChild);
-		}
-		return frag;
-	}
-
-	/**
-	 * Renders an error as a text node
-	 * 
-	 * @param {Error} ex The exception
-	 * @returns {DOMElement}
-	 */
-	function onError(ex) {
-		return document.createTextNode("["+ex+"]");
-	}
-
-	/**
-	 * Applies JsonML to DOM
-	 * 
-	 * @param {DOMElement} elem The element to append
-	 * @param {Array} jml The JsonML structure to build
-	 * @param {function(DOMElement):DOMElement} filter A filter method
-	 * @returns {DOMElement}
-	 */
-	function patch(elem, jml, filter) {
-
-		for (var i=1; i<jml.length; i++) {
-			if (jml[i] instanceof Array || "string" === typeof jml[i]) {
-				// append children
-				appendChild(elem, parse(jml[i], filter));
-			} else if (jml[i] instanceof Unparsed) {
-				appendChild(elem, hydrate(jml[i].value));
-			} else if ("object" === typeof jml[i] && jml[i] !== null && elem.nodeType === 1) {
-				// add attributes
-				elem = addAttributes(elem, jml[i]);
-			}
-		}
-
-		return elem;
-	}
-
-	/**
-	 * Builds DOM from JsonML
-	 * 
-	 * @param {Array} jml The JsonML structure to build
-	 * @param {function(DOMElement):DOMElement} filter A filter method
-	 * @returns {DOMElement}
-	 */
-	parse = function(jml, filter) {
-		try {
-			if (!jml) {
-				return null;
-			}
-			if ("string" === typeof jml) {
-				return document.createTextNode(jml);
-			}
-			if (jml instanceof Unparsed) {
-				return hydrate(jml.value);
-			}
-
-			var tag = jml[0]; // tagName
-			if (!tag) {
-				// correctly handle multiple-roots
-				// create a document fragment to hold elements
-				var frag = document.createDocumentFragment ?
-					document.createDocumentFragment() :
-					document.createElement("");
-				for (var i=1; i<jml.length; i++) {
-					appendChild(frag, parse(jml[i], filter));
-				}
-
-				// trim extraneous whitespace
-				trimWhitespace(frag);
-
-				// eliminate wrapper for single nodes
-				if (frag.childNodes.length === 1) {
-					return frag.firstChild;
-				}
-				return frag;
-			}
-
-			if (tag.toLowerCase() === "style" && document.createStyleSheet) {
-				// IE requires this interface for styles
-				patch(document.createStyleSheet(), jml, filter);
-				// in IE styles are effective immediately
-				return null;
-			}
-
-			var elem = patch(document.createElement(tag), jml, filter);
-
-			// trim extraneous whitespace
-			trimWhitespace(elem);
-			return (elem && "function" === typeof filter) ? filter(elem) : elem;
-		} catch (ex) {
-			try {
-				// handle error with complete context
-				var err = ("function" === typeof duel.onerror) ? duel.onerror : onError;
-				return err(ex, jml, filter);
-			} catch (ex2) {
-				return onError(ex2);
-			}
-		}
-	};
+	/* ToString methods --------------------*/
 
 	/**
 	 * Appends a node to a parent
 		return output.join("");
 	};
 
+	/* ToDom methods --------------------*/
+
+	//attribute name mapping
+	var ATTRMAP = {
+		rowspan : "rowSpan",
+		colspan : "colSpan",
+		cellpadding : "cellPadding",
+		cellspacing : "cellSpacing",
+		tabindex : "tabIndex",
+		accesskey : "accessKey",
+		hidefocus : "hideFocus",
+		usemap : "useMap",
+		maxlength : "maxLength",
+		readonly : "readOnly",
+		contenteditable : "contentEditable"
+		// can add more attributes here as needed
+	},
+
+	// attribute duplicates
+	ATTRDUP = {
+		enctype : "encoding",
+		onscroll : "DOMMouseScroll"
+		// can add more attributes here as needed
+	},
+
+	// event names
+	EVTS = (function(/*string[]*/ names) {
+		var evts = {};
+		while (names.length) {
+			var evt = names.shift();
+			evts["on"+evt.toLowerCase()] = evt;
+		}
+		return evts;
+	})("blur,change,click,dblclick,error,focus,keydown,keypress,keyup,load,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,resize,scroll,select,submit,unload".split(','));
+
+	/**
+	 * Appends a child to an element
+	 * 
+	 * @param {DOMElement} elem The parent element
+	 * @param {DOMElement} child The child
+	 */
+	function appendChild(elem, child) {
+		if (child) {
+			
+			if (elem.tagName && elem.tagName.toLowerCase() === "table" && elem.tBodies) {
+				if (!child.tagName) {
+					// must unwrap documentFragment for tables
+					if (child.nodeType === 11) {
+						while (child.firstChild) {
+							appendChild(elem, child.removeChild(child.firstChild));
+						}
+					}
+					return;
+				}
+				// in IE must explicitly nest TRs in TBODY
+				var childTag = child.tagName.toLowerCase();// child tagName
+				if (childTag && childTag !== "tbody" && childTag !== "thead") {
+					// insert in last tbody
+					var tBody = elem.tBodies.length > 0 ? elem.tBodies[elem.tBodies.length-1] : null;
+					if (!tBody) {
+						tBody = document.createElement(childTag === "th" ? "thead" : "tbody");
+						elem.appendChild(tBody);
+					}
+					tBody.appendChild(child);
+				} else if (elem.canHaveChildren !== false) {
+					elem.appendChild(child);
+				}
+			} else if (elem.tagName && elem.tagName.toLowerCase() === "style" && document.createStyleSheet) {
+				// IE requires this interface for styles
+				elem.cssText = child;
+			} else if (elem.canHaveChildren !== false) {
+				elem.appendChild(child);
+			} else if (elem.tagName && elem.tagName.toLowerCase() === "object" &&
+				child.tagName && child.tagName.toLowerCase() === "param") {
+					// IE-only path
+					try {
+						elem.appendChild(child);
+					} catch (ex1) {}
+					try {
+						if (elem.object) {
+							elem.object[child.name] = child.value;
+						}
+					} catch (ex2) {}
+			}
+		}
+	}
+
+	/**
+	 * Appends a child to an element
+	 * 
+	 * @param {DOMElement} elem The element
+	 * @param {string} name The event name
+	 * @param {function(Event)} handler The event handler
+	 */
+	function addHandler(elem, name, handler) {
+		if ("string" === typeof handler) {
+			/*jslint evil:true */
+			handler = new Function("event", handler);
+			/*jslint evil:false */
+		}
+
+		if ("function" !== typeof handler) {
+			return;
+		}
+
+		elem[name] = handler;
+	}
+
+	/**
+	 * Appends a child to an element
+	 * 
+	 * @param {DOMElement} elem The element
+	 * @param {Object} attr Attributes object
+	 * @returns {DOMElement}
+	 */
+	function addAttributes(elem, attr) {
+		if (attr.name && document.attachEvent) {
+			try {
+				// IE fix for not being able to programatically change the name attribute
+				var alt = document.createElement("<"+elem.tagName+" name='"+attr.name+"'>");
+				// fix for Opera 8.5 and Netscape 7.1 creating malformed elements
+				if (elem.tagName === alt.tagName) {
+					elem = alt;
+				}
+			} catch (ex) { }
+		}
+
+		// for each attributeName
+		for (var name in attr) {
+			if (attr.hasOwnProperty(name)) {
+				// attributeValue
+				var value = attr[name];
+				if (name && value) {
+					name = ATTRMAP[name.toLowerCase()] || name;
+					if (name === "style") {
+						if ("undefined" !== typeof elem.style.cssText) {
+							elem.style.cssText = value;
+						} else {
+							elem.style = value;
+						}
+					} else if (name === "class") {
+						elem.className = value;
+					} else if (EVTS[name]) {
+						addHandler(elem, name, value);
+
+						// also set duplicated events
+						if (ATTRDUP[name]) {
+							addHandler(elem, ATTRDUP[name], value);
+						}
+					} else if ("string" === typeof value || "number" === typeof value || "boolean" === typeof value) {
+						elem.setAttribute(name, value);
+
+						// also set duplicated attributes
+						if (ATTRDUP[name]) {
+							elem.setAttribute(ATTRDUP[name], value);
+						}
+					} else {
+
+						// allow direct setting of complex properties
+						elem[name] = value;
+
+						// also set duplicated attributes
+						if (ATTRDUP[name]) {
+							elem[ATTRDUP[name]] = value;
+						}
+					}
+				}
+			}
+		}
+		return elem;
+	}
+
+	/**
+	 * Tests a node for whitespace
+	 * 
+	 * @param {DOMElement} node The node
+	 * @returns {boolean}
+	 */
+	function isWhitespace(node) {
+		return node && (node.nodeType === 3) && (!node.nodeValue || !/\S/.exec(node.nodeValue));
+	}
+
+	/**
+	 * Removes leading and trailing whitespace nodes
+	 * 
+	 * @param {DOMElement} elem The node
+	 */
+	function trimWhitespace(elem) {
+		if (elem) {
+			while (isWhitespace(elem.firstChild)) {
+				// trim leading whitespace text nodes
+				elem.removeChild(elem.firstChild);
+			}
+			while (isWhitespace(elem.lastChild)) {
+				// trim trailing whitespace text nodes
+				elem.removeChild(elem.lastChild);
+			}
+		}
+	}
+
+	/**
+	 * Removes leading and trailing whitespace nodes
+	 * 
+	 * @param {string} value The node
+	 * @returns {DOMElement}
+	 */
+	function hydrate(value) {
+		var wrapper = document.createElement("div");
+		wrapper.innerHTML = value;
+
+		// trim extraneous whitespace
+		trimWhitespace(wrapper);
+
+		// eliminate wrapper for single nodes
+		if (wrapper.childNodes.length === 1) {
+			return wrapper.firstChild;
+		}
+
+		// create a document fragment to hold elements
+		var frag = document.createDocumentFragment ?
+			document.createDocumentFragment() :
+			document.createElement("");
+
+		while (wrapper.firstChild) {
+			frag.appendChild(wrapper.firstChild);
+		}
+		return frag;
+	}
+
+	/**
+	 * Renders an error as a text node
+	 * 
+	 * @param {Error} ex The exception
+	 * @returns {DOMElement}
+	 */
+	function onError(ex) {
+		return document.createTextNode("["+ex+"]");
+	}
+
+	/**
+	 * Applies JsonML to DOM
+	 * 
+	 * @param {DOMElement} elem The element to append
+	 * @param {Array} jml The JsonML structure to build
+	 * @param {function(DOMElement):DOMElement} filter A filter method
+	 * @returns {DOMElement}
+	 */
+	function patch(elem, jml, filter) {
+
+		for (var i=1; i<jml.length; i++) {
+			if (jml[i] instanceof Array || "string" === typeof jml[i]) {
+				// append children
+				appendChild(elem, parse(jml[i], filter));
+			} else if (jml[i] instanceof Unparsed) {
+				appendChild(elem, hydrate(jml[i].value));
+			} else if ("object" === typeof jml[i] && jml[i] !== null && elem.nodeType === 1) {
+				// add attributes
+				elem = addAttributes(elem, jml[i]);
+			}
+		}
+
+		return elem;
+	}
+
+	/**
+	 * Builds DOM from JsonML
+	 * 
+	 * @param {Array} jml The JsonML structure to build
+	 * @param {function(DOMElement):DOMElement} filter A filter method
+	 * @returns {DOMElement}
+	 */
+	parse = function(jml, filter) {
+		try {
+			if (!jml) {
+				return null;
+			}
+			if ("string" === typeof jml) {
+				return document.createTextNode(jml);
+			}
+			if (jml instanceof Unparsed) {
+				return hydrate(jml.value);
+			}
+
+			var tag = jml[0]; // tagName
+			if (!tag) {
+				// correctly handle multiple-roots
+				// create a document fragment to hold elements
+				var frag = document.createDocumentFragment ?
+					document.createDocumentFragment() :
+					document.createElement("");
+				for (var i=1; i<jml.length; i++) {
+					appendChild(frag, parse(jml[i], filter));
+				}
+
+				// trim extraneous whitespace
+				trimWhitespace(frag);
+
+				// eliminate wrapper for single nodes
+				if (frag.childNodes.length === 1) {
+					return frag.firstChild;
+				}
+				return frag;
+			}
+
+			if (tag.toLowerCase() === "style" && document.createStyleSheet) {
+				// IE requires this interface for styles
+				patch(document.createStyleSheet(), jml, filter);
+				// in IE styles are effective immediately
+				return null;
+			}
+
+			var elem = patch(document.createElement(tag), jml, filter);
+
+			// trim extraneous whitespace
+			trimWhitespace(elem);
+			return (elem && "function" === typeof filter) ? filter(elem) : elem;
+		} catch (ex) {
+			try {
+				// handle error with complete context
+				var err = ("function" === typeof duel.onerror) ? duel.onerror : onError;
+				return err(ex, jml, filter);
+			} catch (ex2) {
+				return onError(ex2);
+			}
+		}
+	};
+
+	/* public methods --------------------*/
+
 	/**
 	 * @param {Array|Object|string|function(*,number,number):Array|Object|string} view The view template
 	 * @returns {Template}