|
-
-
-
- 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]);
- }
-
- toString(){
- var r = "<" + this.__tag.toUpperCase();
- if (this.__classes.length > 0){
- var cls = this.__classes.reduce(function(a, c){
- c = c.split(" ");
- for (var i=0; i < c.length; i++){
- c[i] = c[i].trim();
- a += ((a !== "" && c[i].length > 0) ? " " : "") + c[i];
- }
- return a;
- }, "");
- r += " class=\"" + cls + "\"";
- }
-
- 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 += ">";
- r += this.__children.reduce(function(a, c){
- if (c instanceof ENode){
- a += c.toString() + "\n";
- } else {
- a += c;
- }
- return a;
- }, "");
- r += "</" + this.__tag.toUpperCase() + ">";
- } else {
- r += " />";
- }
- return r;
- }
- }
-
-
- function appendCTX(el, ctx){
- el.appendChild((ctx instanceof ENode) ? ctx : ctx.toString());
- }
-
- function el(ename, ctx, cls, attr){
- var e = new ENode(ename);
- if (ctx){
- if (ctx instanceof Array){
- ctx.forEach(item => appendCTX(e, item));
- } else {
- appendCTX(e, ctx);
- }
- }
-
- if (cls){
- var cl = cls;
- if (typeof(cl) === "String")
- cl = cl.split(" ");
- if (cl instanceof Array){
- cl.forEach(c => e.addClass(c.trim()));
- }
- }
- if (attr){
- var keys = Object.keys(attr);
- keys.forEach(function(key){
- if (attr.hasOwnProperty(key)){
- if (typeof(attr[key]) === "string"){
- e.addAttribute(key.toString(), attr[key]);
- }
- }
- });
- }
-
- 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")
- };
-
-
-
-
-
-
|