@@ -39,6 +39,7 @@ const defaultOptions = {
39
39
attrValueProcessor : function ( a ) {
40
40
return a ;
41
41
} ,
42
+ singleTags : [ ] ,
42
43
attributeProtectArray : [ ] // 哪些属性的值为''但需要渲染出来,默认:如果value为''就不生成key=value,只生成key
43
44
} ;
44
45
@@ -54,6 +55,7 @@ const props = [
54
55
'supressEmptyNode' ,
55
56
'tagValueProcessor' ,
56
57
'attrValueProcessor' ,
58
+ 'singleTags' ,
57
59
'attributeProtectArray'
58
60
] ;
59
61
@@ -116,22 +118,22 @@ Parser.prototype.j2x = function (jObj, level) {
116
118
val += this . indentate ( level ) + '<' + key + '/' + this . tagEndChar ;
117
119
} else if ( jObj [ key ] instanceof Date ) {
118
120
val += this . buildTextNode ( jObj [ key ] , key , '' , level ) ;
119
- } else if ( key === '__children' ) { // 生成子节点
121
+ } else if ( key === '__children' ) { // 生成子节点
120
122
const item = jObj [ key ] ;
121
123
122
- if ( item instanceof Array ) {
123
- item . forEach ( i => {
124
+ if ( item instanceof Array ) {
125
+ item . forEach ( i => {
124
126
const result = this . j2x ( i , level + 1 ) ;
125
127
val += result . val ;
126
128
} ) ;
127
- } else
128
- if ( typeof item === 'object' ) {
129
- console . info ( `不应该出现的意外` ) ;
130
- } else {
131
- val += this . buildTextNode ( item , key , '' , level ) ;
132
- }
133
- }
134
-
129
+ } else
130
+ if ( typeof item === 'object' ) {
131
+ console . info ( `不应该出现的意外` ) ;
132
+ } else {
133
+ val += this . buildTextNode ( item , key , '' , level ) ;
134
+ }
135
+ }
136
+
135
137
else if ( typeof jObj [ key ] !== 'object' ) {
136
138
//premitive type
137
139
const attr = this . isAttribute ( key ) ;
@@ -144,7 +146,7 @@ Parser.prototype.j2x = function (jObj, level) {
144
146
if ( attr ) {
145
147
if ( typeof jObj [ key ] === "boolean" && jObj [ key ] ) {
146
148
attrStr += ` ${ key } ` ;
147
- } else if ( jObj [ key ] || this . options . attributeProtectArray . includes ( key ) ) {
149
+ } else if ( jObj [ key ] || this . options . attributeProtectArray . includes ( key ) ) {
148
150
attrStr += ' ' + key + '="' + this . options . attrValueProcessor ( '' + jObj [ key ] ) + '"' ;
149
151
} else {
150
152
attrStr += ' ' + key ;
@@ -167,8 +169,8 @@ Parser.prototype.j2x = function (jObj, level) {
167
169
}
168
170
}
169
171
}
170
-
171
-
172
+
173
+
172
174
else if ( Array . isArray ( jObj [ key ] ) ) {
173
175
//repeated nodes
174
176
if ( this . isCDATA ( key ) ) {
@@ -234,7 +236,7 @@ function replaceCDATAarr(str, cdata) {
234
236
function buildObjectNode ( val , key , attrStr , level ) {
235
237
if ( attrStr && ! val . includes ( '<' ) ) {
236
238
237
- if ( key === "img" || key === "input" ) {
239
+ if ( key === "img" || key === "a-icon" || key === " input" || ( this . options . singleTags && this . options . singleTags . includes ( key ) ) ) {
238
240
return ( this . indentate ( level ) + '<' + key + attrStr + '/>' ) ;
239
241
}
240
242
0 commit comments