1
1
import { textElems } from '../plugins/_collections.js' ;
2
2
3
3
/**
4
- * @typedef {import('./types.js').XastParent } XastParent
5
- * @typedef {import('./types.js').XastRoot } XastRoot
4
+ * @typedef {import('./types.js').StringifyOptions } StringifyOptions
5
+ * @typedef {import('./types.js').XastCdata } XastCdata
6
+ * @typedef {import('./types.js').XastComment } XastComment
7
+ * @typedef {import('./types.js').XastDoctype } XastDoctype
6
8
* @typedef {import('./types.js').XastElement } XastElement
7
9
* @typedef {import('./types.js').XastInstruction } XastInstruction
8
- * @typedef {import('./types.js').XastDoctype } XastDoctype
10
+ * @typedef {import('./types.js').XastParent } XastParent
11
+ * @typedef {import('./types.js').XastRoot } XastRoot
9
12
* @typedef {import('./types.js').XastText } XastText
10
- * @typedef {import('./types.js').XastCdata } XastCdata
11
- * @typedef {import('./types.js').XastComment } XastComment
12
- * @typedef {import('./types.js').StringifyOptions } StringifyOptions
13
13
* @typedef {Required<StringifyOptions> } Options
14
14
*
15
15
* @typedef State
@@ -114,28 +114,29 @@ export const stringifySvg = (data, userOptions = {}) => {
114
114
*/
115
115
const stringifyNode = ( data , config , state ) => {
116
116
let svg = '' ;
117
- state . indentLevel += 1 ;
117
+ state . indentLevel ++ ;
118
118
for ( const item of data . children ) {
119
- if ( item . type === 'element' ) {
120
- svg += stringifyElement ( item , config , state ) ;
121
- }
122
- if ( item . type === 'text' ) {
123
- svg += stringifyText ( item , config , state ) ;
124
- }
125
- if ( item . type === 'doctype' ) {
126
- svg += stringifyDoctype ( item , config ) ;
127
- }
128
- if ( item . type === 'instruction' ) {
129
- svg += stringifyInstruction ( item , config ) ;
130
- }
131
- if ( item . type === 'comment' ) {
132
- svg += stringifyComment ( item , config ) ;
133
- }
134
- if ( item . type === 'cdata' ) {
135
- svg += stringifyCdata ( item , config , state ) ;
119
+ switch ( item . type ) {
120
+ case 'element' :
121
+ svg += stringifyElement ( item , config , state ) ;
122
+ break ;
123
+ case 'text' :
124
+ svg += stringifyText ( item , config , state ) ;
125
+ break ;
126
+ case 'doctype' :
127
+ svg += stringifyDoctype ( item , config ) ;
128
+ break ;
129
+ case 'instruction' :
130
+ svg += stringifyInstruction ( item , config ) ;
131
+ break ;
132
+ case 'comment' :
133
+ svg += stringifyComment ( item , config ) ;
134
+ break ;
135
+ case 'cdata' :
136
+ svg += stringifyCdata ( item , config , state ) ;
136
137
}
137
138
}
138
- state . indentLevel -= 1 ;
139
+ state . indentLevel -- ;
139
140
return svg ;
140
141
} ;
141
142
@@ -215,59 +216,59 @@ const stringifyElement = (node, config, state) => {
215
216
stringifyAttributes ( node , config ) +
216
217
config . tagShortEnd
217
218
) ;
218
- } else {
219
- return (
220
- createIndent ( config , state ) +
221
- config . tagShortStart +
222
- node . name +
223
- stringifyAttributes ( node , config ) +
224
- config . tagOpenEnd +
225
- config . tagCloseStart +
226
- node . name +
227
- config . tagCloseEnd
228
- ) ;
229
- }
230
- // non-empty element
231
- } else {
232
- let tagOpenStart = config . tagOpenStart ;
233
- let tagOpenEnd = config . tagOpenEnd ;
234
- let tagCloseStart = config . tagCloseStart ;
235
- let tagCloseEnd = config . tagCloseEnd ;
236
- let openIndent = createIndent ( config , state ) ;
237
- let closeIndent = createIndent ( config , state ) ;
238
-
239
- if ( state . textContext ) {
240
- tagOpenStart = defaults . tagOpenStart ;
241
- tagOpenEnd = defaults . tagOpenEnd ;
242
- tagCloseStart = defaults . tagCloseStart ;
243
- tagCloseEnd = defaults . tagCloseEnd ;
244
- openIndent = '' ;
245
- } else if ( textElems . has ( node . name ) ) {
246
- tagOpenEnd = defaults . tagOpenEnd ;
247
- tagCloseStart = defaults . tagCloseStart ;
248
- closeIndent = '' ;
249
- state . textContext = node ;
250
- }
251
-
252
- const children = stringifyNode ( node , config , state ) ;
253
-
254
- if ( state . textContext === node ) {
255
- state . textContext = null ;
256
219
}
257
220
258
221
return (
259
- openIndent +
260
- tagOpenStart +
222
+ createIndent ( config , state ) +
223
+ config . tagShortStart +
261
224
node . name +
262
225
stringifyAttributes ( node , config ) +
263
- tagOpenEnd +
264
- children +
265
- closeIndent +
266
- tagCloseStart +
226
+ config . tagOpenEnd +
227
+ config . tagCloseStart +
267
228
node . name +
268
- tagCloseEnd
229
+ config . tagCloseEnd
269
230
) ;
270
231
}
232
+
233
+ // non-empty element
234
+ let tagOpenStart = config . tagOpenStart ;
235
+ let tagOpenEnd = config . tagOpenEnd ;
236
+ let tagCloseStart = config . tagCloseStart ;
237
+ let tagCloseEnd = config . tagCloseEnd ;
238
+ let openIndent = createIndent ( config , state ) ;
239
+ let closeIndent = createIndent ( config , state ) ;
240
+
241
+ if ( state . textContext ) {
242
+ tagOpenStart = defaults . tagOpenStart ;
243
+ tagOpenEnd = defaults . tagOpenEnd ;
244
+ tagCloseStart = defaults . tagCloseStart ;
245
+ tagCloseEnd = defaults . tagCloseEnd ;
246
+ openIndent = '' ;
247
+ } else if ( textElems . has ( node . name ) ) {
248
+ tagOpenEnd = defaults . tagOpenEnd ;
249
+ tagCloseStart = defaults . tagCloseStart ;
250
+ closeIndent = '' ;
251
+ state . textContext = node ;
252
+ }
253
+
254
+ const children = stringifyNode ( node , config , state ) ;
255
+
256
+ if ( state . textContext === node ) {
257
+ state . textContext = null ;
258
+ }
259
+
260
+ return (
261
+ openIndent +
262
+ tagOpenStart +
263
+ node . name +
264
+ stringifyAttributes ( node , config ) +
265
+ tagOpenEnd +
266
+ children +
267
+ closeIndent +
268
+ tagCloseStart +
269
+ node . name +
270
+ tagCloseEnd
271
+ ) ;
271
272
} ;
272
273
273
274
/**
@@ -278,14 +279,13 @@ const stringifyElement = (node, config, state) => {
278
279
const stringifyAttributes = ( node , config ) => {
279
280
let attrs = '' ;
280
281
for ( const [ name , value ] of Object . entries ( node . attributes ) ) {
281
- // TODO remove attributes without values support in v3
282
+ attrs += ' ' + name ;
283
+
282
284
if ( value !== undefined ) {
283
285
const encodedValue = value
284
286
. toString ( )
285
287
. replace ( config . regValEntities , config . encodeEntity ) ;
286
- attrs += ' ' + name + config . attrStart + encodedValue + config . attrEnd ;
287
- } else {
288
- attrs += ' ' + name ;
288
+ attrs += config . attrStart + encodedValue + config . attrEnd ;
289
289
}
290
290
}
291
291
return attrs ;
0 commit comments