2
0

xml_parser.js 15 KB

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