function isNil(obj:any): boolean {
return typeof obj === "undefined" || obj === null;
}

function fixCls(clsStr: string): string {
if (!clsStr) {
return "";
}

let clsArray = clsStr.split(" ");
clsArray = clsArray.filter((cls) => {
return cls.length > 0;
});
if (clsArray.length === 0) {
return "";
}

clsArray = clsArray.map((cls) => {
if (cls.startsWith("next")) {
return cls;
}
return `SOME_PREFIX_${cls}`;
})

return clsArray.join(" ");
}

const html2EscapeEnums: Record<string, string> = {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
'"': '&quot;'
};

function html2Escape(sHtml: string) {
return sHtml.replace(/[<>&"]/g, function (c) {
return html2EscapeEnums[c] || c;
});
}

function appendChild(element: HTMLElement, child: any) {
if (isNil(child)) {
return;
} else if (typeof child === "string") {
const txt = html2Escape(child);
const oTxt = document.createTextNode(txt);
element.appendChild(oTxt);
return;
} else if (child instanceof HTMLElement) {
element.appendChild(child);
return;
} else {
const oTxt = document.createTextNode('' + child);
element.appendChild(oTxt);
}
}

function createElement(tagName: string, clazz: string, children?: any) {
const element = document.createElement(tagName);
element.className = fixCls(clazz);
if (!isNil(children)) {
if (Array.isArray(children)) {
for (let i = 0; i < children.length; i++) {
const child = children[i];
appendChild(element, child);
}
} else {
appendChild(element, children);
}
}
return element;
}

function div(clazz: string, children?: any): HTMLElement {
return createElement('div', clazz, children);
}

function span(clazz: string, children?: any): HTMLElement {
return createElement('span', clazz, children);
}

function img(clazz: string, src: string): HTMLElement {
const img = createElement('img', clazz);
img.setAttribute('src', src);
return img;
}

function ifElse(c: boolean, arr: HTMLElement[]): HTMLElement | null {
const a = isNil(arr[0]) ? null : arr[0];
const b = isNil(arr[1]) ? null : arr[1];
return c ? a : b;
}

export {
span,
div,
img,
ifElse
}

最新文章

  1. linux磁盘分区模式
  2. btrfs-snapper 实现Linux 文件系统快照回滚
  3. bzoj1067 降雨量&amp;&amp;vijos1265 暴风雨
  4. 《Linux内核分析》第六周 进程的描述与创建
  5. 非常全面的java基础笔试题
  6. nginx 3.nginx+fastcgi
  7. C# Base64解码 二进制输出
  8. C#模板打印功能-模板为WPS或Excel
  9. Python 3 使用venv创建虚拟环境
  10. POJ3155 Hard Life [最大密度子图]
  11. DB2开发系列之四——触发器
  12. bzoj5107: [CodePlus2017]找爸爸
  13. 剑指offer——python【第29题】最小的K个数
  14. C语言基础复习:字符,字符数组,字符串,字符指针
  15. Libevent源码分析—event_base_dispatch()
  16. day17-json格式转换
  17. 『Sklearn』框架自带数据集接口
  18. (转)MySQL登陆后提示符的修改
  19. 基于curl的异步http实现
  20. window7下 cmd命令行 Mysql导出表结构 + 表数据

热门文章

  1. 代码随想录算法训练营day06 | leetcode 242、349 、202、1
  2. Postgres14.4(Docker安装)
  3. Mbps 与 MBps
  4. 自己动手从零写桌面操作系统GrapeOS系列教程——6.电脑启动过程介绍
  5. python编辑excel表格文件的简单方法练习
  6. 简述traceroute命令的原理
  7. SOJ1737 题解
  8. 自己写的垃圾shell
  9. VMware-查看虚拟机版本
  10. EF和dapper