var PRETTY_INDENTATION = " "; class ENode{ constructor(tag){ this.__tag = tag; this.__classes = []; this.__attribs = []; this.__children = []; } appendChild(c){ var val = (c instanceof ENode) ? c : c.toString(); this.__children.push(c); return this; } addClass(cname){ this.__classes.push(cname); } addAttribute(aname, aval){ this.__attribs.push([aname, aval]); } toPrettyString(depth){ depth || (depth = 0); var ind = ""; var strind = ""; var eol = ""; if (depth > -1){ if (depth > 0) ind = PRETTY_INDENTATION + (new Array(depth)).join(PRETTY_INDENTATION); strind = ind + PRETTY_INDENTATION; eol = "\n"; } var r = ind + "<" + this.__tag.toUpperCase(); if (this.__attribs.length > 0){ var attr = this.__attribs.reduce(function(a, at){ a += ((a !== "") ? " " : "") + at[0].trim() + "=\"" + at[1] + "\""; return a; }, ""); r += " " + attr; } if (this.__children.length > 0){ r += ">" + eol; var lcn = true // lcn = Last Child was Node; r += this.__children.reduce(function(a, c){ if (c instanceof ENode){ a += ((lcn === false) ? "\n" : "") + ((depth >= 0) ? c.toPrettyString(depth + 1) : c.toString()); lcn = true; } else { a += strind + c; lcn = false; } return a; }, ""); r += ((lcn === false) ? eol : "") + ind + "" + eol; } else { r += " />" + eol; } return r; } toString(){ return this.toPrettyString(-1); } } function el(){ if (arguments.length < 1) throw new Error("Invalid number of arguments."); var ename = arguments[0]; var args = Array.prototype.slice.call(arguments, 1); var e = new ENode(ename); args.forEach(function(item){ if (item === null){return;} // If given an array, assume it to be an array children. if (item instanceof Array){ item.forEach(itm => e.appendChild((itm instanceof ENode) ? itm : itm.toString())); // If given an ENode, then it IS a child } else if (item instanceof ENode){ e.appendChild(item); // All objects are assumed to be attributes. } else if (item instanceof Object){ var keys = Object.keys(item); keys.forEach(function(key){ if (item.hasOwnProperty(key)){ if (typeof(item[key]) === "string"){ e.addAttribute(key.toString(), item[key]); } } }); // Everything else is assumed to be a child. } else { e.appendChild(item.toString()); } }); return e; } module.exports = { ENode: ENode, el: el, html: el.bind(null, "html"), head: el.bind(null, "head"), title: el.bind(null, "title"), script: el.bind(null, "script"), link: el.bind(null, "link"), meta: el.bind(null, "meta"), body: el.bind(null, "body"), div: el.bind(null, "div"), span: el.bind(null, "span"), a: el.bind(null, "a"), p: el.bind(null, "p"), b: el.bind(null, "b"), i: el.bind(null, "i"), br: el.bind(null, "br"), ol: el.bind(null, "ol"), ul: el.bind(null, "ul"), li: el.bind(null, "li"), em: el.bind(null, "em"), img: el.bind(null, "img"), font: el.bind(null, "font"), h1: el.bind(null, "h1"), h2: el.bind(null, "h2"), h3: el.bind(null, "h3"), h4: el.bind(null, "h4"), h5: el.bind(null, "h5"), h6: el.bind(null, "h6"), hr: el.bind(null, "hr"), blockquote: el.bind(null, "blockquote"), button: el.bind(null, "button"), nav: el.bind(null, "nav"), canvas: el.bind(null, "canvas"), caption: el.bind(null, "caption"), indentationChar: function(c){ if (type(c) === "string" && c.length > 0){ PRETTY_INDENTATION = c; } return PRETTY_INDENTATION; } };