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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// Generated by CoffeeScript 2.4.1
(function() {
// Converts values to strings
var XMLStringifier,
hasProp = {}.hasOwnProperty;
module.exports = XMLStringifier = (function() {
class XMLStringifier {
// Initializes a new instance of `XMLStringifier`
// `options.version` The version number string of the XML spec to validate against, e.g. 1.0
// `options.noDoubleEncoding` whether existing html entities are encoded: true or false
// `options.stringify` a set of functions to use for converting values to strings
// `options.noValidation` whether values will be validated and escaped or returned as is
// `options.invalidCharReplacement` a character to replace invalid characters and disable character validation
constructor(options) {
var key, ref, value;
// Checks whether the given string contains legal characters
// Fails with an exception on error
// `str` the string to check
this.assertLegalChar = this.assertLegalChar.bind(this);
// Checks whether the given string contains legal characters for a name
// Fails with an exception on error
// `str` the string to check
this.assertLegalName = this.assertLegalName.bind(this);
options || (options = {});
this.options = options;
if (!this.options.version) {
this.options.version = '1.0';
}
ref = options.stringify || {};
for (key in ref) {
if (!hasProp.call(ref, key)) continue;
value = ref[key];
this[key] = value;
}
}
// Defaults
name(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalName('' + val || '');
}
text(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar(this.textEscape('' + val || ''));
}
cdata(val) {
if (this.options.noValidation) {
return val;
}
val = '' + val || '';
val = val.replace(']]>', ']]]]><![CDATA[>');
return this.assertLegalChar(val);
}
comment(val) {
if (this.options.noValidation) {
return val;
}
val = '' + val || '';
if (val.match(/--/)) {
throw new Error("Comment text cannot contain double-hypen: " + val);
}
return this.assertLegalChar(val);
}
raw(val) {
if (this.options.noValidation) {
return val;
}
return '' + val || '';
}
attValue(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar(this.attEscape(val = '' + val || ''));
}
insTarget(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
insValue(val) {
if (this.options.noValidation) {
return val;
}
val = '' + val || '';
if (val.match(/\?>/)) {
throw new Error("Invalid processing instruction value: " + val);
}
return this.assertLegalChar(val);
}
xmlVersion(val) {
if (this.options.noValidation) {
return val;
}
val = '' + val || '';
if (!val.match(/1\.[0-9]+/)) {
throw new Error("Invalid version number: " + val);
}
return val;
}
xmlEncoding(val) {
if (this.options.noValidation) {
return val;
}
val = '' + val || '';
if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-])*$/)) {
throw new Error("Invalid encoding: " + val);
}
return this.assertLegalChar(val);
}
xmlStandalone(val) {
if (this.options.noValidation) {
return val;
}
if (val) {
return "yes";
} else {
return "no";
}
}
dtdPubID(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdSysID(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdElementValue(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdAttType(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdAttDefault(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdEntityValue(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
dtdNData(val) {
if (this.options.noValidation) {
return val;
}
return this.assertLegalChar('' + val || '');
}
assertLegalChar(str) {
var regex, res;
if (this.options.noValidation) {
return str;
}
if (this.options.version === '1.0') {
// Valid characters from https://www.w3.org/TR/xml/#charsets
// any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// This ES5 compatible Regexp has been generated using the "regenerate" NPM module:
// let xml_10_InvalidChars = regenerate()
// .addRange(0x0000, 0x0008)
// .add(0x000B, 0x000C)
// .addRange(0x000E, 0x001F)
// .addRange(0xD800, 0xDFFF)
// .addRange(0xFFFE, 0xFFFF)
regex = /[\0-\x08\x0B\f\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g;
if (this.options.invalidCharReplacement !== void 0) {
str = str.replace(regex, this.options.invalidCharReplacement);
} else if (res = str.match(regex)) {
throw new Error(`Invalid character in string: ${str} at index ${res.index}`);
}
} else if (this.options.version === '1.1') {
// Valid characters from https://www.w3.org/TR/xml11/#charsets
// any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
// [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
// This ES5 compatible Regexp has been generated using the "regenerate" NPM module:
// let xml_11_InvalidChars = regenerate()
// .add(0x0000)
// .addRange(0xD800, 0xDFFF)
// .addRange(0xFFFE, 0xFFFF)
regex = /[\0\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g;
if (this.options.invalidCharReplacement !== void 0) {
str = str.replace(regex, this.options.invalidCharReplacement);
} else if (res = str.match(regex)) {
throw new Error(`Invalid character in string: ${str} at index ${res.index}`);
}
}
return str;
}
assertLegalName(str) {
var regex;
if (this.options.noValidation) {
return str;
}
str = this.assertLegalChar(str);
regex = /^([:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])([\x2D\.0-:A-Z_a-z\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])*$/;
if (!str.match(regex)) {
throw new Error(`Invalid character in name: ${str}`);
}
return str;
}
// Escapes special characters in text
// See http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
// `str` the string to escape
textEscape(str) {
var ampregex;
if (this.options.noValidation) {
return str;
}
ampregex = this.options.noDoubleEncoding ? /(?!&(lt|gt|amp|apos|quot);)&/g : /&/g;
return str.replace(ampregex, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\r/g, '
');
}
// Escapes special characters in attribute values
// See http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
// `str` the string to escape
attEscape(str) {
var ampregex;
if (this.options.noValidation) {
return str;
}
ampregex = this.options.noDoubleEncoding ? /(?!&(lt|gt|amp|apos|quot);)&/g : /&/g;
return str.replace(ampregex, '&').replace(/</g, '<').replace(/"/g, '"').replace(/\t/g, '	').replace(/\n/g, '
').replace(/\r/g, '
');
}
};
// strings to match while converting from JS objects
XMLStringifier.prototype.convertAttKey = '@';
XMLStringifier.prototype.convertPIKey = '?';
XMLStringifier.prototype.convertTextKey = '#text';
XMLStringifier.prototype.convertCDataKey = '#cdata';
XMLStringifier.prototype.convertCommentKey = '#comment';
XMLStringifier.prototype.convertRawKey = '#raw';
return XMLStringifier;
}).call(this);
}).call(this);
|