xml_parser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.SimpleXMLParser = void 0;
  27. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  28. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  29. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  30. function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  31. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  32. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  33. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  34. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  35. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  36. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  37. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  38. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  39. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  40. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  41. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  42. var XMLParserErrorCode = {
  43. NoError: 0,
  44. EndOfDocument: -1,
  45. UnterminatedCdat: -2,
  46. UnterminatedXmlDeclaration: -3,
  47. UnterminatedDoctypeDeclaration: -4,
  48. UnterminatedComment: -5,
  49. MalformedElement: -6,
  50. OutOfMemory: -7,
  51. UnterminatedAttributeValue: -8,
  52. UnterminatedElement: -9,
  53. ElementNeverBegun: -10
  54. };
  55. function isWhitespace(s, index) {
  56. var ch = s[index];
  57. return ch === ' ' || ch === '\n' || ch === '\r' || ch === '\t';
  58. }
  59. function isWhitespaceString(s) {
  60. for (var i = 0, ii = s.length; i < ii; i++) {
  61. if (!isWhitespace(s, i)) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. var XMLParserBase =
  68. /*#__PURE__*/
  69. function () {
  70. function XMLParserBase() {
  71. _classCallCheck(this, XMLParserBase);
  72. }
  73. _createClass(XMLParserBase, [{
  74. key: "_resolveEntities",
  75. value: function _resolveEntities(s) {
  76. var _this = this;
  77. return s.replace(/&([^;]+);/g, function (all, entity) {
  78. if (entity.substring(0, 2) === '#x') {
  79. return String.fromCharCode(parseInt(entity.substring(2), 16));
  80. } else if (entity.substring(0, 1) === '#') {
  81. return String.fromCharCode(parseInt(entity.substring(1), 10));
  82. }
  83. switch (entity) {
  84. case 'lt':
  85. return '<';
  86. case 'gt':
  87. return '>';
  88. case 'amp':
  89. return '&';
  90. case 'quot':
  91. return '\"';
  92. }
  93. return _this.onResolveEntity(entity);
  94. });
  95. }
  96. }, {
  97. key: "_parseContent",
  98. value: function _parseContent(s, start) {
  99. var pos = start,
  100. name,
  101. attributes = [];
  102. function skipWs() {
  103. while (pos < s.length && isWhitespace(s, pos)) {
  104. ++pos;
  105. }
  106. }
  107. while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '>' && s[pos] !== '/') {
  108. ++pos;
  109. }
  110. name = s.substring(start, pos);
  111. skipWs();
  112. while (pos < s.length && s[pos] !== '>' && s[pos] !== '/' && s[pos] !== '?') {
  113. skipWs();
  114. var attrName = '',
  115. attrValue = '';
  116. while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '=') {
  117. attrName += s[pos];
  118. ++pos;
  119. }
  120. skipWs();
  121. if (s[pos] !== '=') {
  122. return null;
  123. }
  124. ++pos;
  125. skipWs();
  126. var attrEndChar = s[pos];
  127. if (attrEndChar !== '\"' && attrEndChar !== '\'') {
  128. return null;
  129. }
  130. var attrEndIndex = s.indexOf(attrEndChar, ++pos);
  131. if (attrEndIndex < 0) {
  132. return null;
  133. }
  134. attrValue = s.substring(pos, attrEndIndex);
  135. attributes.push({
  136. name: attrName,
  137. value: this._resolveEntities(attrValue)
  138. });
  139. pos = attrEndIndex + 1;
  140. skipWs();
  141. }
  142. return {
  143. name: name,
  144. attributes: attributes,
  145. parsed: pos - start
  146. };
  147. }
  148. }, {
  149. key: "_parseProcessingInstruction",
  150. value: function _parseProcessingInstruction(s, start) {
  151. var pos = start,
  152. name,
  153. value;
  154. function skipWs() {
  155. while (pos < s.length && isWhitespace(s, pos)) {
  156. ++pos;
  157. }
  158. }
  159. while (pos < s.length && !isWhitespace(s, pos) && s[pos] !== '>' && s[pos] !== '/') {
  160. ++pos;
  161. }
  162. name = s.substring(start, pos);
  163. skipWs();
  164. var attrStart = pos;
  165. while (pos < s.length && (s[pos] !== '?' || s[pos + 1] !== '>')) {
  166. ++pos;
  167. }
  168. value = s.substring(attrStart, pos);
  169. return {
  170. name: name,
  171. value: value,
  172. parsed: pos - start
  173. };
  174. }
  175. }, {
  176. key: "parseXml",
  177. value: function parseXml(s) {
  178. var i = 0;
  179. while (i < s.length) {
  180. var ch = s[i];
  181. var j = i;
  182. if (ch === '<') {
  183. ++j;
  184. var ch2 = s[j];
  185. var q = void 0;
  186. switch (ch2) {
  187. case '/':
  188. ++j;
  189. q = s.indexOf('>', j);
  190. if (q < 0) {
  191. this.onError(XMLParserErrorCode.UnterminatedElement);
  192. return;
  193. }
  194. this.onEndElement(s.substring(j, q));
  195. j = q + 1;
  196. break;
  197. case '?':
  198. ++j;
  199. var pi = this._parseProcessingInstruction(s, j);
  200. if (s.substring(j + pi.parsed, j + pi.parsed + 2) !== '?>') {
  201. this.onError(XMLParserErrorCode.UnterminatedXmlDeclaration);
  202. return;
  203. }
  204. this.onPi(pi.name, pi.value);
  205. j += pi.parsed + 2;
  206. break;
  207. case '!':
  208. if (s.substring(j + 1, j + 3) === '--') {
  209. q = s.indexOf('-->', j + 3);
  210. if (q < 0) {
  211. this.onError(XMLParserErrorCode.UnterminatedComment);
  212. return;
  213. }
  214. this.onComment(s.substring(j + 3, q));
  215. j = q + 3;
  216. } else if (s.substring(j + 1, j + 8) === '[CDATA[') {
  217. q = s.indexOf(']]>', j + 8);
  218. if (q < 0) {
  219. this.onError(XMLParserErrorCode.UnterminatedCdat);
  220. return;
  221. }
  222. this.onCdata(s.substring(j + 8, q));
  223. j = q + 3;
  224. } else if (s.substring(j + 1, j + 8) === 'DOCTYPE') {
  225. var q2 = s.indexOf('[', j + 8);
  226. var complexDoctype = false;
  227. q = s.indexOf('>', j + 8);
  228. if (q < 0) {
  229. this.onError(XMLParserErrorCode.UnterminatedDoctypeDeclaration);
  230. return;
  231. }
  232. if (q2 > 0 && q > q2) {
  233. q = s.indexOf(']>', j + 8);
  234. if (q < 0) {
  235. this.onError(XMLParserErrorCode.UnterminatedDoctypeDeclaration);
  236. return;
  237. }
  238. complexDoctype = true;
  239. }
  240. var doctypeContent = s.substring(j + 8, q + (complexDoctype ? 1 : 0));
  241. this.onDoctype(doctypeContent);
  242. j = q + (complexDoctype ? 2 : 1);
  243. } else {
  244. this.onError(XMLParserErrorCode.MalformedElement);
  245. return;
  246. }
  247. break;
  248. default:
  249. var content = this._parseContent(s, j);
  250. if (content === null) {
  251. this.onError(XMLParserErrorCode.MalformedElement);
  252. return;
  253. }
  254. var isClosed = false;
  255. if (s.substring(j + content.parsed, j + content.parsed + 2) === '/>') {
  256. isClosed = true;
  257. } else if (s.substring(j + content.parsed, j + content.parsed + 1) !== '>') {
  258. this.onError(XMLParserErrorCode.UnterminatedElement);
  259. return;
  260. }
  261. this.onBeginElement(content.name, content.attributes, isClosed);
  262. j += content.parsed + (isClosed ? 2 : 1);
  263. break;
  264. }
  265. } else {
  266. while (j < s.length && s[j] !== '<') {
  267. j++;
  268. }
  269. var text = s.substring(i, j);
  270. this.onText(this._resolveEntities(text));
  271. }
  272. i = j;
  273. }
  274. }
  275. }, {
  276. key: "onResolveEntity",
  277. value: function onResolveEntity(name) {
  278. return "&".concat(name, ";");
  279. }
  280. }, {
  281. key: "onPi",
  282. value: function onPi(name, value) {}
  283. }, {
  284. key: "onComment",
  285. value: function onComment(text) {}
  286. }, {
  287. key: "onCdata",
  288. value: function onCdata(text) {}
  289. }, {
  290. key: "onDoctype",
  291. value: function onDoctype(doctypeContent) {}
  292. }, {
  293. key: "onText",
  294. value: function onText(text) {}
  295. }, {
  296. key: "onBeginElement",
  297. value: function onBeginElement(name, attributes, isEmpty) {}
  298. }, {
  299. key: "onEndElement",
  300. value: function onEndElement(name) {}
  301. }, {
  302. key: "onError",
  303. value: function onError(code) {}
  304. }]);
  305. return XMLParserBase;
  306. }();
  307. var SimpleDOMNode =
  308. /*#__PURE__*/
  309. function () {
  310. function SimpleDOMNode(nodeName, nodeValue) {
  311. _classCallCheck(this, SimpleDOMNode);
  312. this.nodeName = nodeName;
  313. this.nodeValue = nodeValue;
  314. Object.defineProperty(this, 'parentNode', {
  315. value: null,
  316. writable: true
  317. });
  318. }
  319. _createClass(SimpleDOMNode, [{
  320. key: "hasChildNodes",
  321. value: function hasChildNodes() {
  322. return this.childNodes && this.childNodes.length > 0;
  323. }
  324. }, {
  325. key: "firstChild",
  326. get: function get() {
  327. return this.childNodes && this.childNodes[0];
  328. }
  329. }, {
  330. key: "nextSibling",
  331. get: function get() {
  332. var childNodes = this.parentNode.childNodes;
  333. if (!childNodes) {
  334. return undefined;
  335. }
  336. var index = childNodes.indexOf(this);
  337. if (index === -1) {
  338. return undefined;
  339. }
  340. return childNodes[index + 1];
  341. }
  342. }, {
  343. key: "textContent",
  344. get: function get() {
  345. if (!this.childNodes) {
  346. return this.nodeValue || '';
  347. }
  348. return this.childNodes.map(function (child) {
  349. return child.textContent;
  350. }).join('');
  351. }
  352. }]);
  353. return SimpleDOMNode;
  354. }();
  355. var SimpleXMLParser =
  356. /*#__PURE__*/
  357. function (_XMLParserBase) {
  358. _inherits(SimpleXMLParser, _XMLParserBase);
  359. function SimpleXMLParser() {
  360. var _this2;
  361. _classCallCheck(this, SimpleXMLParser);
  362. _this2 = _possibleConstructorReturn(this, _getPrototypeOf(SimpleXMLParser).call(this));
  363. _this2._currentFragment = null;
  364. _this2._stack = null;
  365. _this2._errorCode = XMLParserErrorCode.NoError;
  366. return _this2;
  367. }
  368. _createClass(SimpleXMLParser, [{
  369. key: "parseFromString",
  370. value: function parseFromString(data) {
  371. this._currentFragment = [];
  372. this._stack = [];
  373. this._errorCode = XMLParserErrorCode.NoError;
  374. this.parseXml(data);
  375. if (this._errorCode !== XMLParserErrorCode.NoError) {
  376. return undefined;
  377. }
  378. var _this$_currentFragmen = _slicedToArray(this._currentFragment, 1),
  379. documentElement = _this$_currentFragmen[0];
  380. if (!documentElement) {
  381. return undefined;
  382. }
  383. return {
  384. documentElement: documentElement
  385. };
  386. }
  387. }, {
  388. key: "onResolveEntity",
  389. value: function onResolveEntity(name) {
  390. switch (name) {
  391. case 'apos':
  392. return '\'';
  393. }
  394. return _get(_getPrototypeOf(SimpleXMLParser.prototype), "onResolveEntity", this).call(this, name);
  395. }
  396. }, {
  397. key: "onText",
  398. value: function onText(text) {
  399. if (isWhitespaceString(text)) {
  400. return;
  401. }
  402. var node = new SimpleDOMNode('#text', text);
  403. this._currentFragment.push(node);
  404. }
  405. }, {
  406. key: "onCdata",
  407. value: function onCdata(text) {
  408. var node = new SimpleDOMNode('#text', text);
  409. this._currentFragment.push(node);
  410. }
  411. }, {
  412. key: "onBeginElement",
  413. value: function onBeginElement(name, attributes, isEmpty) {
  414. var node = new SimpleDOMNode(name);
  415. node.childNodes = [];
  416. this._currentFragment.push(node);
  417. if (isEmpty) {
  418. return;
  419. }
  420. this._stack.push(this._currentFragment);
  421. this._currentFragment = node.childNodes;
  422. }
  423. }, {
  424. key: "onEndElement",
  425. value: function onEndElement(name) {
  426. this._currentFragment = this._stack.pop() || [];
  427. var lastElement = this._currentFragment[this._currentFragment.length - 1];
  428. if (!lastElement) {
  429. return;
  430. }
  431. for (var i = 0, ii = lastElement.childNodes.length; i < ii; i++) {
  432. lastElement.childNodes[i].parentNode = lastElement;
  433. }
  434. }
  435. }, {
  436. key: "onError",
  437. value: function onError(code) {
  438. this._errorCode = code;
  439. }
  440. }]);
  441. return SimpleXMLParser;
  442. }(XMLParserBase);
  443. exports.SimpleXMLParser = SimpleXMLParser;