1 | "use strict";
|
---|
2 | var __extends = (this && this.__extends) || (function () {
|
---|
3 | var extendStatics = function (d, b) {
|
---|
4 | extendStatics = Object.setPrototypeOf ||
|
---|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
---|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
---|
7 | return extendStatics(d, b);
|
---|
8 | };
|
---|
9 | return function (d, b) {
|
---|
10 | if (typeof b !== "function" && b !== null)
|
---|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
---|
12 | extendStatics(d, b);
|
---|
13 | function __() { this.constructor = d; }
|
---|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
15 | };
|
---|
16 | })();
|
---|
17 | var __assign = (this && this.__assign) || function () {
|
---|
18 | __assign = Object.assign || function(t) {
|
---|
19 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
---|
20 | s = arguments[i];
|
---|
21 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
---|
22 | t[p] = s[p];
|
---|
23 | }
|
---|
24 | return t;
|
---|
25 | };
|
---|
26 | return __assign.apply(this, arguments);
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.cloneNode = exports.hasChildren = exports.isDocument = exports.isDirective = exports.isComment = exports.isText = exports.isCDATA = exports.isTag = exports.Element = exports.Document = exports.NodeWithChildren = exports.ProcessingInstruction = exports.Comment = exports.Text = exports.DataNode = exports.Node = void 0;
|
---|
30 | var domelementtype_1 = require("domelementtype");
|
---|
31 | var nodeTypes = new Map([
|
---|
32 | [domelementtype_1.ElementType.Tag, 1],
|
---|
33 | [domelementtype_1.ElementType.Script, 1],
|
---|
34 | [domelementtype_1.ElementType.Style, 1],
|
---|
35 | [domelementtype_1.ElementType.Directive, 1],
|
---|
36 | [domelementtype_1.ElementType.Text, 3],
|
---|
37 | [domelementtype_1.ElementType.CDATA, 4],
|
---|
38 | [domelementtype_1.ElementType.Comment, 8],
|
---|
39 | [domelementtype_1.ElementType.Root, 9],
|
---|
40 | ]);
|
---|
41 | /**
|
---|
42 | * This object will be used as the prototype for Nodes when creating a
|
---|
43 | * DOM-Level-1-compliant structure.
|
---|
44 | */
|
---|
45 | var Node = /** @class */ (function () {
|
---|
46 | /**
|
---|
47 | *
|
---|
48 | * @param type The type of the node.
|
---|
49 | */
|
---|
50 | function Node(type) {
|
---|
51 | this.type = type;
|
---|
52 | /** Parent of the node */
|
---|
53 | this.parent = null;
|
---|
54 | /** Previous sibling */
|
---|
55 | this.prev = null;
|
---|
56 | /** Next sibling */
|
---|
57 | this.next = null;
|
---|
58 | /** The start index of the node. Requires `withStartIndices` on the handler to be `true. */
|
---|
59 | this.startIndex = null;
|
---|
60 | /** The end index of the node. Requires `withEndIndices` on the handler to be `true. */
|
---|
61 | this.endIndex = null;
|
---|
62 | }
|
---|
63 | Object.defineProperty(Node.prototype, "nodeType", {
|
---|
64 | // Read-only aliases
|
---|
65 | /**
|
---|
66 | * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible
|
---|
67 | * node {@link type}.
|
---|
68 | */
|
---|
69 | get: function () {
|
---|
70 | var _a;
|
---|
71 | return (_a = nodeTypes.get(this.type)) !== null && _a !== void 0 ? _a : 1;
|
---|
72 | },
|
---|
73 | enumerable: false,
|
---|
74 | configurable: true
|
---|
75 | });
|
---|
76 | Object.defineProperty(Node.prototype, "parentNode", {
|
---|
77 | // Read-write aliases for properties
|
---|
78 | /**
|
---|
79 | * Same as {@link parent}.
|
---|
80 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
81 | */
|
---|
82 | get: function () {
|
---|
83 | return this.parent;
|
---|
84 | },
|
---|
85 | set: function (parent) {
|
---|
86 | this.parent = parent;
|
---|
87 | },
|
---|
88 | enumerable: false,
|
---|
89 | configurable: true
|
---|
90 | });
|
---|
91 | Object.defineProperty(Node.prototype, "previousSibling", {
|
---|
92 | /**
|
---|
93 | * Same as {@link prev}.
|
---|
94 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
95 | */
|
---|
96 | get: function () {
|
---|
97 | return this.prev;
|
---|
98 | },
|
---|
99 | set: function (prev) {
|
---|
100 | this.prev = prev;
|
---|
101 | },
|
---|
102 | enumerable: false,
|
---|
103 | configurable: true
|
---|
104 | });
|
---|
105 | Object.defineProperty(Node.prototype, "nextSibling", {
|
---|
106 | /**
|
---|
107 | * Same as {@link next}.
|
---|
108 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
109 | */
|
---|
110 | get: function () {
|
---|
111 | return this.next;
|
---|
112 | },
|
---|
113 | set: function (next) {
|
---|
114 | this.next = next;
|
---|
115 | },
|
---|
116 | enumerable: false,
|
---|
117 | configurable: true
|
---|
118 | });
|
---|
119 | /**
|
---|
120 | * Clone this node, and optionally its children.
|
---|
121 | *
|
---|
122 | * @param recursive Clone child nodes as well.
|
---|
123 | * @returns A clone of the node.
|
---|
124 | */
|
---|
125 | Node.prototype.cloneNode = function (recursive) {
|
---|
126 | if (recursive === void 0) { recursive = false; }
|
---|
127 | return cloneNode(this, recursive);
|
---|
128 | };
|
---|
129 | return Node;
|
---|
130 | }());
|
---|
131 | exports.Node = Node;
|
---|
132 | /**
|
---|
133 | * A node that contains some data.
|
---|
134 | */
|
---|
135 | var DataNode = /** @class */ (function (_super) {
|
---|
136 | __extends(DataNode, _super);
|
---|
137 | /**
|
---|
138 | * @param type The type of the node
|
---|
139 | * @param data The content of the data node
|
---|
140 | */
|
---|
141 | function DataNode(type, data) {
|
---|
142 | var _this = _super.call(this, type) || this;
|
---|
143 | _this.data = data;
|
---|
144 | return _this;
|
---|
145 | }
|
---|
146 | Object.defineProperty(DataNode.prototype, "nodeValue", {
|
---|
147 | /**
|
---|
148 | * Same as {@link data}.
|
---|
149 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
150 | */
|
---|
151 | get: function () {
|
---|
152 | return this.data;
|
---|
153 | },
|
---|
154 | set: function (data) {
|
---|
155 | this.data = data;
|
---|
156 | },
|
---|
157 | enumerable: false,
|
---|
158 | configurable: true
|
---|
159 | });
|
---|
160 | return DataNode;
|
---|
161 | }(Node));
|
---|
162 | exports.DataNode = DataNode;
|
---|
163 | /**
|
---|
164 | * Text within the document.
|
---|
165 | */
|
---|
166 | var Text = /** @class */ (function (_super) {
|
---|
167 | __extends(Text, _super);
|
---|
168 | function Text(data) {
|
---|
169 | return _super.call(this, domelementtype_1.ElementType.Text, data) || this;
|
---|
170 | }
|
---|
171 | return Text;
|
---|
172 | }(DataNode));
|
---|
173 | exports.Text = Text;
|
---|
174 | /**
|
---|
175 | * Comments within the document.
|
---|
176 | */
|
---|
177 | var Comment = /** @class */ (function (_super) {
|
---|
178 | __extends(Comment, _super);
|
---|
179 | function Comment(data) {
|
---|
180 | return _super.call(this, domelementtype_1.ElementType.Comment, data) || this;
|
---|
181 | }
|
---|
182 | return Comment;
|
---|
183 | }(DataNode));
|
---|
184 | exports.Comment = Comment;
|
---|
185 | /**
|
---|
186 | * Processing instructions, including doc types.
|
---|
187 | */
|
---|
188 | var ProcessingInstruction = /** @class */ (function (_super) {
|
---|
189 | __extends(ProcessingInstruction, _super);
|
---|
190 | function ProcessingInstruction(name, data) {
|
---|
191 | var _this = _super.call(this, domelementtype_1.ElementType.Directive, data) || this;
|
---|
192 | _this.name = name;
|
---|
193 | return _this;
|
---|
194 | }
|
---|
195 | return ProcessingInstruction;
|
---|
196 | }(DataNode));
|
---|
197 | exports.ProcessingInstruction = ProcessingInstruction;
|
---|
198 | /**
|
---|
199 | * A `Node` that can have children.
|
---|
200 | */
|
---|
201 | var NodeWithChildren = /** @class */ (function (_super) {
|
---|
202 | __extends(NodeWithChildren, _super);
|
---|
203 | /**
|
---|
204 | * @param type Type of the node.
|
---|
205 | * @param children Children of the node. Only certain node types can have children.
|
---|
206 | */
|
---|
207 | function NodeWithChildren(type, children) {
|
---|
208 | var _this = _super.call(this, type) || this;
|
---|
209 | _this.children = children;
|
---|
210 | return _this;
|
---|
211 | }
|
---|
212 | Object.defineProperty(NodeWithChildren.prototype, "firstChild", {
|
---|
213 | // Aliases
|
---|
214 | /** First child of the node. */
|
---|
215 | get: function () {
|
---|
216 | var _a;
|
---|
217 | return (_a = this.children[0]) !== null && _a !== void 0 ? _a : null;
|
---|
218 | },
|
---|
219 | enumerable: false,
|
---|
220 | configurable: true
|
---|
221 | });
|
---|
222 | Object.defineProperty(NodeWithChildren.prototype, "lastChild", {
|
---|
223 | /** Last child of the node. */
|
---|
224 | get: function () {
|
---|
225 | return this.children.length > 0
|
---|
226 | ? this.children[this.children.length - 1]
|
---|
227 | : null;
|
---|
228 | },
|
---|
229 | enumerable: false,
|
---|
230 | configurable: true
|
---|
231 | });
|
---|
232 | Object.defineProperty(NodeWithChildren.prototype, "childNodes", {
|
---|
233 | /**
|
---|
234 | * Same as {@link children}.
|
---|
235 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
236 | */
|
---|
237 | get: function () {
|
---|
238 | return this.children;
|
---|
239 | },
|
---|
240 | set: function (children) {
|
---|
241 | this.children = children;
|
---|
242 | },
|
---|
243 | enumerable: false,
|
---|
244 | configurable: true
|
---|
245 | });
|
---|
246 | return NodeWithChildren;
|
---|
247 | }(Node));
|
---|
248 | exports.NodeWithChildren = NodeWithChildren;
|
---|
249 | /**
|
---|
250 | * The root node of the document.
|
---|
251 | */
|
---|
252 | var Document = /** @class */ (function (_super) {
|
---|
253 | __extends(Document, _super);
|
---|
254 | function Document(children) {
|
---|
255 | return _super.call(this, domelementtype_1.ElementType.Root, children) || this;
|
---|
256 | }
|
---|
257 | return Document;
|
---|
258 | }(NodeWithChildren));
|
---|
259 | exports.Document = Document;
|
---|
260 | /**
|
---|
261 | * An element within the DOM.
|
---|
262 | */
|
---|
263 | var Element = /** @class */ (function (_super) {
|
---|
264 | __extends(Element, _super);
|
---|
265 | /**
|
---|
266 | * @param name Name of the tag, eg. `div`, `span`.
|
---|
267 | * @param attribs Object mapping attribute names to attribute values.
|
---|
268 | * @param children Children of the node.
|
---|
269 | */
|
---|
270 | function Element(name, attribs, children, type) {
|
---|
271 | if (children === void 0) { children = []; }
|
---|
272 | if (type === void 0) { type = name === "script"
|
---|
273 | ? domelementtype_1.ElementType.Script
|
---|
274 | : name === "style"
|
---|
275 | ? domelementtype_1.ElementType.Style
|
---|
276 | : domelementtype_1.ElementType.Tag; }
|
---|
277 | var _this = _super.call(this, type, children) || this;
|
---|
278 | _this.name = name;
|
---|
279 | _this.attribs = attribs;
|
---|
280 | return _this;
|
---|
281 | }
|
---|
282 | Object.defineProperty(Element.prototype, "tagName", {
|
---|
283 | // DOM Level 1 aliases
|
---|
284 | /**
|
---|
285 | * Same as {@link name}.
|
---|
286 | * [DOM spec](https://dom.spec.whatwg.org)-compatible alias.
|
---|
287 | */
|
---|
288 | get: function () {
|
---|
289 | return this.name;
|
---|
290 | },
|
---|
291 | set: function (name) {
|
---|
292 | this.name = name;
|
---|
293 | },
|
---|
294 | enumerable: false,
|
---|
295 | configurable: true
|
---|
296 | });
|
---|
297 | Object.defineProperty(Element.prototype, "attributes", {
|
---|
298 | get: function () {
|
---|
299 | var _this = this;
|
---|
300 | return Object.keys(this.attribs).map(function (name) {
|
---|
301 | var _a, _b;
|
---|
302 | return ({
|
---|
303 | name: name,
|
---|
304 | value: _this.attribs[name],
|
---|
305 | namespace: (_a = _this["x-attribsNamespace"]) === null || _a === void 0 ? void 0 : _a[name],
|
---|
306 | prefix: (_b = _this["x-attribsPrefix"]) === null || _b === void 0 ? void 0 : _b[name],
|
---|
307 | });
|
---|
308 | });
|
---|
309 | },
|
---|
310 | enumerable: false,
|
---|
311 | configurable: true
|
---|
312 | });
|
---|
313 | return Element;
|
---|
314 | }(NodeWithChildren));
|
---|
315 | exports.Element = Element;
|
---|
316 | /**
|
---|
317 | * @param node Node to check.
|
---|
318 | * @returns `true` if the node is a `Element`, `false` otherwise.
|
---|
319 | */
|
---|
320 | function isTag(node) {
|
---|
321 | return (0, domelementtype_1.isTag)(node);
|
---|
322 | }
|
---|
323 | exports.isTag = isTag;
|
---|
324 | /**
|
---|
325 | * @param node Node to check.
|
---|
326 | * @returns `true` if the node has the type `CDATA`, `false` otherwise.
|
---|
327 | */
|
---|
328 | function isCDATA(node) {
|
---|
329 | return node.type === domelementtype_1.ElementType.CDATA;
|
---|
330 | }
|
---|
331 | exports.isCDATA = isCDATA;
|
---|
332 | /**
|
---|
333 | * @param node Node to check.
|
---|
334 | * @returns `true` if the node has the type `Text`, `false` otherwise.
|
---|
335 | */
|
---|
336 | function isText(node) {
|
---|
337 | return node.type === domelementtype_1.ElementType.Text;
|
---|
338 | }
|
---|
339 | exports.isText = isText;
|
---|
340 | /**
|
---|
341 | * @param node Node to check.
|
---|
342 | * @returns `true` if the node has the type `Comment`, `false` otherwise.
|
---|
343 | */
|
---|
344 | function isComment(node) {
|
---|
345 | return node.type === domelementtype_1.ElementType.Comment;
|
---|
346 | }
|
---|
347 | exports.isComment = isComment;
|
---|
348 | /**
|
---|
349 | * @param node Node to check.
|
---|
350 | * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
|
---|
351 | */
|
---|
352 | function isDirective(node) {
|
---|
353 | return node.type === domelementtype_1.ElementType.Directive;
|
---|
354 | }
|
---|
355 | exports.isDirective = isDirective;
|
---|
356 | /**
|
---|
357 | * @param node Node to check.
|
---|
358 | * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
|
---|
359 | */
|
---|
360 | function isDocument(node) {
|
---|
361 | return node.type === domelementtype_1.ElementType.Root;
|
---|
362 | }
|
---|
363 | exports.isDocument = isDocument;
|
---|
364 | /**
|
---|
365 | * @param node Node to check.
|
---|
366 | * @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise.
|
---|
367 | */
|
---|
368 | function hasChildren(node) {
|
---|
369 | return Object.prototype.hasOwnProperty.call(node, "children");
|
---|
370 | }
|
---|
371 | exports.hasChildren = hasChildren;
|
---|
372 | /**
|
---|
373 | * Clone a node, and optionally its children.
|
---|
374 | *
|
---|
375 | * @param recursive Clone child nodes as well.
|
---|
376 | * @returns A clone of the node.
|
---|
377 | */
|
---|
378 | function cloneNode(node, recursive) {
|
---|
379 | if (recursive === void 0) { recursive = false; }
|
---|
380 | var result;
|
---|
381 | if (isText(node)) {
|
---|
382 | result = new Text(node.data);
|
---|
383 | }
|
---|
384 | else if (isComment(node)) {
|
---|
385 | result = new Comment(node.data);
|
---|
386 | }
|
---|
387 | else if (isTag(node)) {
|
---|
388 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
389 | var clone_1 = new Element(node.name, __assign({}, node.attribs), children);
|
---|
390 | children.forEach(function (child) { return (child.parent = clone_1); });
|
---|
391 | if (node.namespace != null) {
|
---|
392 | clone_1.namespace = node.namespace;
|
---|
393 | }
|
---|
394 | if (node["x-attribsNamespace"]) {
|
---|
395 | clone_1["x-attribsNamespace"] = __assign({}, node["x-attribsNamespace"]);
|
---|
396 | }
|
---|
397 | if (node["x-attribsPrefix"]) {
|
---|
398 | clone_1["x-attribsPrefix"] = __assign({}, node["x-attribsPrefix"]);
|
---|
399 | }
|
---|
400 | result = clone_1;
|
---|
401 | }
|
---|
402 | else if (isCDATA(node)) {
|
---|
403 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
404 | var clone_2 = new NodeWithChildren(domelementtype_1.ElementType.CDATA, children);
|
---|
405 | children.forEach(function (child) { return (child.parent = clone_2); });
|
---|
406 | result = clone_2;
|
---|
407 | }
|
---|
408 | else if (isDocument(node)) {
|
---|
409 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
410 | var clone_3 = new Document(children);
|
---|
411 | children.forEach(function (child) { return (child.parent = clone_3); });
|
---|
412 | if (node["x-mode"]) {
|
---|
413 | clone_3["x-mode"] = node["x-mode"];
|
---|
414 | }
|
---|
415 | result = clone_3;
|
---|
416 | }
|
---|
417 | else if (isDirective(node)) {
|
---|
418 | var instruction = new ProcessingInstruction(node.name, node.data);
|
---|
419 | if (node["x-name"] != null) {
|
---|
420 | instruction["x-name"] = node["x-name"];
|
---|
421 | instruction["x-publicId"] = node["x-publicId"];
|
---|
422 | instruction["x-systemId"] = node["x-systemId"];
|
---|
423 | }
|
---|
424 | result = instruction;
|
---|
425 | }
|
---|
426 | else {
|
---|
427 | throw new Error("Not implemented yet: ".concat(node.type));
|
---|
428 | }
|
---|
429 | result.startIndex = node.startIndex;
|
---|
430 | result.endIndex = node.endIndex;
|
---|
431 | if (node.sourceCodeLocation != null) {
|
---|
432 | result.sourceCodeLocation = node.sourceCodeLocation;
|
---|
433 | }
|
---|
434 | return result;
|
---|
435 | }
|
---|
436 | exports.cloneNode = cloneNode;
|
---|
437 | function cloneChildren(childs) {
|
---|
438 | var children = childs.map(function (child) { return cloneNode(child, true); });
|
---|
439 | for (var i = 1; i < children.length; i++) {
|
---|
440 | children[i].prev = children[i - 1];
|
---|
441 | children[i - 1].next = children[i];
|
---|
442 | }
|
---|
443 | return children;
|
---|
444 | }
|
---|