easysax.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. 'use strict';
  2. /*
  3. new function() {
  4. var parser = new EasySAXParser();
  5. parser.ns('rss', { // or false
  6. 'http://search.yahoo.com/mrss/': 'media',
  7. 'http://www.w3.org/1999/xhtml': 'xhtml',
  8. 'http://www.w3.org/2005/Atom': 'atom',
  9. 'http://purl.org/rss/1.0/': 'rss',
  10. });
  11. parser.on('error', function(msgError) {
  12. });
  13. parser.on('startNode', function(elemName, getAttr, isTagEnd, getStrNode) {
  14. var attr = getAttr();
  15. });
  16. parser.on('endNode', function(elemName, isTagStart, getStrNode) {
  17. });
  18. parser.on('textNode', function(text) {
  19. });
  20. parser.on('cdata', function(data) {
  21. });
  22. parser.on('comment', function(text) {
  23. //console.log('--'+text+'--')
  24. });
  25. //parser.on('unknownNS', function(key) {console.log('unknownNS: ' + key)});
  26. //parser.on('question', function() {}); // <? ... ?>
  27. //parser.on('attention', function() {}); // <!XXXXX zzzz="eeee">
  28. console.time('easysax');
  29. for(var z=1000;z--;) {
  30. parser.parse(xml)
  31. };
  32. console.timeEnd('easysax');
  33. };
  34. */
  35. // << ------------------------------------------------------------------------ >> //
  36. EasySAXParser.entityDecode = xmlEntityDecode;
  37. export default EasySAXParser;
  38. var stringFromCharCode = String.fromCharCode;
  39. var objectCreate = Object.create;
  40. function NULL_FUNC() {}
  41. function entity2char(x) {
  42. if (x === 'amp') {
  43. return '&';
  44. }
  45. switch(x.toLocaleLowerCase()) {
  46. case 'quot': return '"';
  47. case 'amp': return '&'
  48. case 'lt': return '<'
  49. case 'gt': return '>'
  50. case 'plusmn': return '\u00B1';
  51. case 'laquo': return '\u00AB';
  52. case 'raquo': return '\u00BB';
  53. case 'micro': return '\u00B5';
  54. case 'nbsp': return '\u00A0';
  55. case 'copy': return '\u00A9';
  56. case 'sup2': return '\u00B2';
  57. case 'sup3': return '\u00B3';
  58. case 'para': return '\u00B6';
  59. case 'reg': return '\u00AE';
  60. case 'deg': return '\u00B0';
  61. case 'apos': return '\'';
  62. }
  63. return '&' + x + ';';
  64. }
  65. function replaceEntities(s, d, x, z) {
  66. if (z) {
  67. return entity2char(z);
  68. }
  69. if (d) {
  70. return stringFromCharCode(d);
  71. }
  72. return stringFromCharCode(parseInt(x, 16));
  73. }
  74. function xmlEntityDecode(s) {
  75. s = ('' + s);
  76. if (s.length > 3 && s.indexOf('&') !== -1) {
  77. if (s.indexOf('&lt;') !== -1) {s = s.replace(/&lt;/g, '<');}
  78. if (s.indexOf('&gt;') !== -1) {s = s.replace(/&gt;/g, '>');}
  79. if (s.indexOf('&quot;') !== -1) {s = s.replace(/&quot;/g, '"');}
  80. if (s.indexOf('&') !== -1) {
  81. s = s.replace(/&#(\d+);|&#x([0123456789abcdef]+);|&(\w+);/ig, replaceEntities);
  82. }
  83. }
  84. return s;
  85. }
  86. function cloneMatrixNS(nsmatrix) {
  87. var nn = objectCreate(null);
  88. for (var n in nsmatrix) {
  89. nn[n] = nsmatrix[n];
  90. }
  91. return nn;
  92. }
  93. function EasySAXParser(config) {
  94. if (!this) {
  95. return null;
  96. }
  97. var onTextNode = NULL_FUNC, onStartNode = NULL_FUNC, onEndNode = NULL_FUNC, onCDATA = NULL_FUNC, onError = NULL_FUNC,
  98. onComment, onQuestion, onAttention, onUnknownNS, onProgress;
  99. var is_onComment = false, is_onQuestion = false, is_onAttention = false, is_onUnknownNS = false, is_onProgress = false;
  100. var isAutoEntity = true; // делать "EntityDecode" всегда
  101. var entityDecode = xmlEntityDecode;
  102. var hasSurmiseNS = false;
  103. var isNamespace = false;
  104. var returnError = null;
  105. var parseStop = false; // прервать парсер
  106. var defaultNS;
  107. var nsmatrix = null;
  108. var useNS;
  109. var xml = ''; // string
  110. this.setup = function(op) {
  111. for (var name in op) {
  112. switch(name) {
  113. case 'entityDecode': entityDecode = op.entityDecode || entityDecode; break;
  114. case 'autoEntity': isAutoEntity = !!op.autoEntity; break;
  115. case 'defaultNS': defaultNS = op.defaultNS || null; break;
  116. case 'ns': isNamespace = !!(useNS = op.ns || null); break;
  117. case 'on':
  118. var listeners = op.on;
  119. for (var ev in listeners) {
  120. this.on(ev, listeners[ev]);
  121. }
  122. break;
  123. }
  124. }
  125. };
  126. this.on = function(name, cb) {
  127. if (typeof cb !== 'function') {
  128. if (cb !== null) {
  129. throw Error('required args on(string, function||null)');
  130. }
  131. }
  132. switch(name) {
  133. case 'startNode': onStartNode = cb || NULL_FUNC; break;
  134. case 'textNode': onTextNode = cb || NULL_FUNC; break;
  135. case 'endNode': onEndNode = cb || NULL_FUNC; break;
  136. case 'error': onError = cb || NULL_FUNC; break;
  137. case 'cdata': onCDATA = cb || NULL_FUNC; break;
  138. case 'unknownNS': onUnknownNS = cb; is_onUnknownNS = !!cb; break;
  139. case 'attention': onAttention = cb; is_onAttention = !!cb; break; // <!XXXXX zzzz="eeee">
  140. case 'question': onQuestion = cb; is_onQuestion = !!cb; break; // <? .... ?>
  141. case 'comment': onComment = cb; is_onComment = !!cb; break;
  142. case 'progress': onProgress = cb; is_onProgress = !!cb; break;
  143. }
  144. };
  145. this.ns = function(root, ns) {
  146. if (!root) {
  147. isNamespace = false;
  148. defaultNS = null;
  149. useNS = null;
  150. return this;
  151. }
  152. if (!ns || typeof root !== 'string') {
  153. throw Error('required args ns(string, object)');
  154. }
  155. isNamespace = !!(useNS = ns || null);
  156. defaultNS = root || null;
  157. return this;
  158. };
  159. this.parse = async function(_xml) {
  160. if (typeof _xml !== 'string') {
  161. return 'required args parser(string)'; // error
  162. }
  163. returnError = null;
  164. xml = _xml;
  165. if (isNamespace) {
  166. nsmatrix = objectCreate(null);
  167. nsmatrix.xmlns = defaultNS;
  168. await parse();
  169. nsmatrix = null;
  170. } else {
  171. await parse();
  172. }
  173. parseStop = false;
  174. attrRes = true;
  175. xml = '';
  176. return returnError;
  177. };
  178. this.stop = function() {
  179. parseStop = true;
  180. };
  181. if (config) {
  182. this.setup(config);
  183. }
  184. // -----------------------------------------------------
  185. var stringNodePosStart; // number
  186. var stringNodePosEnd; // number
  187. var attrStartPos; // number начало позиции атрибутов в строке attrString <(div^ class="xxxx" title="sssss")/>
  188. var attrString; // строка атрибутов <(div class="xxxx" title="sssss")/>
  189. var attrRes; // закешированный результат разбора атрибутов , null - разбор не проводился, object - хеш атрибутов, true - нет атрибутов, false - невалидный xml
  190. /*
  191. парсит атрибуты по требованию. Важно! - функция не генерирует исключения.
  192. если была ошибка разбора возврашается false
  193. если атрибутов нет и разбор удачен то возврашается true
  194. если есть атрибуты то возврашается обьект(хеш)
  195. */
  196. function getAttrs() {
  197. if (attrRes !== null) {
  198. return attrRes;
  199. }
  200. var xmlnsAlias;
  201. var nsAttrName;
  202. var attrList = isNamespace && hasSurmiseNS ? [] : null;
  203. var i = attrStartPos + 1; // так как первый символ уже был проверен
  204. var s = attrString;
  205. var l = s.length;
  206. var hasNewMatrix;
  207. var newalias;
  208. var value;
  209. var alias;
  210. var name;
  211. var res = {};
  212. var ok;
  213. var w;
  214. var j;
  215. for(; i < l; i++) {
  216. w = s.charCodeAt(i);
  217. if (w === 32 || (w < 14 && w > 8) ) { // \f\n\r\t\v
  218. continue
  219. }
  220. if (w < 65 || w > 122 || (w > 90 && w < 97) ) { // недопустимые первые символы
  221. if (w !== 95 && w !== 58) { // char 95"_" 58":"
  222. return attrRes = false; // error. invalid first char
  223. }
  224. }
  225. for(j = i + 1; j < l; j++) { // проверяем все символы имени атрибута
  226. w = s.charCodeAt(j);
  227. if ( w > 96 && w < 123 || w > 64 && w < 91 || w > 47 && w < 59 || w === 45 || w === 95) {
  228. continue;
  229. }
  230. if (w !== 61) { // "=" == 61
  231. return attrRes = false; // error. invalid char "="
  232. }
  233. break;
  234. }
  235. name = s.substring(i, j);
  236. ok = true;
  237. if (name === 'xmlns:xmlns') {
  238. return attrRes = false; // error. invalid name
  239. }
  240. w = s.charCodeAt(j + 1);
  241. if (w === 34) { // '"'
  242. j = s.indexOf('"', i = j + 2 );
  243. } else {
  244. if (w !== 39) { // "'"
  245. return attrRes = false; // error. invalid char
  246. }
  247. j = s.indexOf('\'', i = j + 2 );
  248. }
  249. if (j === -1) {
  250. return attrRes = false; // error. invalid char
  251. }
  252. if (j + 1 < l) {
  253. w = s.charCodeAt(j + 1);
  254. if (w > 32 || w < 9 || (w < 32 && w > 13)) {
  255. // error. invalid char
  256. return attrRes = false;
  257. }
  258. }
  259. value = s.substring(i, j);
  260. i = j + 1; // след. семвол уже проверен потому проверять нужно следуюший
  261. if (isAutoEntity) {
  262. value = entityDecode(value);
  263. }
  264. if (!isNamespace) { //
  265. res[name] = value;
  266. continue;
  267. }
  268. if (hasSurmiseNS) {
  269. // есть подозрение что в атрибутах присутствует xmlns
  270. newalias = (name !== 'xmlns'
  271. ? name.charCodeAt(0) === 120 && name.substr(0, 6) === 'xmlns:' ? name.substr(6) : null
  272. : 'xmlns'
  273. );
  274. if (newalias !== null) {
  275. alias = useNS[entityDecode(value)];
  276. if (is_onUnknownNS && !alias) {
  277. alias = onUnknownNS(value);
  278. }
  279. if (alias) {
  280. if (nsmatrix[newalias] !== alias) {
  281. if (!hasNewMatrix) {
  282. nsmatrix = cloneMatrixNS(nsmatrix);
  283. hasNewMatrix = true;
  284. }
  285. nsmatrix[newalias] = alias;
  286. }
  287. } else {
  288. if (nsmatrix[newalias]) {
  289. if (!hasNewMatrix) {
  290. nsmatrix = cloneMatrixNS(nsmatrix);
  291. hasNewMatrix = true;
  292. }
  293. nsmatrix[newalias] = false;
  294. }
  295. }
  296. res[name] = value;
  297. continue;
  298. }
  299. attrList.push(name, value);
  300. continue;
  301. }
  302. w = name.indexOf(':');
  303. if (w === -1) {
  304. res[name] = value;
  305. continue;
  306. }
  307. nsAttrName = nsmatrix[name.substring(0, w)];
  308. if (nsAttrName) {
  309. nsAttrName = nsmatrix['xmlns'] === nsAttrName ? name.substr(w + 1) : nsAttrName + name.substr(w);
  310. res[nsAttrName + name.substr(w)] = value;
  311. }
  312. }
  313. if (!ok) {
  314. return attrRes = true; // атрибутов нет, ошибок тоже нет
  315. }
  316. if (hasSurmiseNS) {
  317. xmlnsAlias = nsmatrix['xmlns'];
  318. for (i = 0, l = attrList.length; i < l; i++) {
  319. name = attrList[i++];
  320. w = name.indexOf(':');
  321. if (w !== -1) {
  322. nsAttrName = nsmatrix[name.substring(0, w)];
  323. if (nsAttrName) {
  324. nsAttrName = xmlnsAlias === nsAttrName ? name.substr(w + 1) : nsAttrName + name.substr(w);
  325. res[nsAttrName] = attrList[i];
  326. }
  327. continue;
  328. }
  329. res[name] = attrList[i];
  330. }
  331. }
  332. return attrRes = res;
  333. }
  334. function getStringNode() {
  335. return xml.substring(stringNodePosStart, stringNodePosEnd + 1);
  336. }
  337. async function parse() {
  338. var stacknsmatrix = [];
  339. var nodestack = [];
  340. var stopIndex = 0;
  341. var _nsmatrix;
  342. var isTagStart = false;
  343. var isTagEnd = false;
  344. var x, y, q, w;
  345. var j = 0;
  346. var i = 0;
  347. var xmlns;
  348. var elem;
  349. var stop; // используется при разборе "namespace" . если встретился неизвестное пространство то события не генерируются
  350. var xmlLength = xml.length;
  351. var progStep = xmlLength/100;
  352. var progCur = 0;
  353. while(j !== -1) {
  354. stop = stopIndex > 0;
  355. if (xml.charCodeAt(j) === 60) { // "<"
  356. i = j;
  357. } else {
  358. i = xml.indexOf('<', j);
  359. }
  360. if (i === -1) { // конец разбора
  361. if (nodestack.length) {
  362. onError(returnError = 'unexpected end parse');
  363. return;
  364. }
  365. if (j === 0) {
  366. onError(returnError = 'missing first tag');
  367. return;
  368. }
  369. return;
  370. }
  371. if (j !== i && !stop) {
  372. onTextNode(isAutoEntity ? entityDecode(xml.substring(j, i)) : xml.substring(j, i));
  373. if (parseStop) {
  374. return;
  375. }
  376. }
  377. w = xml.charCodeAt(i+1);
  378. if (w === 33) { // "!"
  379. w = xml.charCodeAt(i+2);
  380. if (w === 91 && xml.substr(i + 3, 6) === 'CDATA[') { // 91 == "["
  381. j = xml.indexOf(']]>', i);
  382. if (j === -1) {
  383. onError(returnError = 'cdata');
  384. return;
  385. }
  386. if (!stop) {
  387. onCDATA(xml.substring(i + 9, j));
  388. if (parseStop) {
  389. return;
  390. }
  391. }
  392. j += 3;
  393. continue;
  394. }
  395. if (w === 45 && xml.charCodeAt(i + 3) === 45) { // 45 == "-"
  396. j = xml.indexOf('-->', i);
  397. if (j === -1) {
  398. onError(returnError = 'expected -->');
  399. return;
  400. }
  401. if (is_onComment && !stop) {
  402. onComment(isAutoEntity ? entityDecode(xml.substring(i + 4, j)) : xml.substring(i + 4, j));
  403. if (parseStop) {
  404. return;
  405. }
  406. }
  407. j += 3;
  408. continue;
  409. }
  410. j = xml.indexOf('>', i + 1);
  411. if (j === -1) {
  412. onError(returnError = 'expected ">"');
  413. return;
  414. }
  415. if (is_onAttention && !stop) {
  416. onAttention(xml.substring(i, j + 1));
  417. if (parseStop) {
  418. return;
  419. }
  420. }
  421. j += 1;
  422. continue;
  423. }
  424. if (w === 63) { // "?"
  425. j = xml.indexOf('?>', i);
  426. if (j === -1) { // error
  427. onError(returnError = '...?>');
  428. return;
  429. }
  430. if (is_onQuestion) {
  431. onQuestion(xml.substring(i, j + 2));
  432. if (parseStop) {
  433. return;
  434. }
  435. }
  436. j += 2;
  437. continue;
  438. }
  439. j = xml.indexOf('>', i + 1);
  440. if (j == -1) { // error
  441. onError(returnError = 'unclosed tag'); // ...>
  442. return;
  443. }
  444. attrRes = true; // атрибутов нет
  445. //if (xml.charCodeAt(i+1) === 47) { // </...
  446. if (w === 47) { // </...
  447. isTagStart = false;
  448. isTagEnd = true;
  449. // проверяем что должен быть закрыт тотже тег что и открывался
  450. if (!nodestack.length) {
  451. onError(returnError = 'close tag, requires open tag');
  452. return;
  453. }
  454. x = elem = nodestack.pop();
  455. q = i + 2 + elem.length;
  456. if (elem !== xml.substring(i + 2, q)) {
  457. onError(returnError = 'close tag, not equal to the open tag');
  458. return;
  459. }
  460. // проверим что в закрываюшем теге нет лишнего
  461. for(; q < j; q++) {
  462. w = xml.charCodeAt(q);
  463. if (w === 32 || (w > 8 && w < 14)) { // \f\n\r\t\v пробел
  464. continue;
  465. }
  466. onError(returnError = 'close tag');
  467. return;
  468. }
  469. } else {
  470. if (xml.charCodeAt(j - 1) === 47) { // .../>
  471. x = elem = xml.substring(i + 1, j - 1);
  472. isTagStart = true;
  473. isTagEnd = true;
  474. } else {
  475. x = elem = xml.substring(i + 1, j);
  476. isTagStart = true;
  477. isTagEnd = false;
  478. }
  479. if (!(w > 96 && w < 123 || w > 64 && w < 91 || w === 95 || w === 58)) { // char 95"_" 58":"
  480. onError(returnError = 'first char nodeName');
  481. return;
  482. }
  483. for (q = 1, y = x.length; q < y; q++) {
  484. w = x.charCodeAt(q);
  485. if (w > 96 && w < 123 || w > 64 && w < 91 || w > 47 && w < 59 || w === 45 || w === 95) {
  486. continue;
  487. }
  488. if (w === 32 || (w < 14 && w > 8)) { // \f\n\r\t\v пробел
  489. attrRes = null; // возможно есть атирибуты
  490. elem = x.substring(0, q)
  491. break;
  492. }
  493. onError(returnError = 'invalid nodeName');
  494. return;
  495. }
  496. if (!isTagEnd) {
  497. nodestack.push(elem);
  498. }
  499. }
  500. if (isNamespace) {
  501. if (stop) { // потомки неизвестного пространства имен
  502. if (isTagEnd) {
  503. if (!isTagStart) {
  504. if (--stopIndex === 0) {
  505. nsmatrix = stacknsmatrix.pop();
  506. }
  507. }
  508. } else {
  509. stopIndex += 1;
  510. }
  511. j += 1;
  512. continue;
  513. }
  514. // добавляем в stacknsmatrix только если !isTagEnd, иначе сохраняем контекст пространств в переменной
  515. _nsmatrix = nsmatrix;
  516. if (!isTagEnd) {
  517. stacknsmatrix.push(nsmatrix);
  518. }
  519. if (isTagStart && (attrRes === null)) {
  520. hasSurmiseNS = x.indexOf('xmlns', q) !== -1;
  521. if (hasSurmiseNS) { // есть подозрение на xmlns
  522. attrStartPos = q;
  523. attrString = x;
  524. getAttrs();
  525. hasSurmiseNS = false;
  526. }
  527. }
  528. w = elem.indexOf(':');
  529. if (w !== -1) {
  530. xmlns = nsmatrix[elem.substring(0, w)];
  531. elem = elem.substr(w + 1);
  532. } else {
  533. xmlns = nsmatrix.xmlns;
  534. }
  535. if (!xmlns) {
  536. // элемент неизвестного пространства имен
  537. if (isTagEnd) {
  538. nsmatrix = _nsmatrix; // так как тут всегда isTagStart
  539. } else {
  540. stopIndex = 1; // первый элемент для которого не определено пространство имен
  541. }
  542. j += 1;
  543. continue;
  544. }
  545. elem = xmlns + ':' + elem;
  546. }
  547. stringNodePosStart = i;
  548. stringNodePosEnd = j;
  549. if (isTagStart) {
  550. attrStartPos = q;
  551. attrString = x;
  552. onStartNode(elem, getAttrs, isTagEnd, getStringNode);
  553. if (parseStop) {
  554. return;
  555. }
  556. }
  557. if (isTagEnd) {
  558. onEndNode(elem, isTagStart, getStringNode);
  559. if (parseStop) {
  560. return;
  561. }
  562. if (isNamespace) {
  563. if (isTagStart) {
  564. nsmatrix = _nsmatrix;
  565. } else {
  566. nsmatrix = stacknsmatrix.pop();
  567. }
  568. }
  569. }
  570. j += 1;
  571. if (j > progCur) {
  572. if (is_onProgress)
  573. await onProgress(Math.round(j*100/xmlLength));
  574. progCur += progStep;
  575. }
  576. }
  577. }
  578. }