UNPKG

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