-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.dom.js
214 lines (201 loc) · 5.9 KB
/
html.dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
String.prototype.toKebabCase=function(){
let fix=this.replace(/([A-Z])/g,"-$1").toLowerCase();
if(fix[0]==`-`){
return fix.slice(1);
}else{
return fix;
}
}
String.prototype.toPascalCase=function(){
let fixs=this.split(`-`);
for(let i=1; i<fixs.length; i++){
let f=fixs[i].split(``);
f[0]=f[0].toUpperCase();
fixs[i]=f.join(``);
}
return fixs.join(``);
}
String.prototype.replaceAll=function(org,tgt){
return this.split(org).join(tgt);
}
const generalStrSymbol=`*%*`;
getDOMHtml=DOMHtml=function(dom_tag,dom_attr,dom_html,dom_fix={}){
let domFullHtml=[];
//dom_tag为数组时,批量为母元素添加元素
if(typeof dom_tag==`object` && dom_tag.length!=undefined){
let default_children={
tag:undefined,attr:undefined,html:undefined,attachType:`append`
};
for(let cur of dom_tag){
cur=JSON.parse(JSON.stringify({
...default_children,
...cur,
}));
domFullHtml.push(getDOMHtml(cur,undefined,undefined,dom_fix));
}
return domFullHtml.join(``);
}
//dom_tag为对象时,和普通情况一样
if(typeof dom_tag==`object` && dom_tag.length==undefined){
dom_tag=JSON.parse(JSON.stringify(dom_tag));
dom_attr=dom_tag.attr || dom_tag;
dom_tag=dom_tag.tag;
}
dom_html=dom_attr.html || dom_html;
let dom_attr_fix_blacklist=[
`tag`,`attachType`,
]
let dom_attr_fix_replace={
...{tagName:`tag`, attrName:`attr`,tag_name:`tagName`,attr_name:`attrName`,},
...dom_fix.attr,
}
let dom_attr_value_fix_keywords=[
...dom_fix.attrKeywords,
]
let dom_attr_value_fix_replace=[
...dom_fix.attrValue,
]
let dom_attr_fix={};
for(let key in dom_attr){
if(!dom_attr_fix_blacklist.includes(key)){
let key_fix=key;
let val_fix=dom_attr[key];
for(let origin in dom_attr_fix_replace){
key_fix=key_fix.replace(origin,dom_attr_fix_replace[origin]);
}
if(!key_fix.includes(`:`)){
key_fix=key_fix.toKebabCase();
}else{
key_fix=key_fix.toLowerCase();
}
for(let rep of dom_attr_value_fix_replace){
for(let fk of dom_attr_value_fix_keywords){
if(key_fix.includes(fk) && key_fix!=`tag` && key_fix!=`html`){
// if(rep.origin==generalStrSymbol){
if(typeof rep.origin!=`string`){
let val_fix_match=val_fix.match(rep.origin);
if(val_fix_match){
for(let m of val_fix_match){
val_fix=val_fix.replace(m, rep.target.replaceAll(generalStrSymbol,m));
}
}
// val_fix=val_fix.replace(rep.origin,rep.target.replace(generalStrSymbol,val_fix));
// val_fix=rep.target.replace(generalStrSymbol,val_fix);
// if(!val_fix.includes(rep.target.split(generalStrSymbol)[0]) && !val_fix.includes(rep.target.split(generalStrSymbol)[1])){
// val_fix=rep.target.replace(generalStrSymbol,val_fix);
// }
}else{
val_fix=val_fix.replaceAll(rep.origin,rep.target);
}
}
}
}
dom_attr_fix[key_fix]=val_fix;
}
}
dom_attr=dom_attr_fix;
dom_tag=dom_tag.toKebabCase();
if(typeof dom_attr==`object`){
if(typeof dom_attr.class==`object` && dom_attr.class.length){
dom_attr.class=dom_attr.class.join(` `).trim();
}else if(typeof dom_attr.class==`object` && dom_attr.class.length==undefined){
let classList=[];
for(let key in dom_attr.class){
if(dom_attr.class[key]==true){
classList.push(key);
}
}
dom_attr.class=classList.join(` `).trim();
}
if(typeof dom_attr.style==`object`){
let styleList=[];
for(let key in dom_attr.style){
styleList.push(`${key.toKebabCase()}: ${dom_attr.style[key]}`);
}
dom_attr.style=styleList.join(`;`);
}
}
let attr_blacklist=[
`bind`,`children`,`html`,`tbody`,`tr`,`td`,
]
let domElement=document.createElement(dom_tag);
if(typeof dom_attr==`object`){
for(let key in dom_attr){
if(!attr_blacklist.includes(key)){
domElement.setAttribute(key, dom_attr[key]);
}
}
}else if(typeof dom_attr==`string`){
domFullHtml.push(dom_attr);
}
if(dom_html){
domFullHtml.push(dom_html);
}
if(typeof dom_attr==`object` && typeof dom_attr.children==`object`){
let default_children={
tag:undefined,attr:undefined,html:undefined,
};
if(dom_attr.children.length==undefined){
/*仅一个子项时,可以直接使用Object
{
tag:`html`,attr:{id:`id`},html:`Test`,attachType:`append`
}
*/
let children={
...JSON.parse(JSON.stringify(default_children)),
...dom_attr.children,
}
domFullHtml.push(getDOMHtml(children,undefined,undefined,dom_fix));
}else{
/*多个子项时,采用数组形式
[
{
tag:`html`,attr:{id:`id1`},html:`Test1`,attachType:`append`
},
{
tag:`html`,attr:{id:`id2`},html:`Test2`,attachType:`append`
},
]
*/
for(let i=0; i<dom_attr.children.length; i++){
let children={
...JSON.parse(JSON.stringify(default_children)),
...dom_attr.children[i],
}
domFullHtml.push(getDOMHtml(children,undefined,undefined,dom_fix));
}
}
}
if(typeof dom_attr==`object` && (typeof dom_attr.tbody==`object` || typeof dom_attr.tr==`object`)){
let default_tr={
tag:`tr`,attr:undefined,html:undefined,children:[],
};
let default_td={
tag:`td`,attr:undefined,html:undefined,children:[],
}
let trList=dom_attr.tbody || dom_attr.tr;
let domTrHtml=[];
for(let i=0; i<trList.length; i++){
let curTr=trList[i];
let tr={
...JSON.parse(JSON.stringify(default_tr)),
...curTr
}
for(let j=0; j<curTr.td.length; j++){
let curTd=curTr.td[j];
if(typeof curTd==`string`){
curTd={html:curTd};
}
let td={
...JSON.parse(JSON.stringify(default_td)),
...curTd,
}
tr.children.push(td);
}
domTrHtml.push(tr);
}
domFullHtml.push(getDOMHtml(domTrHtml,undefined,undefined,dom_fix));
}
domElement.innerHTML=domFullHtml.join(``);
return domElement.outerHTML;
}