/** Write HTML without leaving Javascript land. Create DOM Elements with nestable arrays that reflect the structure of HTML. @author Raine Lourie @note Created independently from JsonML (http://jsonml.org). */ var Creatable = (function() { /****************************************** * DOM Emulation ******************************************/ // Emulated TextNode var TextNode = function(value) { this.value = value; }; TextNode.prototype.render = function() { return this.value; }; // Emulated Document Fragment var DocumentFragment = function() { this.children = []; }; DocumentFragment.prototype.appendChild = function(child) { if(this.children.length === 0) { this.firstChild = child; } this.children.push(child); }; DocumentFragment.prototype.render = function() { var output = ""; for(var i=0; i"; return output; } // Emulated document var emulatedDocument = { createTextNode: function(content) { return new TextNode(content); }, createDocumentFragment: function() { return new DocumentFragment(); }, createElement: function(tagName) { return new Element(tagName); }, body: new Element("body"), render: function() { return this.body.render(); }, }; // emulate DOM if document doesn't exit var doc = typeof(document) != "undefined" ? document : emulatedDocument; /****************************************** * Regexes ******************************************/ var regexIdOrClassSeparator = new RegExp("[#.]"); var regexIdOrClass = new RegExp("[#.][^#.]+", "g"); /****************************************** * Private ******************************************/ var map = function(arr, f) { var output = []; for(var i=0; i -1; }; /** Adds the given className to the element's class attribute. */ var addClass = function(element, className) { element.setAttribute("class", element.hasAttribute("class") ? element.getAttribute("class") + " " + className : className); }; /** Adds the given children to the element. */ var addChildren = function(element, children) { if(typeof children === "string" || typeof children === "number") { children = [children]; } if(children instanceof Array) { for(var i=0; i= 1 && arr.length <= 3 && // 1, 2, or 3 items //typeof arr[0] === "string" // tagname is a string }; var mergeAttributes = function(a, b) { var uniqueClass = function(singleClass) { return !Creatable.containsClass(a["class"], singleClass); }; var output = {}; for(var aProp in a) { output[aProp] = a[aProp]; } for(var bProp in b) { output[bProp] = b[bProp]; } // merge class attributes output["class"] = a["class"] && b["class"] ? [].concat(a["class"], filter(b["class"].split(" "), uniqueClass)).join(" ") : a["class"] || b["class"]; return output; }; /****************************************** * Error Handling ******************************************/ /** Abstracts the error handling for Creatable so that we can substitute a different handler if necessary. */ var error = function(message, params) { if(params) { console.error(params); } throw new Error(message || "ERROR"); }; /****************************************** * Render Helper ******************************************/ var render = function(sexp) { var body = doc.body; while(body.firstChild) { body.removeChild(body.firstChild); } body.appendChild(Creatable.create(sexp)); }; /** Adds autoincremented tab-indexes to given elements (only modifying markup arrays). */ var autoTabIndex = function(elements, start) { start = start || 1; var counter = start; var results = []; for(var i=0, len=elements.length; i 1 && typeOf(el[1]) == "object") { el[1].tabindex = counter; newEl = el; } else { newEl = splice(el, 1, 0, { tabindex: counter }); } counter++; } else { newEl = el; } results.push(newEl); } return results; }; // return public interface return { document : emulatedDocument, TextNode : TextNode, Element : Element, DocumentFragment : DocumentFragment, create : create, types : types, plugins : plugins, parseMarkupArray : parseMarkupArray, parseTagName : parseTagName, parseSelectorAttributes : parseSelectorAttributes, addAttributes : addAttributes, containsClass : containsClass, addClass : addClass, addChildren : addChildren, isValue : isValue, isDomNode : isDomNode , isMarkupArray : isMarkupArray, mergeAttributes : mergeAttributes, error : error, render : render, autoTabIndex : autoTabIndex, }; })(); // nodejs module if(typeof(module) != "undefined") { module.exports = Creatable; }