/** 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). */ this.Creatable = (function() { /****************************************** * Regexes ******************************************/ var regexIdOrClassSeparator = new RegExp("[#.]"); var regexIdOrClass = new RegExp("[#.][^#.]+", "g"); /****************************************** * Functional ******************************************/ var map = function(arr, f) { var output = []; for(var i=0; i -1; }, /** Adds the given className to the element's class attribute. */ addClass: function(element, className) { element.setAttribute("class", element.hasAttribute("class") ? element.getAttribute("class") + " " + className : className); }, /** Adds the given children to the element. */ 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 }, 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. */ error: function(message) { throw new Error(message || "ERROR"); } }; })(); // add create into the global namespace this.create = Creatable.create;