Skip to content

Commit 18aae2f

Browse files
committed
chore: refactor stringifier.js
1 parent 8c593fa commit 18aae2f

File tree

2 files changed

+76
-75
lines changed

2 files changed

+76
-75
lines changed

lib/path.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ const isDigit = (c) => {
7272
const readNumber = (string, cursor) => {
7373
let i = cursor;
7474
let value = '';
75-
let state = /** @type {ReadNumberState} */ ('none');
75+
/** @type {ReadNumberState} */
76+
let state = 'none';
7677
for (; i < string.length; i += 1) {
7778
const c = string[i];
7879
if (c === '+' || c === '-') {

lib/stringifier.js

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { textElems } from '../plugins/_collections.js';
22

33
/**
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
68
* @typedef {import('./types.js').XastElement} XastElement
79
* @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
912
* @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
1313
* @typedef {Required<StringifyOptions>} Options
1414
*
1515
* @typedef State
@@ -114,28 +114,29 @@ export const stringifySvg = (data, userOptions = {}) => {
114114
*/
115115
const stringifyNode = (data, config, state) => {
116116
let svg = '';
117-
state.indentLevel += 1;
117+
state.indentLevel++;
118118
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);
136137
}
137138
}
138-
state.indentLevel -= 1;
139+
state.indentLevel--;
139140
return svg;
140141
};
141142

@@ -215,59 +216,59 @@ const stringifyElement = (node, config, state) => {
215216
stringifyAttributes(node, config) +
216217
config.tagShortEnd
217218
);
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;
256219
}
257220

258221
return (
259-
openIndent +
260-
tagOpenStart +
222+
createIndent(config, state) +
223+
config.tagShortStart +
261224
node.name +
262225
stringifyAttributes(node, config) +
263-
tagOpenEnd +
264-
children +
265-
closeIndent +
266-
tagCloseStart +
226+
config.tagOpenEnd +
227+
config.tagCloseStart +
267228
node.name +
268-
tagCloseEnd
229+
config.tagCloseEnd
269230
);
270231
}
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+
);
271272
};
272273

273274
/**
@@ -278,14 +279,13 @@ const stringifyElement = (node, config, state) => {
278279
const stringifyAttributes = (node, config) => {
279280
let attrs = '';
280281
for (const [name, value] of Object.entries(node.attributes)) {
281-
// TODO remove attributes without values support in v3
282+
attrs += ' ' + name;
283+
282284
if (value !== undefined) {
283285
const encodedValue = value
284286
.toString()
285287
.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;
289289
}
290290
}
291291
return attrs;

0 commit comments

Comments
 (0)