/
XmlElement.ts
365 lines (331 loc) · 11.5 KB
/
XmlElement.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* Copyright (C) 2016-2019 Michael Kourlas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {getContext} from "../error";
import {IStringOptions, StringOptions} from "../options";
import {fixName, isUndefined, validateName} from "../validate";
import {
default as XmlAttribute,
IXmlAttributeOptions as IXmlAttributeOptions
} from "./XmlAttribute";
import XmlCdata, {IXmlCdataOptions} from "./XmlCdata";
import XmlCharData, {IXmlCharDataOptions} from "./XmlCharData";
import XmlCharRef, {IXmlCharRefOptions} from "./XmlCharRef";
import XmlComment, {IXmlCommentOptions} from "./XmlComment";
import XmlEntityRef, {IXmlEntityRefOptions} from "./XmlEntityRef";
import XmlProcInst, {IXmlProcInstOptions} from "./XmlProcInst";
/**
* The options used to create a new element.
*/
export interface IXmlElementOptions {
/**
* The name of the element.
*/
name: string;
/**
* Whether to replace any invalid characters in the name of the element
* with the Unicode replacement character. By default, this is disabled.
*/
replaceInvalidCharsInName?: boolean;
/**
* Whether to use a self-closing tag if this element is empty.
*
* For example, use:
* ```xml
* <element/>
* ```
* instead of:
* ```xml
* <element></element>
* ```
*
* By default, this is enabled.
*/
useSelfClosingTagIfEmpty?: boolean;
}
type Child<Parent> = XmlAttribute<XmlElement<Parent>>
| XmlCdata<XmlElement<Parent>>
| XmlCharData<XmlElement<Parent>>
| XmlCharRef<XmlElement<Parent>>
| XmlComment<XmlElement<Parent>>
| XmlElement<XmlElement<Parent>>
| XmlEntityRef<XmlElement<Parent>>
| XmlProcInst<XmlElement<Parent>>;
/**
* Represents an XML element.
*
* A sample element is structured as follows, where `{name}` is the name
* of the element:
*
* ```xml
* <{name} attname="attvalue">
* <subelem/>
* <?pitarget picontent?>
* text
* </{name}></pre>
* ```
*
* XML elements can have an unlimited number of attributes, CDATA sections,
* character references, comments, elements, entity references, processing
* instructions, and character data.
*
* An element with no content will be represented using an empty element tag:
*
* ```xml
* <{name}/>
* ```
*/
export default class XmlElement<Parent> {
private readonly _validation: boolean;
private readonly _children: Array<Child<Parent>>;
private readonly _attributeNames: string[];
private readonly _parent: Parent;
private readonly _replaceInvalidCharsInName: boolean;
private readonly _useSelfClosingTagIfEmpty: boolean;
private _name!: string;
constructor(parent: Parent, validation: boolean,
options: IXmlElementOptions)
{
this._validation = validation;
if (!isUndefined(options.replaceInvalidCharsInName)) {
this._replaceInvalidCharsInName = options.replaceInvalidCharsInName;
} else {
this._replaceInvalidCharsInName = false;
}
if (!isUndefined(options.useSelfClosingTagIfEmpty)) {
this._useSelfClosingTagIfEmpty = options.useSelfClosingTagIfEmpty;
} else {
this._useSelfClosingTagIfEmpty = true;
}
this._children = [];
this._attributeNames = [];
this._parent = parent;
this.name = options.name;
}
/**
* Gets the name of this element.
*/
public get name() {
return this._name;
}
/**
* Sets the name of this element.
*/
public set name(name: string) {
if (this._replaceInvalidCharsInName) {
name = fixName(name);
if (name.length === 0) {
throw new Error(`${getContext(this.up())}: element name should`
+ " not be empty");
}
} else if (this._validation && !validateName(name)) {
if (name.length === 0) {
throw new Error(`${getContext(this.up())}: element name should`
+ " not be empty");
} else {
throw new Error(`${getContext(this.up())}: element name`
+ ` "${name}" should not contain characters not`
+ " allowed in XML names");
}
}
this._name = name;
}
/**
* Adds an attribute to this element and returns the new attribute.
*/
public attribute(options: IXmlAttributeOptions) {
if (this._validation
&& this._attributeNames.indexOf(options.name) !== -1)
{
throw new Error(`${getContext(this.up())}: element "${this.name}"`
+ " already contains an attribute with the"
+ ` name "${options.name}"`);
}
const attribute = new XmlAttribute(this, this._validation, options);
this._children.push(attribute);
this._attributeNames.push(options.name);
return attribute;
}
/**
* Adds a CDATA section to this element and returns the new CDATA section.
*/
public cdata(options: IXmlCdataOptions) {
const cdata = new XmlCdata(this, this._validation, options);
this._children.push(cdata);
return cdata;
}
/**
* Adds character data to this element and returns the new character data.
*/
public charData(options: IXmlCharDataOptions) {
const charDataNode = new XmlCharData(this, this._validation, options);
this._children.push(charDataNode);
return charDataNode;
}
/**
* Adds a character reference to this element and returns the new
* character reference.
*/
public charRef(options: IXmlCharRefOptions) {
const charRef = new XmlCharRef(this, this._validation, options);
this._children.push(charRef);
return charRef;
}
/**
* Adds a comment to this element and returns the new comment.
*/
public comment(options: IXmlCommentOptions) {
const comment = new XmlComment(this, this._validation, options);
this._children.push(comment);
return comment;
}
/**
* Adds an element to this element and returns the new element.
*/
public element(options: IXmlElementOptions) {
const element = new XmlElement(this, this._validation, options);
this._children.push(element);
return element;
}
/**
* Adds an entity reference to this element and returns the new entity
* reference.
*/
public entityRef(options: IXmlEntityRefOptions) {
const entityRef = new XmlEntityRef(this, this._validation, options);
this._children.push(entityRef);
return entityRef;
}
/**
* Adds a processing instruction to this element and returns the new
* processing instruction.
*/
public procInst(options: IXmlProcInstOptions) {
const procInst = new XmlProcInst(this, this._validation, options);
this._children.push(procInst);
return procInst;
}
/**
* Returns an XML string representation of this element using the specified
* options.
*/
public toString(options: IStringOptions = {}) {
return this.toStringWithIndent(options, "");
}
/**
* Returns the parent of this element.
*/
public up() {
return this._parent;
}
/**
* Returns an XML string representation of this element using the specified
* options and initial indent.
*/
private toStringWithIndent(options: IStringOptions, indent: string) {
const optionsObj = new StringOptions(options);
const newIndent = indent + optionsObj.indent;
// Element tag start
let str = "<" + this._name;
// Attributes and other nodes
const nodes: Array<Child<Parent>> = [];
for (const node of this._children) {
if (node instanceof XmlAttribute) {
str += " " + node.toString(options);
} else {
nodes.push(node);
}
}
// Child nodes
if (nodes.length > 0) {
let childStr = "";
for (let i = 0; i < nodes.length; i++) {
const next = nodes[i];
let nextStr = "";
if (next instanceof XmlElement) {
nextStr += next.toStringWithIndent(
optionsObj, newIndent);
} else {
nextStr += next.toString();
}
const prev = i > 0 ? nodes[i - 1] : undefined;
// Skip empty nodes
if (next instanceof XmlCharData && next.toString() === "") {
continue;
}
// Line break before child nodes unless all nodes, or at least
// the most recent two, are of type XmlCharacterReference,
// XmlEntityReference, or XmlCharData
if (optionsObj.pretty) {
if (!this.allSameLineNodes(nodes)) {
if (!(i > 0 && this.onSameLine(next, prev))) {
nextStr = optionsObj.newline + newIndent + nextStr;
}
}
}
childStr += nextStr;
}
// Line break before end tag unless all nodes are of type
// XmlCharacterReference, XmlEntityReference, or XmlCharData
if (optionsObj.pretty) {
if (!this.allSameLineNodes(nodes)) {
childStr += optionsObj.newline + indent;
}
}
if (childStr.length === 0 && this._useSelfClosingTagIfEmpty) {
// Element empty tag end
str += "/>";
} else {
// Element start and end tags
str += ">" + childStr + "</" + this._name + ">";
}
} else if (this._useSelfClosingTagIfEmpty) {
// Element empty tag end
str += "/>";
} else {
// Element start and end tags
str += "></" + this._name + ">";
}
return str;
}
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
private allSameLineNodes(nodes: Array<Child<Parent>>) {
for (const node of nodes) {
if (!((node instanceof XmlCharRef
|| node instanceof XmlEntityRef
|| node instanceof XmlCharData)))
{
return false;
}
}
return true;
}
/**
* Returns true if the specified nodes are all character references,
* entity references, or character data.
*/
private onSameLine(prev: Child<Parent>, next?: Child<Parent>) {
return (prev instanceof XmlCharRef
|| prev instanceof XmlEntityRef
|| prev instanceof XmlCharData)
&& (!isUndefined(next)
&& (next instanceof XmlCharRef
|| next instanceof XmlEntityRef
|| next instanceof XmlCharData));
}
}