@@ -59,12 +59,17 @@ function render(tree, options) {
59
59
replaceQuote = true ;
60
60
}
61
61
62
+ let { quoteStyle} = options ;
63
+ if ( quoteStyle === undefined ) {
64
+ quoteStyle = 2 ;
65
+ }
66
+
62
67
return html ( tree ) ;
63
68
64
69
/** @private */
65
70
function isSingleTag ( tag ) {
66
71
if ( singleRegExp . length > 0 ) {
67
- return singleRegExp . some ( reg => reg . test ( tag ) )
72
+ return singleRegExp . some ( reg => reg . test ( tag ) ) ;
68
73
}
69
74
70
75
if ( ! singleTags . includes ( tag ) ) {
@@ -87,7 +92,7 @@ function render(tree, options) {
87
92
attrValue = object [ key ] . replace ( / " / g, '"' ) ;
88
93
}
89
94
90
- attr += ' ' + key + '="' + attrValue + '"' ;
95
+ attr += makeAttr ( key , attrValue , quoteStyle ) ;
91
96
} else if ( object [ key ] === '' ) {
92
97
attr += ' ' + key ;
93
98
} else {
@@ -96,7 +101,7 @@ function render(tree, options) {
96
101
} else if ( object [ key ] === true ) {
97
102
attr += ' ' + key ;
98
103
} else if ( typeof object [ key ] === 'number' ) {
99
- attr += ' ' + key + '="' + object [ key ] + '"' ;
104
+ attr += makeAttr ( key , object [ key ] , quoteStyle ) ;
100
105
}
101
106
}
102
107
@@ -112,6 +117,26 @@ function render(tree, options) {
112
117
}
113
118
}
114
119
120
+ /** @private */
121
+ function makeAttr ( key , attrValue , quoteStyle = 1 ) {
122
+ if ( quoteStyle === 1 ) {
123
+ // Single Quote
124
+ return ` ${ key } ='${ attrValue } '` ;
125
+ }
126
+
127
+ if ( quoteStyle === 2 ) {
128
+ // Double Quote
129
+ return ` ${ key } ="${ attrValue } "` ;
130
+ }
131
+
132
+ // Smart Quote
133
+ if ( attrValue . includes ( '"' ) ) {
134
+ return ` ${ key } ='${ attrValue } '` ;
135
+ }
136
+
137
+ return ` ${ key } ="${ attrValue } "` ;
138
+ }
139
+
115
140
/**
116
141
* HTML Stringifier
117
142
*
@@ -129,10 +154,10 @@ function render(tree, options) {
129
154
traverse ( tree , node => {
130
155
// Undefined, null, '', [], NaN
131
156
if ( node === undefined ||
132
- node === null ||
133
- node === false ||
134
- node . length === 0 ||
135
- Number . isNaN ( node ) ) {
157
+ node === null ||
158
+ node === false ||
159
+ node . length === 0 ||
160
+ Number . isNaN ( node ) ) {
136
161
return ;
137
162
}
138
163
0 commit comments