UNPKG

3.93 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.parseXml = parseXml;
7exports.XElement = void 0;
8
9function sax() {
10 const data = _interopRequireWildcard(require("sax"));
11
12 sax = function () {
13 return data;
14 };
15
16 return data;
17}
18
19function _index() {
20 const data = require("./index");
21
22 _index = function () {
23 return data;
24 };
25
26 return data;
27}
28
29function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
30
31class XElement {
32 constructor(name) {
33 this.name = name;
34 this.value = "";
35 this.attributes = null;
36 this.isCData = false;
37 this.elements = null;
38
39 if (!name) {
40 throw (0, _index().newError)("Element name cannot be empty", "ERR_XML_ELEMENT_NAME_EMPTY");
41 }
42
43 if (!isValidName(name)) {
44 throw (0, _index().newError)(`Invalid element name: ${name}`, "ERR_XML_ELEMENT_INVALID_NAME");
45 }
46 }
47
48 attribute(name) {
49 const result = this.attributes === null ? null : this.attributes[name];
50
51 if (result == null) {
52 throw (0, _index().newError)(`No attribute "${name}"`, "ERR_XML_MISSED_ATTRIBUTE");
53 }
54
55 return result;
56 }
57
58 removeAttribute(name) {
59 if (this.attributes !== null) {
60 delete this.attributes[name];
61 }
62 }
63
64 element(name, ignoreCase = false, errorIfMissed = null) {
65 const result = this.elementOrNull(name, ignoreCase);
66
67 if (result === null) {
68 throw (0, _index().newError)(errorIfMissed || `No element "${name}"`, "ERR_XML_MISSED_ELEMENT");
69 }
70
71 return result;
72 }
73
74 elementOrNull(name, ignoreCase = false) {
75 if (this.elements === null) {
76 return null;
77 }
78
79 for (const element of this.elements) {
80 if (isNameEquals(element, name, ignoreCase)) {
81 return element;
82 }
83 }
84
85 return null;
86 }
87
88 getElements(name, ignoreCase = false) {
89 if (this.elements === null) {
90 return [];
91 }
92
93 return this.elements.filter(it => isNameEquals(it, name, ignoreCase));
94 }
95
96 elementValueOrEmpty(name, ignoreCase = false) {
97 const element = this.elementOrNull(name, ignoreCase);
98 return element === null ? "" : element.value;
99 }
100
101}
102
103exports.XElement = XElement;
104const NAME_REG_EXP = new RegExp(/^[A-Za-z_][:A-Za-z0-9_-]*$/i);
105
106function isValidName(name) {
107 return NAME_REG_EXP.test(name);
108}
109
110function isNameEquals(element, name, ignoreCase) {
111 const elementName = element.name;
112 return elementName === name || ignoreCase === true && elementName.length === name.length && elementName.toLowerCase() === name.toLowerCase();
113}
114
115function parseXml(data) {
116 let rootElement = null;
117 const parser = sax().parser(true, {});
118 const elements = [];
119
120 parser.onopentag = saxElement => {
121 const element = new XElement(saxElement.name);
122 element.attributes = saxElement.attributes;
123
124 if (rootElement === null) {
125 rootElement = element;
126 } else {
127 const parent = elements[elements.length - 1];
128
129 if (parent.elements == null) {
130 parent.elements = [];
131 }
132
133 parent.elements.push(element);
134 }
135
136 elements.push(element);
137 };
138
139 parser.onclosetag = () => {
140 elements.pop();
141 };
142
143 parser.ontext = text => {
144 if (elements.length > 0) {
145 elements[elements.length - 1].value = text;
146 }
147 };
148
149 parser.oncdata = cdata => {
150 const element = elements[elements.length - 1];
151 element.value = cdata;
152 element.isCData = true;
153 };
154
155 parser.onerror = err => {
156 throw err;
157 };
158
159 parser.write(data);
160 return rootElement;
161}
162// __ts-babel@6.0.4
163//# sourceMappingURL=xml.js.map
\No newline at end of file