[6a3a178] | 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 | get: function () {
|
---|
| 66 | var _a;
|
---|
| 67 | return (_a = nodeTypes.get(this.type)) !== null && _a !== void 0 ? _a : 1;
|
---|
| 68 | },
|
---|
| 69 | enumerable: false,
|
---|
| 70 | configurable: true
|
---|
| 71 | });
|
---|
| 72 | Object.defineProperty(Node.prototype, "parentNode", {
|
---|
| 73 | // Read-write aliases for properties
|
---|
| 74 | get: function () {
|
---|
| 75 | return this.parent;
|
---|
| 76 | },
|
---|
| 77 | set: function (parent) {
|
---|
| 78 | this.parent = parent;
|
---|
| 79 | },
|
---|
| 80 | enumerable: false,
|
---|
| 81 | configurable: true
|
---|
| 82 | });
|
---|
| 83 | Object.defineProperty(Node.prototype, "previousSibling", {
|
---|
| 84 | get: function () {
|
---|
| 85 | return this.prev;
|
---|
| 86 | },
|
---|
| 87 | set: function (prev) {
|
---|
| 88 | this.prev = prev;
|
---|
| 89 | },
|
---|
| 90 | enumerable: false,
|
---|
| 91 | configurable: true
|
---|
| 92 | });
|
---|
| 93 | Object.defineProperty(Node.prototype, "nextSibling", {
|
---|
| 94 | get: function () {
|
---|
| 95 | return this.next;
|
---|
| 96 | },
|
---|
| 97 | set: function (next) {
|
---|
| 98 | this.next = next;
|
---|
| 99 | },
|
---|
| 100 | enumerable: false,
|
---|
| 101 | configurable: true
|
---|
| 102 | });
|
---|
| 103 | /**
|
---|
| 104 | * Clone this node, and optionally its children.
|
---|
| 105 | *
|
---|
| 106 | * @param recursive Clone child nodes as well.
|
---|
| 107 | * @returns A clone of the node.
|
---|
| 108 | */
|
---|
| 109 | Node.prototype.cloneNode = function (recursive) {
|
---|
| 110 | if (recursive === void 0) { recursive = false; }
|
---|
| 111 | return cloneNode(this, recursive);
|
---|
| 112 | };
|
---|
| 113 | return Node;
|
---|
| 114 | }());
|
---|
| 115 | exports.Node = Node;
|
---|
| 116 | /**
|
---|
| 117 | * A node that contains some data.
|
---|
| 118 | */
|
---|
| 119 | var DataNode = /** @class */ (function (_super) {
|
---|
| 120 | __extends(DataNode, _super);
|
---|
| 121 | /**
|
---|
| 122 | * @param type The type of the node
|
---|
| 123 | * @param data The content of the data node
|
---|
| 124 | */
|
---|
| 125 | function DataNode(type, data) {
|
---|
| 126 | var _this = _super.call(this, type) || this;
|
---|
| 127 | _this.data = data;
|
---|
| 128 | return _this;
|
---|
| 129 | }
|
---|
| 130 | Object.defineProperty(DataNode.prototype, "nodeValue", {
|
---|
| 131 | get: function () {
|
---|
| 132 | return this.data;
|
---|
| 133 | },
|
---|
| 134 | set: function (data) {
|
---|
| 135 | this.data = data;
|
---|
| 136 | },
|
---|
| 137 | enumerable: false,
|
---|
| 138 | configurable: true
|
---|
| 139 | });
|
---|
| 140 | return DataNode;
|
---|
| 141 | }(Node));
|
---|
| 142 | exports.DataNode = DataNode;
|
---|
| 143 | /**
|
---|
| 144 | * Text within the document.
|
---|
| 145 | */
|
---|
| 146 | var Text = /** @class */ (function (_super) {
|
---|
| 147 | __extends(Text, _super);
|
---|
| 148 | function Text(data) {
|
---|
| 149 | return _super.call(this, domelementtype_1.ElementType.Text, data) || this;
|
---|
| 150 | }
|
---|
| 151 | return Text;
|
---|
| 152 | }(DataNode));
|
---|
| 153 | exports.Text = Text;
|
---|
| 154 | /**
|
---|
| 155 | * Comments within the document.
|
---|
| 156 | */
|
---|
| 157 | var Comment = /** @class */ (function (_super) {
|
---|
| 158 | __extends(Comment, _super);
|
---|
| 159 | function Comment(data) {
|
---|
| 160 | return _super.call(this, domelementtype_1.ElementType.Comment, data) || this;
|
---|
| 161 | }
|
---|
| 162 | return Comment;
|
---|
| 163 | }(DataNode));
|
---|
| 164 | exports.Comment = Comment;
|
---|
| 165 | /**
|
---|
| 166 | * Processing instructions, including doc types.
|
---|
| 167 | */
|
---|
| 168 | var ProcessingInstruction = /** @class */ (function (_super) {
|
---|
| 169 | __extends(ProcessingInstruction, _super);
|
---|
| 170 | function ProcessingInstruction(name, data) {
|
---|
| 171 | var _this = _super.call(this, domelementtype_1.ElementType.Directive, data) || this;
|
---|
| 172 | _this.name = name;
|
---|
| 173 | return _this;
|
---|
| 174 | }
|
---|
| 175 | return ProcessingInstruction;
|
---|
| 176 | }(DataNode));
|
---|
| 177 | exports.ProcessingInstruction = ProcessingInstruction;
|
---|
| 178 | /**
|
---|
| 179 | * A `Node` that can have children.
|
---|
| 180 | */
|
---|
| 181 | var NodeWithChildren = /** @class */ (function (_super) {
|
---|
| 182 | __extends(NodeWithChildren, _super);
|
---|
| 183 | /**
|
---|
| 184 | * @param type Type of the node.
|
---|
| 185 | * @param children Children of the node. Only certain node types can have children.
|
---|
| 186 | */
|
---|
| 187 | function NodeWithChildren(type, children) {
|
---|
| 188 | var _this = _super.call(this, type) || this;
|
---|
| 189 | _this.children = children;
|
---|
| 190 | return _this;
|
---|
| 191 | }
|
---|
| 192 | Object.defineProperty(NodeWithChildren.prototype, "firstChild", {
|
---|
| 193 | // Aliases
|
---|
| 194 | get: function () {
|
---|
| 195 | var _a;
|
---|
| 196 | return (_a = this.children[0]) !== null && _a !== void 0 ? _a : null;
|
---|
| 197 | },
|
---|
| 198 | enumerable: false,
|
---|
| 199 | configurable: true
|
---|
| 200 | });
|
---|
| 201 | Object.defineProperty(NodeWithChildren.prototype, "lastChild", {
|
---|
| 202 | get: function () {
|
---|
| 203 | return this.children.length > 0
|
---|
| 204 | ? this.children[this.children.length - 1]
|
---|
| 205 | : null;
|
---|
| 206 | },
|
---|
| 207 | enumerable: false,
|
---|
| 208 | configurable: true
|
---|
| 209 | });
|
---|
| 210 | Object.defineProperty(NodeWithChildren.prototype, "childNodes", {
|
---|
| 211 | get: function () {
|
---|
| 212 | return this.children;
|
---|
| 213 | },
|
---|
| 214 | set: function (children) {
|
---|
| 215 | this.children = children;
|
---|
| 216 | },
|
---|
| 217 | enumerable: false,
|
---|
| 218 | configurable: true
|
---|
| 219 | });
|
---|
| 220 | return NodeWithChildren;
|
---|
| 221 | }(Node));
|
---|
| 222 | exports.NodeWithChildren = NodeWithChildren;
|
---|
| 223 | /**
|
---|
| 224 | * The root node of the document.
|
---|
| 225 | */
|
---|
| 226 | var Document = /** @class */ (function (_super) {
|
---|
| 227 | __extends(Document, _super);
|
---|
| 228 | function Document(children) {
|
---|
| 229 | return _super.call(this, domelementtype_1.ElementType.Root, children) || this;
|
---|
| 230 | }
|
---|
| 231 | return Document;
|
---|
| 232 | }(NodeWithChildren));
|
---|
| 233 | exports.Document = Document;
|
---|
| 234 | /**
|
---|
| 235 | * An element within the DOM.
|
---|
| 236 | */
|
---|
| 237 | var Element = /** @class */ (function (_super) {
|
---|
| 238 | __extends(Element, _super);
|
---|
| 239 | /**
|
---|
| 240 | * @param name Name of the tag, eg. `div`, `span`.
|
---|
| 241 | * @param attribs Object mapping attribute names to attribute values.
|
---|
| 242 | * @param children Children of the node.
|
---|
| 243 | */
|
---|
| 244 | function Element(name, attribs, children, type) {
|
---|
| 245 | if (children === void 0) { children = []; }
|
---|
| 246 | if (type === void 0) { type = name === "script"
|
---|
| 247 | ? domelementtype_1.ElementType.Script
|
---|
| 248 | : name === "style"
|
---|
| 249 | ? domelementtype_1.ElementType.Style
|
---|
| 250 | : domelementtype_1.ElementType.Tag; }
|
---|
| 251 | var _this = _super.call(this, type, children) || this;
|
---|
| 252 | _this.name = name;
|
---|
| 253 | _this.attribs = attribs;
|
---|
| 254 | return _this;
|
---|
| 255 | }
|
---|
| 256 | Object.defineProperty(Element.prototype, "tagName", {
|
---|
| 257 | // DOM Level 1 aliases
|
---|
| 258 | get: function () {
|
---|
| 259 | return this.name;
|
---|
| 260 | },
|
---|
| 261 | set: function (name) {
|
---|
| 262 | this.name = name;
|
---|
| 263 | },
|
---|
| 264 | enumerable: false,
|
---|
| 265 | configurable: true
|
---|
| 266 | });
|
---|
| 267 | Object.defineProperty(Element.prototype, "attributes", {
|
---|
| 268 | get: function () {
|
---|
| 269 | var _this = this;
|
---|
| 270 | return Object.keys(this.attribs).map(function (name) {
|
---|
| 271 | var _a, _b;
|
---|
| 272 | return ({
|
---|
| 273 | name: name,
|
---|
| 274 | value: _this.attribs[name],
|
---|
| 275 | namespace: (_a = _this["x-attribsNamespace"]) === null || _a === void 0 ? void 0 : _a[name],
|
---|
| 276 | prefix: (_b = _this["x-attribsPrefix"]) === null || _b === void 0 ? void 0 : _b[name],
|
---|
| 277 | });
|
---|
| 278 | });
|
---|
| 279 | },
|
---|
| 280 | enumerable: false,
|
---|
| 281 | configurable: true
|
---|
| 282 | });
|
---|
| 283 | return Element;
|
---|
| 284 | }(NodeWithChildren));
|
---|
| 285 | exports.Element = Element;
|
---|
| 286 | /**
|
---|
| 287 | * @param node Node to check.
|
---|
| 288 | * @returns `true` if the node is a `Element`, `false` otherwise.
|
---|
| 289 | */
|
---|
| 290 | function isTag(node) {
|
---|
| 291 | return (0, domelementtype_1.isTag)(node);
|
---|
| 292 | }
|
---|
| 293 | exports.isTag = isTag;
|
---|
| 294 | /**
|
---|
| 295 | * @param node Node to check.
|
---|
| 296 | * @returns `true` if the node has the type `CDATA`, `false` otherwise.
|
---|
| 297 | */
|
---|
| 298 | function isCDATA(node) {
|
---|
| 299 | return node.type === domelementtype_1.ElementType.CDATA;
|
---|
| 300 | }
|
---|
| 301 | exports.isCDATA = isCDATA;
|
---|
| 302 | /**
|
---|
| 303 | * @param node Node to check.
|
---|
| 304 | * @returns `true` if the node has the type `Text`, `false` otherwise.
|
---|
| 305 | */
|
---|
| 306 | function isText(node) {
|
---|
| 307 | return node.type === domelementtype_1.ElementType.Text;
|
---|
| 308 | }
|
---|
| 309 | exports.isText = isText;
|
---|
| 310 | /**
|
---|
| 311 | * @param node Node to check.
|
---|
| 312 | * @returns `true` if the node has the type `Comment`, `false` otherwise.
|
---|
| 313 | */
|
---|
| 314 | function isComment(node) {
|
---|
| 315 | return node.type === domelementtype_1.ElementType.Comment;
|
---|
| 316 | }
|
---|
| 317 | exports.isComment = isComment;
|
---|
| 318 | /**
|
---|
| 319 | * @param node Node to check.
|
---|
| 320 | * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
|
---|
| 321 | */
|
---|
| 322 | function isDirective(node) {
|
---|
| 323 | return node.type === domelementtype_1.ElementType.Directive;
|
---|
| 324 | }
|
---|
| 325 | exports.isDirective = isDirective;
|
---|
| 326 | /**
|
---|
| 327 | * @param node Node to check.
|
---|
| 328 | * @returns `true` if the node has the type `ProcessingInstruction`, `false` otherwise.
|
---|
| 329 | */
|
---|
| 330 | function isDocument(node) {
|
---|
| 331 | return node.type === domelementtype_1.ElementType.Root;
|
---|
| 332 | }
|
---|
| 333 | exports.isDocument = isDocument;
|
---|
| 334 | /**
|
---|
| 335 | * @param node Node to check.
|
---|
| 336 | * @returns `true` if the node is a `NodeWithChildren` (has children), `false` otherwise.
|
---|
| 337 | */
|
---|
| 338 | function hasChildren(node) {
|
---|
| 339 | return Object.prototype.hasOwnProperty.call(node, "children");
|
---|
| 340 | }
|
---|
| 341 | exports.hasChildren = hasChildren;
|
---|
| 342 | /**
|
---|
| 343 | * Clone a node, and optionally its children.
|
---|
| 344 | *
|
---|
| 345 | * @param recursive Clone child nodes as well.
|
---|
| 346 | * @returns A clone of the node.
|
---|
| 347 | */
|
---|
| 348 | function cloneNode(node, recursive) {
|
---|
| 349 | if (recursive === void 0) { recursive = false; }
|
---|
| 350 | var result;
|
---|
| 351 | if (isText(node)) {
|
---|
| 352 | result = new Text(node.data);
|
---|
| 353 | }
|
---|
| 354 | else if (isComment(node)) {
|
---|
| 355 | result = new Comment(node.data);
|
---|
| 356 | }
|
---|
| 357 | else if (isTag(node)) {
|
---|
| 358 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
| 359 | var clone_1 = new Element(node.name, __assign({}, node.attribs), children);
|
---|
| 360 | children.forEach(function (child) { return (child.parent = clone_1); });
|
---|
| 361 | if (node["x-attribsNamespace"]) {
|
---|
| 362 | clone_1["x-attribsNamespace"] = __assign({}, node["x-attribsNamespace"]);
|
---|
| 363 | }
|
---|
| 364 | if (node["x-attribsPrefix"]) {
|
---|
| 365 | clone_1["x-attribsPrefix"] = __assign({}, node["x-attribsPrefix"]);
|
---|
| 366 | }
|
---|
| 367 | result = clone_1;
|
---|
| 368 | }
|
---|
| 369 | else if (isCDATA(node)) {
|
---|
| 370 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
| 371 | var clone_2 = new NodeWithChildren(domelementtype_1.ElementType.CDATA, children);
|
---|
| 372 | children.forEach(function (child) { return (child.parent = clone_2); });
|
---|
| 373 | result = clone_2;
|
---|
| 374 | }
|
---|
| 375 | else if (isDocument(node)) {
|
---|
| 376 | var children = recursive ? cloneChildren(node.children) : [];
|
---|
| 377 | var clone_3 = new Document(children);
|
---|
| 378 | children.forEach(function (child) { return (child.parent = clone_3); });
|
---|
| 379 | if (node["x-mode"]) {
|
---|
| 380 | clone_3["x-mode"] = node["x-mode"];
|
---|
| 381 | }
|
---|
| 382 | result = clone_3;
|
---|
| 383 | }
|
---|
| 384 | else if (isDirective(node)) {
|
---|
| 385 | var instruction = new ProcessingInstruction(node.name, node.data);
|
---|
| 386 | if (node["x-name"] != null) {
|
---|
| 387 | instruction["x-name"] = node["x-name"];
|
---|
| 388 | instruction["x-publicId"] = node["x-publicId"];
|
---|
| 389 | instruction["x-systemId"] = node["x-systemId"];
|
---|
| 390 | }
|
---|
| 391 | result = instruction;
|
---|
| 392 | }
|
---|
| 393 | else {
|
---|
| 394 | throw new Error("Not implemented yet: " + node.type);
|
---|
| 395 | }
|
---|
| 396 | result.startIndex = node.startIndex;
|
---|
| 397 | result.endIndex = node.endIndex;
|
---|
| 398 | return result;
|
---|
| 399 | }
|
---|
| 400 | exports.cloneNode = cloneNode;
|
---|
| 401 | function cloneChildren(childs) {
|
---|
| 402 | var children = childs.map(function (child) { return cloneNode(child, true); });
|
---|
| 403 | for (var i = 1; i < children.length; i++) {
|
---|
| 404 | children[i].prev = children[i - 1];
|
---|
| 405 | children[i - 1].next = children[i];
|
---|
| 406 | }
|
---|
| 407 | return children;
|
---|
| 408 | }
|
---|