source: trip-planner-front/node_modules/domhandler/lib/index.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.2 KB
Line 
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __exportStar = (this && this.__exportStar) || function(m, exports) {
10 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.DomHandler = void 0;
14var domelementtype_1 = require("domelementtype");
15var node_1 = require("./node");
16__exportStar(require("./node"), exports);
17var reWhitespace = /\s+/g;
18// Default options
19var defaultOpts = {
20 normalizeWhitespace: false,
21 withStartIndices: false,
22 withEndIndices: false,
23 xmlMode: false,
24};
25var DomHandler = /** @class */ (function () {
26 /**
27 * @param callback Called once parsing has completed.
28 * @param options Settings for the handler.
29 * @param elementCB Callback whenever a tag is closed.
30 */
31 function DomHandler(callback, options, elementCB) {
32 /** The elements of the DOM */
33 this.dom = [];
34 /** The root element for the DOM */
35 this.root = new node_1.Document(this.dom);
36 /** Indicated whether parsing has been completed. */
37 this.done = false;
38 /** Stack of open tags. */
39 this.tagStack = [this.root];
40 /** A data node that is still being written to. */
41 this.lastNode = null;
42 /** Reference to the parser instance. Used for location information. */
43 this.parser = null;
44 // Make it possible to skip arguments, for backwards-compatibility
45 if (typeof options === "function") {
46 elementCB = options;
47 options = defaultOpts;
48 }
49 if (typeof callback === "object") {
50 options = callback;
51 callback = undefined;
52 }
53 this.callback = callback !== null && callback !== void 0 ? callback : null;
54 this.options = options !== null && options !== void 0 ? options : defaultOpts;
55 this.elementCB = elementCB !== null && elementCB !== void 0 ? elementCB : null;
56 }
57 DomHandler.prototype.onparserinit = function (parser) {
58 this.parser = parser;
59 };
60 // Resets the handler back to starting state
61 DomHandler.prototype.onreset = function () {
62 this.dom = [];
63 this.root = new node_1.Document(this.dom);
64 this.done = false;
65 this.tagStack = [this.root];
66 this.lastNode = null;
67 this.parser = null;
68 };
69 // Signals the handler that parsing is done
70 DomHandler.prototype.onend = function () {
71 if (this.done)
72 return;
73 this.done = true;
74 this.parser = null;
75 this.handleCallback(null);
76 };
77 DomHandler.prototype.onerror = function (error) {
78 this.handleCallback(error);
79 };
80 DomHandler.prototype.onclosetag = function () {
81 this.lastNode = null;
82 var elem = this.tagStack.pop();
83 if (this.options.withEndIndices) {
84 elem.endIndex = this.parser.endIndex;
85 }
86 if (this.elementCB)
87 this.elementCB(elem);
88 };
89 DomHandler.prototype.onopentag = function (name, attribs) {
90 var type = this.options.xmlMode ? domelementtype_1.ElementType.Tag : undefined;
91 var element = new node_1.Element(name, attribs, undefined, type);
92 this.addNode(element);
93 this.tagStack.push(element);
94 };
95 DomHandler.prototype.ontext = function (data) {
96 var normalizeWhitespace = this.options.normalizeWhitespace;
97 var lastNode = this.lastNode;
98 if (lastNode && lastNode.type === domelementtype_1.ElementType.Text) {
99 if (normalizeWhitespace) {
100 lastNode.data = (lastNode.data + data).replace(reWhitespace, " ");
101 }
102 else {
103 lastNode.data += data;
104 }
105 if (this.options.withEndIndices) {
106 lastNode.endIndex = this.parser.endIndex;
107 }
108 }
109 else {
110 if (normalizeWhitespace) {
111 data = data.replace(reWhitespace, " ");
112 }
113 var node = new node_1.Text(data);
114 this.addNode(node);
115 this.lastNode = node;
116 }
117 };
118 DomHandler.prototype.oncomment = function (data) {
119 if (this.lastNode && this.lastNode.type === domelementtype_1.ElementType.Comment) {
120 this.lastNode.data += data;
121 return;
122 }
123 var node = new node_1.Comment(data);
124 this.addNode(node);
125 this.lastNode = node;
126 };
127 DomHandler.prototype.oncommentend = function () {
128 this.lastNode = null;
129 };
130 DomHandler.prototype.oncdatastart = function () {
131 var text = new node_1.Text("");
132 var node = new node_1.NodeWithChildren(domelementtype_1.ElementType.CDATA, [text]);
133 this.addNode(node);
134 text.parent = node;
135 this.lastNode = text;
136 };
137 DomHandler.prototype.oncdataend = function () {
138 this.lastNode = null;
139 };
140 DomHandler.prototype.onprocessinginstruction = function (name, data) {
141 var node = new node_1.ProcessingInstruction(name, data);
142 this.addNode(node);
143 };
144 DomHandler.prototype.handleCallback = function (error) {
145 if (typeof this.callback === "function") {
146 this.callback(error, this.dom);
147 }
148 else if (error) {
149 throw error;
150 }
151 };
152 DomHandler.prototype.addNode = function (node) {
153 var parent = this.tagStack[this.tagStack.length - 1];
154 var previousSibling = parent.children[parent.children.length - 1];
155 if (this.options.withStartIndices) {
156 node.startIndex = this.parser.startIndex;
157 }
158 if (this.options.withEndIndices) {
159 node.endIndex = this.parser.endIndex;
160 }
161 parent.children.push(node);
162 if (previousSibling) {
163 node.prev = previousSibling;
164 previousSibling.next = node;
165 }
166 node.parent = parent;
167 this.lastNode = null;
168 };
169 return DomHandler;
170}());
171exports.DomHandler = DomHandler;
172exports.default = DomHandler;
Note: See TracBrowser for help on using the repository browser.