UNPKG

15.2 kBTypeScriptView Raw
1/**
2 * Copyright (C) 2016-2019 Michael Kourlas
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/**
17 * The options associated with parsing an object and formatting the resulting
18 * XML.
19 */
20export interface IOptions {
21 /**
22 * If an object or map contains a key that, when converted to a string,
23 * is equal to the value of `aliasString`, then the name of the XML element
24 * containing the object will be replaced with the value associated with
25 * said key.
26 *
27 * For example, if `aliasString` is `"="`, then the following object:
28 * ```javascript
29 * {
30 * "abc": {
31 * "=": "def"
32 * "#": "ghi"
33 * }
34 * }
35 * ```
36 * will result in the following XML for a root element named `"root"`:
37 * ```xml
38 * <root>
39 * <def>ghi</def>
40 * </root>
41 * ```
42 *
43 * The default alias string is `"="`.
44 */
45 aliasString?: string;
46 /**
47 * If an object or map contains a key that, when converted to a string,
48 * begins with the value of `attributeString`, then the value mapped by
49 * said key will be interpreted as attributes for the XML element for that
50 * object.
51 *
52 * The keys of the value of `attributeString` are interpreted as attribute
53 * names, while the values mapping to those keys are interpreted as
54 * attribute values.
55 *
56 * For example, if `attributeString` is `"@"`, then the following object:
57 * ```javascript
58 * {
59 * "abc": {
60 * "@1": {
61 * "ghi": "jkl",
62 * "mno": "pqr"
63 * },
64 * "stu": "vwx",
65 * "@2": {
66 * "yza": "bcd"
67 * },
68 * }
69 * }
70 * ```
71 * will result in the following XML for a root element named `"root"`:
72 * ```xml
73 * <root>
74 * <abc ghi='jkl' mno='pqr' yza='bcd'>
75 * <stu>vwx</stu>
76 * </abc>
77 * </root>
78 * ```
79 *
80 * The default attribute string is `"@"`.
81 */
82 attributeString?: string;
83 /**
84 * Whether to enclose any text containing the characters `<` or `&`
85 * in CDATA sections. If this is false, these characters shall be replaced
86 * with XML escape characters instead.
87 *
88 * By default, this is disabled.
89 */
90 cdataInvalidChars?: boolean;
91 /**
92 * If an object or map contains a key that, when converted to a string, is
93 * equal to an item in `cdataKeys`, then the value mapped by said key will
94 * be enclosed in a CDATA section.
95 *
96 * For example, if `cdataKeys` is:
97 * ```javascript
98 * [
99 * "abc"
100 * ]
101 * ```
102 * then the following object:
103 * ```javascript
104 * {
105 * "abc": "def&",
106 * "ghi": "jkl",
107 * "mno": "pqr<"
108 * }
109 * ```
110 * will result in the following XML for a root element named `"root"`:
111 * ```xml
112 * <root>
113 * <abc><![CDATA[def&]]></ghi>
114 * <ghi>jlk</ghi>
115 * <mno>pqr&lt;</mno>
116 * </root>
117 * ```
118 *
119 * If `cdataKeys` has a key named `"*"`, then that entry will match all
120 * keys.
121 *
122 * By default, this is an empty array.
123 */
124 cdataKeys?: string[];
125 /**
126 * The options associated with the XML declaration.
127 */
128 declaration?: IDeclarationOptions;
129 /**
130 * The options associated with the XML document type definition.
131 */
132 dtd?: IDtdOptions;
133 /**
134 * The options associated with the formatting of the XML document.
135 */
136 format?: IFormatOptions;
137 /**
138 * Whether to replace any characters that are not valid in XML in particular
139 * contexts with the Unicode replacement character, U+FFFD.
140 *
141 * At present this is limited to attribute names and values; element names
142 * and character data; CDATA sections; and comments. This may be extended
143 * in future.
144 *
145 * By default, this is disabled.
146 */
147 replaceInvalidChars?: boolean;
148 /**
149 * If a value has a type (as defined by calling `Object.prototype.toString`
150 * on the value) equal to a key in `typeHandlers`, then said value will be
151 * replaced by the return value of the function mapped to by the key in
152 * `typeHandlers`. This function is called with the value as a parameter.
153 *
154 * If one of these functions returns the sole instance of {@link Absent},
155 * then the value will be suppressed from the XML output altogether.
156 *
157 * For example, if `typeHandlers` is:
158 * ```javascript
159 * {
160 * "[object Date]": function(value) {
161 * return value.getYear();
162 * },
163 * "[object Null]": function(value) {
164 * return Absent.instance;
165 * }
166 * }
167 * ```
168 * then the following object:
169 * ```javascript
170 * {
171 * "abc": new Date(2012, 10, 31),
172 * "def": null
173 * }
174 * ```
175 * will result in the following XML for a root element named `"root"`:
176 * ```xml
177 * <root>
178 * <abc>2012</abc>
179 * </root>
180 * ```
181 *
182 * If `typeHandlers` has a key named `"*"`, then that entry will match all
183 * values, unless there is a more specific entry.
184 *
185 * Note that normal parsing still occurs for the value returned by the
186 * function; it is not directly converted to a string.
187 *
188 * The default value is an empty object.
189 */
190 typeHandlers?: ITypeHandlers;
191 /**
192 * Whether to use a self-closing tag for empty elements.
193 *
194 * For example, the following element will be used:
195 * ```xml
196 * <element/>
197 * ```
198 * instead of:
199 * ```xml
200 * <element></element>
201 * ```
202 *
203 * By default, this is enabled.
204 */
205 useSelfClosingTagIfEmpty?: boolean;
206 /**
207 * Whether to throw an exception if basic XML validation fails while
208 * building the document.
209 *
210 * By default, this is enabled.
211 */
212 validation?: boolean;
213 /**
214 * If an object or map contains a key that, when converted to a string,
215 * begins with the value of `valueString`, then the value mapped by said key
216 * will be represented as bare text within the XML element for that object.
217 *
218 * For example, if `valueString` is `"#"`, then the following object:
219 * ```javascript
220 * new Map([
221 * ["#1", "abc"],
222 * ["def", "ghi"],
223 * ["#2", "jkl"]
224 * ])
225 * ```
226 * will result in the following XML for a root element named `"root"`:
227 * ```xml
228 * <root>
229 * abc
230 * <def>ghi</def>
231 * jkl
232 * </root>
233 * ```
234 *
235 * The default value is `"#"`.
236 */
237 valueString?: string;
238 /**
239 * If an object or map contains a key that, when converted to a string, is
240 * equal to a key in `wrapHandlers`, and the key in said object or map maps
241 * to an array or set, then all items in the array or set will be wrapped
242 * in an XML element with the same name as the key.
243 *
244 * The key in `wrapHandlers` must map to a function that is called with the
245 * key name, as well as the array or set, as parameters. This function must
246 * return a string or value that can be converted to a string, which will
247 * become the name for each XML element for each item in the array or set.
248 * Alternatively, this function may return `null` to indicate that no
249 * wrapping should occur.
250 *
251 * For example, if `wrapHandlers` is:
252 * ```javascript
253 * {
254 * "abc": function(key, value) {
255 * return "def";
256 * }
257 * }
258 * ```
259 * then the following object:
260 * ```javascript
261 * {
262 * "ghi": "jkl",
263 * "mno": {
264 * "pqr": ["s", "t"]
265 * },
266 * "uvw": {
267 * "abc": ["x", "y"]
268 * }
269 * }
270 * ```
271 * will result in the following XML for a root element named `"root"`:
272 * ```xml
273 * <root>
274 * <ghi>jkl</ghi>
275 * <mno>
276 * <pqr>s</pqr>
277 * <pqr>t</pqr>
278 * </mno>
279 * <uwv>
280 * <abc>
281 * <def>x</def>
282 * <def>y</def>
283 * </abc>
284 * </uwv>
285 * </root>
286 * ```
287 *
288 * If `wrapHandlers` has a key named `"*"`, then that entry will
289 * match all arrays and sets, unless there is a more specific entry.
290 *
291 * The default value is an empty object.
292 */
293 wrapHandlers?: IWrapHandlers;
294}
295/**
296 * Implementation of the IOptions interface used to provide default values
297 * to fields.
298 *
299 * @private
300 */
301export declare class Options implements IOptions {
302 aliasString: string;
303 attributeString: string;
304 cdataInvalidChars: boolean;
305 cdataKeys: string[];
306 declaration: DeclarationOptions;
307 dtd: DtdOptions;
308 format: FormatOptions;
309 replaceInvalidChars: boolean;
310 typeHandlers: TypeHandlers;
311 useSelfClosingTagIfEmpty: boolean;
312 validation: boolean;
313 valueString: string;
314 wrapHandlers: WrapHandlers;
315 constructor(options?: IOptions);
316}
317/**
318 * The options associated with the XML declaration. An example of an XML
319 * declaration is as follows:
320 *
321 * ```xml
322 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
323 * ```
324 */
325export interface IDeclarationOptions {
326 /**
327 * Whether to include a declaration in the generated XML. By default,
328 * one is included.
329 */
330 include?: boolean;
331 /**
332 * The encoding attribute to be included in the declaration. If defined,
333 * this value must be a valid encoding. By default, no encoding attribute
334 * is included.
335 */
336 encoding?: string;
337 /**
338 * The value of the standalone attribute to be included in the declaration.
339 * If defined, this value must be "yes" or "no". By default, no standalone
340 * attribute is included.
341 */
342 standalone?: string;
343 /**
344 * The XML version to be included in the declaration. If defined, this
345 * value must be a valid XML version number. Defaults to "1.0".
346 */
347 version?: string;
348}
349/**
350 * Implementation of the IDeclarationOptions interface used to provide default
351 * values to fields.
352 *
353 * @private
354 */
355export declare class DeclarationOptions implements IDeclarationOptions {
356 include: boolean;
357 encoding?: string;
358 standalone?: string;
359 version?: string;
360 constructor(declarationOptions?: IDeclarationOptions);
361}
362/**
363 * The options associated with the XML document type definition (DTD). An
364 * example of a DTD is as follows:
365 *
366 * ```xml
367 * <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
368 * "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
369 * ```
370 */
371export interface IDtdOptions {
372 /**
373 * Whether to include a DTD in the generated XML. By default, no DTD is
374 * included.
375 */
376 include?: boolean;
377 /**
378 * The name of the DTD. This value cannot be left undefined if `include`
379 * is true.
380 */
381 name?: string;
382 /**
383 * The system identifier of the DTD, excluding quotation marks. By default,
384 * no system identifier is included.
385 */
386 sysId?: string;
387 /**
388 * The public identifier of the DTD, excluding quotation marks. If a public
389 * identifier is provided, a system identifier must be provided as well.
390 * By default, no public identifier is included.
391 */
392 pubId?: string;
393}
394/**
395 * Implementation of the IDtdOptions interface used to provide default values
396 * to fields.
397 *
398 * @private
399 */
400export declare class DtdOptions implements IDtdOptions {
401 include: boolean;
402 name?: string;
403 sysId?: string;
404 pubId?: string;
405 constructor(validation: boolean, dtdOptions?: IDtdOptions);
406}
407/**
408 * The options associated with the formatting of the XML document.
409 */
410export interface IFormatOptions {
411 /**
412 * Whether double quotes or single quotes should be used in XML attributes.
413 * By default, single quotes are used.
414 */
415 doubleQuotes?: boolean;
416 /**
417 * The indent string used for pretty-printing. The default indent string is
418 * four spaces.
419 */
420 indent?: string;
421 /**
422 * The newline string used for pretty-printing. The default newline string
423 * is "\n".
424 */
425 newline?: string;
426 /**
427 * Whether pretty-printing is enabled. By default, pretty-printing is
428 * enabled.
429 */
430 pretty?: boolean;
431}
432/**
433 * Implementation of the IFormatOptions interface used to provide default values
434 * to fields.
435 *
436 * @private
437 */
438export declare class FormatOptions implements IFormatOptions {
439 doubleQuotes?: boolean;
440 indent?: string;
441 newline?: string;
442 pretty?: boolean;
443 constructor(formatOptions?: IFormatOptions);
444}
445/**
446 * Map for the `typeHandlers` property in the {@link IOptions} interface.
447 */
448export interface ITypeHandlers {
449 /**
450 * Mapping between the type of a value in an object to a function taking
451 * this value and returning a replacement value.
452 */
453 [type: string]: (value: any) => unknown;
454}
455/**
456 * Implementation of the ITypeHandlers interface used to provide default values
457 * to fields.
458 *
459 * @private
460 */
461export declare class TypeHandlers implements ITypeHandlers {
462 [type: string]: (value: any) => unknown;
463 constructor(typeHandlers?: ITypeHandlers);
464}
465/**
466 * Map for the `wrapHandlers` property in the {@link IOptions} interface.
467 */
468export interface IWrapHandlers {
469 /**
470 * Mapping between the string version of a key in an object or map with a
471 * value that is an array or set to a function taking the string version
472 * of that key, as well as that array or set.
473 *
474 * This function returns either a string that will become the name for each
475 * XML element for each item in the array or set, or `null` to indicate that
476 * wrapping should not occur.
477 */
478 [key: string]: (key: string, value: any) => string | null;
479}
480/**
481 * Implementation of the IWrapHandlers interface used to provide default values
482 * to fields.
483 *
484 * @private
485 */
486export declare class WrapHandlers implements IWrapHandlers {
487 [key: string]: (key: string, value: any) => string | null;
488 constructor(wrapHandlers?: IWrapHandlers);
489}