-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathto-xml.js
249 lines (202 loc) · 5.58 KB
/
to-xml.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* The toXML() method converts a JavaScript value to an XML string.
*
* @function toXML
* @param value {Object} The value to convert to an XML string.
* @param [replacer] {Function} A function that alters the behavior
* of the stringification process.
* @param [space] {Number|String} A String or Number object that's
* used to insert white space into the output XML string for
* readability purposes. If this is a Number, it indicates the number
* of space characters to use as white space.
* If this is a String, the string is used as white space.
* @returns {String}
*/
/* exported toXML */
var toXML = (function(exports) {
const TYPES = {
"boolean": fromString,
"number": fromString,
"object": fromObject,
"string": fromString
};
const ESCAPE = {
"\t": "	",
"\n": "
",
"\r": "
",
" ": " ",
"&": "&",
"<": "<",
">": ">",
'"': """
};
const ATTRIBUTE_KEY = "@";
const CHILD_NODE_KEY = "#";
const LF = "\n";
const isArray = Array.isArray || _isArray;
const REPLACE = String.prototype.replace;
return (exports.toXML = _toXML);
function _toXML(value, replacer, space) {
const job = createJob(replacer, space);
fromAny(job, "", value);
return job.r;
}
function createJob(replacer, space) {
const job = {
f: replacer, // replacer function
// s: "", // indent string
// i: 0, // indent string length
l: "", // current indent string
r: "" // result string
};
if (space) {
let str = "";
if (space > 0) {
for (let i = space; i; i--) {
str += " ";
}
} else {
str += space; // stringify
}
job.s = str;
// indent string length
job.i = str.length;
}
return job;
}
function fromAny(job, key, value) {
// child node synonym
if (key === CHILD_NODE_KEY) key = "";
if (_isArray(value)) return fromArray(job, key, value);
const replacer = job.f;
if (replacer) value = replacer(key, value);
const f = TYPES[typeof value];
if (f) f(job, key, value);
}
function fromString(job, key, value) {
if (key === "?") {
// XML declaration
value = "<?" + value + "?>";
} else if (key === "!") {
// comment, CDATA section
value = "<!" + value + ">";
} else {
value = escapeTextNode(value);
if (key) {
// text element without attributes
value = "<" + key + ">" + value + "</" + key + ">";
}
}
if (key && job.i && job.r) {
job.r += LF + job.l; // indent
}
job.r += value;
}
function fromArray(job, key, value) {
Array.prototype.forEach.call(value, function(value) {
fromAny(job, key, value);
});
}
function fromObject(job, key, value) {
// empty tag
const hasTag = !!key;
const closeTag = (value === null);
if (closeTag) {
if (!hasTag) return;
value = {};
}
const keys = Object.keys(value);
const keyLength = keys.length;
const attrs = keys.filter(isAttribute);
const attrLength = attrs.length;
const hasIndent = job.i;
const curIndent = job.l;
let willIndent = hasTag && hasIndent;
let didIndent;
// open tag
if (hasTag) {
if (hasIndent && job.r) {
job.r += LF + curIndent;
}
job.r += '<' + key;
// attributes
attrs.forEach(function(name) {
writeAttributes(job, name.substr(1), value[name]);
});
// empty element
const isEmpty = closeTag || (attrLength && keyLength === attrLength);
if (isEmpty) {
const firstChar = key[0];
if (firstChar !== "!" && firstChar !== "?") {
job.r += "/";
}
}
job.r += '>';
if (isEmpty) return;
}
keys.forEach(function(name) {
// skip attribute
if (isAttribute(name)) return;
// indent when it has child node but not fragment
if (willIndent && ((name && name !== CHILD_NODE_KEY) || isArray(value[name]))) {
job.l += job.s; // increase indent level
willIndent = 0;
didIndent = 1;
}
// child node or text node
fromAny(job, name, value[name]);
});
if (didIndent) {
// decrease indent level
job.l = job.l.substr(job.i);
job.r += LF + job.l;
}
// close tag
if (hasTag) {
job.r += '</' + key + '>';
}
}
function writeAttributes(job, key, val) {
if (isArray(val)) {
val.forEach(function(child) {
writeAttributes(job, key, child);
});
} else if (!key && "object" === typeof val) {
Object.keys(val).forEach(function(name) {
writeAttributes(job, name, val[name]);
});
} else {
writeAttribute(job, key, val);
}
}
function writeAttribute(job, key, val) {
const replacer = job.f;
if (replacer) val = replacer(ATTRIBUTE_KEY + key, val);
if ("undefined" === typeof val) return;
// empty attribute name
if (!key) {
job.r += ' ' + val;
return;
}
// attribute name
job.r += ' ' + key;
// property attribute
if (val === null) return;
job.r += '="' + escapeAttribute(val) + '"';
}
function isAttribute(name) {
return name && name[0] === ATTRIBUTE_KEY;
}
function escapeTextNode(str) {
return REPLACE.call(str, /(^\s|[&<>]|\s$)/g, escapeRef);
}
function escapeAttribute(str) {
return REPLACE.call(str, /([&"])/g, escapeRef);
}
function escapeRef(str) {
return ESCAPE[str] || str;
}
function _isArray(array) {
return array instanceof Array;
}
})(typeof exports === 'object' && exports || {});