UNPKG

5.08 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 */
16import { IStringOptions } from "../options";
17import { default as XmlAttribute, IXmlAttributeOptions as IXmlAttributeOptions } from "./XmlAttribute";
18import XmlCdata, { IXmlCdataOptions } from "./XmlCdata";
19import XmlCharData, { IXmlCharDataOptions } from "./XmlCharData";
20import XmlCharRef, { IXmlCharRefOptions } from "./XmlCharRef";
21import XmlComment, { IXmlCommentOptions } from "./XmlComment";
22import XmlEntityRef, { IXmlEntityRefOptions } from "./XmlEntityRef";
23import XmlProcInst, { IXmlProcInstOptions } from "./XmlProcInst";
24/**
25 * The options used to create a new element.
26 */
27export interface IXmlElementOptions {
28 /**
29 * The name of the element.
30 */
31 name: string;
32 /**
33 * Whether to replace any invalid characters in the name of the element
34 * with the Unicode replacement character. By default, this is disabled.
35 */
36 replaceInvalidCharsInName?: boolean;
37 /**
38 * Whether to use a self-closing tag if this element is empty.
39 *
40 * For example, use:
41 * ```xml
42 * <element/>
43 * ```
44 * instead of:
45 * ```xml
46 * <element></element>
47 * ```
48 *
49 * By default, this is enabled.
50 */
51 useSelfClosingTagIfEmpty?: boolean;
52}
53/**
54 * Represents an XML element.
55 *
56 * A sample element is structured as follows, where `{name}` is the name
57 * of the element:
58 *
59 * ```xml
60 * <{name} attname="attvalue">
61 * <subelem/>
62 * <?pitarget picontent?>
63 * text
64 * </{name}></pre>
65 * ```
66 *
67 * XML elements can have an unlimited number of attributes, CDATA sections,
68 * character references, comments, elements, entity references, processing
69 * instructions, and character data.
70 *
71 * An element with no content will be represented using an empty element tag:
72 *
73 * ```xml
74 * <{name}/>
75 * ```
76 */
77export default class XmlElement<Parent> {
78 private readonly _validation;
79 private readonly _children;
80 private readonly _attributeNames;
81 private readonly _parent;
82 private readonly _replaceInvalidCharsInName;
83 private readonly _useSelfClosingTagIfEmpty;
84 private _name;
85 constructor(parent: Parent, validation: boolean, options: IXmlElementOptions);
86 /**
87 * Gets the name of this element.
88 */
89 get name(): string;
90 /**
91 * Sets the name of this element.
92 */
93 set name(name: string);
94 /**
95 * Adds an attribute to this element and returns the new attribute.
96 */
97 attribute(options: IXmlAttributeOptions): XmlAttribute<this>;
98 /**
99 * Adds a CDATA section to this element and returns the new CDATA section.
100 */
101 cdata(options: IXmlCdataOptions): XmlCdata<this>;
102 /**
103 * Adds character data to this element and returns the new character data.
104 */
105 charData(options: IXmlCharDataOptions): XmlCharData<this>;
106 /**
107 * Adds a character reference to this element and returns the new
108 * character reference.
109 */
110 charRef(options: IXmlCharRefOptions): XmlCharRef<this>;
111 /**
112 * Adds a comment to this element and returns the new comment.
113 */
114 comment(options: IXmlCommentOptions): XmlComment<this>;
115 /**
116 * Adds an element to this element and returns the new element.
117 */
118 element(options: IXmlElementOptions): XmlElement<this>;
119 /**
120 * Adds an entity reference to this element and returns the new entity
121 * reference.
122 */
123 entityRef(options: IXmlEntityRefOptions): XmlEntityRef<this>;
124 /**
125 * Adds a processing instruction to this element and returns the new
126 * processing instruction.
127 */
128 procInst(options: IXmlProcInstOptions): XmlProcInst<this>;
129 /**
130 * Returns an XML string representation of this element using the specified
131 * options.
132 */
133 toString(options?: IStringOptions): string;
134 /**
135 * Returns the parent of this element.
136 */
137 up(): Parent;
138 /**
139 * Returns an XML string representation of this element using the specified
140 * options and initial indent.
141 */
142 private toStringWithIndent;
143 /**
144 * Returns true if the specified nodes are all character references,
145 * entity references, or character data.
146 */
147 private allSameLineNodes;
148 /**
149 * Returns true if the specified nodes are all character references,
150 * entity references, or character data.
151 */
152 private onSameLine;
153}