XmlNode.js
7.01 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../utils");
/**
* Represents an XML node.
*
* This class is the root class of the XML node hierarchy. It should not be
* directly instantiated; one of its subclasses should be used instead.
*
* @protected
*/
var XmlNode = (function () {
/**
* Initializes a new instance of the {@link XmlNode} class.
*/
function XmlNode() {
this._parent = undefined;
this._children = [];
}
Object.defineProperty(XmlNode.prototype, "parent", {
/**
* Gets the parent of this node.
*
* @returns The parent of this node.
*/
get: function () {
return this._parent;
},
enumerable: true,
configurable: true
});
/**
* Gets this node's children.
*
* Throws an exception if this node cannot have any children. Consult the
* appropriate subclass documentation for more details.
*
* @returns This node's children.
*/
XmlNode.prototype.children = function () {
return this._children.slice();
};
/**
* Inserts the specified node into this node's children at the specified
* index. The node is not inserted if it is already present. If this node
* already has a parent, it is removed from that parent.
*
* Throws an exception if this node cannot have any children, or if the
* specified node cannot be added at the specified index. Consult the
* appropriate subclass documentation for more details.
*
* @param node The node to insert.
* @param index The index at which to insert the node. Nodes at or after
* the index are shifted to the right. If no index is
* specified, the node is inserted at the end.
*
* @returns The node inserted into this node's children, or undefined if no
* node was inserted.
*/
XmlNode.prototype.insertChild = function (node, index) {
if (index === void 0) { index = this._children.length; }
if (!(node instanceof XmlNode)) {
throw new TypeError("node should be an instance of XmlNode");
}
else if (!utils_1.isNumber(index) || !utils_1.isInteger(index)) {
throw new TypeError("index should be an integer");
}
else if (index < 0 || index > this._children.length) {
throw new RangeError("index should respect children array bounds");
}
if (this._children.indexOf(node) === -1) {
if (!utils_1.isUndefined(node.parent)) {
node.parent.removeChild(node);
}
node._parent = this;
this._children.splice(index, 0, node);
return node;
}
return undefined;
};
/**
* Gets the node that follows this one, or undefined if no such node
* exists or if this node has no parent.
*
* @returns The node that follows this one, or undefined if no such node
* exists or if this node has no parent.
*/
XmlNode.prototype.next = function () {
if (utils_1.isUndefined(this.parent)) {
return undefined;
}
else if (this.parent._children.indexOf(this)
=== this.parent._children.length - 1) {
return undefined;
}
return this.parent._children[this.parent._children.indexOf(this) + 1];
};
/**
* Gets the node that is previous to this one, or undefined if no such node
* exists or if this node has no parent.
*
* @returns The node that is previous to this one, or undefined if no such
* node exists or if this node has no parent.
*/
XmlNode.prototype.prev = function () {
if (utils_1.isUndefined(this.parent)) {
return undefined;
}
else if (this.parent._children.indexOf(this) === 0) {
return undefined;
}
return this.parent._children[this.parent._children.indexOf(this) - 1];
};
/**
* Removes this node from its parent if this node has a parent.
*
* @returns This node's parent, or undefined if it has no parent.
*/
XmlNode.prototype.remove = function () {
if (!utils_1.isUndefined(this.parent)) {
var parent_1 = this.parent;
this.parent.removeChild(this);
return parent_1;
}
return undefined;
};
/**
* Removes the specified node from this node's children.
*
* Throws an exception if this node cannot have any children, or if the
* specified node cannot be removed. Consult the appropriate subclass
* documentation for more details.
*
* @param node The node to remove.
*
* @returns Whether a node was removed.
*/
XmlNode.prototype.removeChild = function (node) {
if (!(node instanceof XmlNode)) {
throw new Error("node should be an instance of XmlNode");
}
var index = this._children.indexOf(node);
if (index !== -1) {
node._parent = undefined;
this._children.splice(index, 1);
return true;
}
return false;
};
/**
* Removes the node at the specified index from this node's children.
*
* Throws an exception if this node cannot have any children, or if the
* node at the specified index cannot be removed. Consult the appropriate
* subclass documentation for more details.
*
* @param index The index at which the node to be removed is located.
*
* @returns The node that was removed.
*/
XmlNode.prototype.removeChildAtIndex = function (index) {
if (!utils_1.isNumber(index) || !utils_1.isInteger(index)) {
throw new TypeError("index should be a number");
}
else if (index < 0 || index >= this._children.length) {
throw new RangeError("index should respect children array bounds");
}
var node = this._children[index];
node._parent = undefined;
this._children.splice(index, 1);
return node;
};
/**
* Returns an XML string representation of this node.
*
* @param options Formatting options for the string representation.
*
* @returns An XML string representation of this node.
*/
XmlNode.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
throw new Error("toString not implemented for XmlNode");
};
/**
* Returns the root node of the current hierarchy. If this node has no
* parent, this node itself is returned.
*
* @returns The root node of the current hierarchy.
*/
XmlNode.prototype.top = function () {
if (utils_1.isUndefined(this.parent)) {
return this;
}
else {
return this.parent.top();
}
};
/**
* Gets the parent of this node.
*/
XmlNode.prototype.up = function () {
return this.parent;
};
return XmlNode;
}());
exports.default = XmlNode;