xml.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. var xml = {};
  2. var text = require('./text');
  3. // https://www.w3.org/TR/xml
  4. xml.TextReader = class {
  5. static open(data, callback) {
  6. const decoder = text.Decoder.open(data);
  7. for (;;) {
  8. const c = decoder.decode();
  9. if (c === '<') {
  10. break;
  11. }
  12. if (c === ' ' || c === '\n' || c === '\r' || c === '\t') {
  13. continue;
  14. }
  15. return null;
  16. }
  17. return new xml.TextReader(data, callback);
  18. }
  19. constructor(data, callback) {
  20. this._data = data;
  21. this._callback = callback;
  22. this._entities = new Map([ [ 'quot', '"' ], [ 'amp', '&' ], [ 'apos', "'" ], [ 'lt', '<' ], [ 'gt', '>' ] ]);
  23. this._nameStartCharRegExp = /[:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/;
  24. this._nameCharRegExp = new RegExp("[-.0-9\\xB7" + this._nameStartCharRegExp.source.slice(1, -1) + "]");
  25. xml.Utility.nameStartCharRegExp = this._nameStartCharRegExp;
  26. }
  27. peek() {
  28. this._peek = true;
  29. const value = this.read();
  30. delete this._peek;
  31. return value;
  32. }
  33. read() {
  34. this._stack = [];
  35. this._context = [];
  36. this._pushBuffer(this._data, '', '', false);
  37. this._version = 0;
  38. /* eslint-disable */
  39. this._charRegExp = /[\x09\x0a\x0d\x20-\uD7FF\uE000-\uFFFD]/;
  40. /* eslint-enable */
  41. this._parameterEntities = false;
  42. this._characterData = true;
  43. this._push(new xml.Document());
  44. const document = this._document();
  45. for (;;) {
  46. this._start = this._position;
  47. switch (this._char) {
  48. case '<': {
  49. this._next();
  50. switch (this._char) {
  51. case '?': {
  52. this._processingInstruction();
  53. break;
  54. }
  55. case '!': {
  56. this._next();
  57. if (this._match('--')) {
  58. this._comment();
  59. }
  60. else if (this._match('[CDATA')) {
  61. this._assert(this._stack.length > 1);
  62. this._characterData = true;
  63. this._expect('[');
  64. const data = this._terminal(']]>');
  65. const node = document.createCDATASection(data);
  66. this._appendChild(node);
  67. }
  68. else if (this._match('DOCTYPE')) {
  69. this._assert(this._stack.length > 1 || !document.documentElement || !document.documentType);
  70. this._whitespace(1);
  71. const name = this._name();
  72. this._assert(name !== null);
  73. let systemId = '';
  74. let publicId = '';
  75. let whitespace = this._whitespace(0);
  76. if (whitespace && this._match('SYSTEM')) {
  77. this._whitespace(1);
  78. systemId = this._systemLiteral();
  79. this._whitespace(0);
  80. whitespace = true;
  81. }
  82. else if (whitespace && this._match('PUBLIC')) {
  83. this._whitespace(1);
  84. publicId = this._pubidLiteral();
  85. this._whitespace(1);
  86. systemId = this._systemLiteral();
  87. this._whitespace(0);
  88. whitespace = true;
  89. }
  90. const node = document.createDocumentType(name, publicId, systemId);
  91. this._appendChild(node);
  92. this._push(node);
  93. node.parameterEntities = new xml.NamedNodeMap();
  94. node.elements = new xml.NamedNodeMap();
  95. this._parameterEntities = true;
  96. this._characterData = false;
  97. const internalSubset = whitespace && this._match('[');
  98. if (internalSubset) {
  99. this._internalSubset(']');
  100. }
  101. if (systemId && !this._standalone) {
  102. this._pushResource(systemId, '', true);
  103. this._internalSubset(undefined);
  104. this._popContext();
  105. }
  106. this._characterData = true;
  107. this._parameterEntities = false;
  108. const values = node.entities.filter((entity) => entity.value).map((entity) => entity.value);
  109. for (const entity of node.entities.filter((entity) => entity.notationName)) {
  110. const reference = '&' + entity.localName + ';';
  111. if (values.some((value) => value.indexOf(reference) >= 0)) {
  112. this._error("Entity references unparsed entity '" + entity.localName + "'");
  113. }
  114. }
  115. if (internalSubset) {
  116. this._expect(']');
  117. this._whitespace(0);
  118. }
  119. this._expect('>');
  120. this._assert(this._pop().nodeType === xml.NodeType.DocumentType);
  121. }
  122. else {
  123. this._unexpected();
  124. }
  125. break;
  126. }
  127. case '/': {
  128. this._next();
  129. const name = this._name();
  130. this._assert(name !== null);
  131. this._whitespace(0);
  132. this._expect('>');
  133. const node = this._pop();
  134. const nodeName = node.prefix ? node.prefix + ':' + node.localName : node.localName;
  135. if (name !== nodeName) {
  136. this._error("Opening tag <" + nodeName + "> and ending tag </" + name + "> mismatch", this._start);
  137. }
  138. break;
  139. }
  140. default: {
  141. this._assert(this._stack.length > 1 || !this._document.documentElement);
  142. const name = this._name();
  143. this._assert(name !== null);
  144. this._assert(!name.startsWith('xmlns:'));
  145. const attributes = [];
  146. let whitespace = this._whitespace(0);
  147. if (whitespace) {
  148. while (this._char !== '/' && this._char !== '>') {
  149. if (!whitespace) {
  150. this._unexpected();
  151. }
  152. const position = this._position;
  153. const name = this._name();
  154. if (!name) {
  155. this._unexpected();
  156. }
  157. this._whitespace(0);
  158. this._expect('=');
  159. this._whitespace(0);
  160. const valuePosition = this._valuePosition;
  161. const value = this._attributeValue();
  162. attributes.push({
  163. qualifiedName: name,
  164. value: value,
  165. position: position,
  166. valuePosition: valuePosition
  167. });
  168. whitespace = this._whitespace(0);
  169. if (name === 'xmlns' && (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/' || value === 'http://www.w3.org/XML/1998/namespace')) {
  170. this._error("Invalid namespace '" + value + "'", valuePosition);
  171. }
  172. if (name === 'xml:space' && value !== 'preserve' && value !== 'default') {
  173. this._error("Unexpected xml:space attribute value '" + value + "'", position);
  174. }
  175. }
  176. }
  177. const namespaces = new Map();
  178. for (const entry of attributes.reverse()) {
  179. const name = entry.qualifiedName;
  180. const value = entry.value;
  181. const pair = xml.Utility.split(name);
  182. this._assert(name !== 'xmlns:');
  183. entry.prefix = pair[0];
  184. entry.localName = pair[1];
  185. if (entry.prefix !== null) {
  186. this._assert(entry.localName !== '');
  187. if (entry.prefix === 'xmlns' && entry.localName) {
  188. if (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/') {
  189. this._error("Invalid namespace '" + value + "'", entry.valuePosition);
  190. }
  191. if (entry.localName === 'xmlns' || (entry.localName === 'xml' && value !== 'http://www.w3.org/XML/1998/namespace') || (entry.localName !== 'xml' && value === 'http://www.w3.org/XML/1998/namespace')) {
  192. this._error("Invalid namespace prefix '" + entry.localName + "'", entry.position);
  193. }
  194. if (this._version === 0 && value.length === 0) {
  195. this._error("Invalid namespace declaration'", entry.position);
  196. }
  197. namespaces.set(entry.localName, value);
  198. }
  199. }
  200. else if (entry.localName === 'xmlns') {
  201. namespaces.set('', value);
  202. }
  203. }
  204. const pair = xml.Utility.split(name);
  205. const prefix = pair[0] || '';
  206. const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix);
  207. let element = null;
  208. const documentType = document.documentType;
  209. const elementType = documentType ? documentType.elements.getNamedItem(name) : null;
  210. if (namespaceURI !== null) {
  211. this._assert(name === ':' || (!name.endsWith(':') && !name.startsWith(':')));
  212. if (prefix && namespaceURI === '') {
  213. this._error("Invalid namespace prefix '" + prefix + "'", this._start);
  214. }
  215. element = document.createElementNS(namespaceURI, name);
  216. }
  217. else {
  218. this._assert((pair[0] === null && !name.endsWith(':')) || name === ':' || elementType !== null);
  219. element = document.createElement(name);
  220. }
  221. const parent = this._node();
  222. if (parent.nodeType === xml.NodeType.Document && parent.documentElement !== null) {
  223. this._error('Duplicate document element', this._start);
  224. }
  225. this._appendChild(element);
  226. const keys = new Set();
  227. for (const attr of attributes) {
  228. const name = attr.qualifiedName;
  229. const prefix = attr.prefix || '';
  230. const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix);
  231. let attribute = null;
  232. if (namespaceURI) {
  233. attribute = document.createAttributeNS(namespaceURI, name);
  234. }
  235. else {
  236. const attributeType = elementType ? elementType.attributes.getNamedItem(name) : null;
  237. this._assert(name.indexOf(':') === -1 || attributeType);
  238. attribute = document.createAttribute(name);
  239. }
  240. const key = (attribute.namespaceURI || '') + '|' + attribute.localName;
  241. this._assert(!keys.has(key));
  242. keys.add(key);
  243. attribute.value = attr.value;
  244. attribute.ownerElement = element;
  245. element.setAttributeNode(attribute);
  246. }
  247. const close = this._match('/');
  248. this._expect('>');
  249. if (this._peek && this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) {
  250. return this._pop();
  251. }
  252. if (!close) {
  253. this._push(element);
  254. }
  255. break;
  256. }
  257. }
  258. break;
  259. }
  260. default: {
  261. while (this._char === undefined && this._context.length > 0) {
  262. this._popContext();
  263. }
  264. if (this._char === undefined) {
  265. if (this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) {
  266. this._assert(document.documentElement);
  267. const documentType = document.documentType;
  268. if (documentType) {
  269. delete documentType.parameterEntities;
  270. delete documentType.elements;
  271. }
  272. const value = this._pop();
  273. for (const key of Object.keys(this)) {
  274. if (key !== '_data' && key !== '_callback' && key !== '_entities' && !key.startsWith('_name')) {
  275. delete this[key];
  276. }
  277. }
  278. return value;
  279. }
  280. this._unexpected();
  281. }
  282. const node = this._node();
  283. if (node.nodeType === xml.NodeType.Element) {
  284. const documentType = document.documentType;
  285. const name = node.prefix ? node.prefix + ':' + node.localName : node.localName;
  286. const elementType = documentType ? documentType.elements.getNamedItem(name) : null;
  287. this._characterData = elementType ? elementType.characterData : false;
  288. this._seek(this._position);
  289. const data = [];
  290. while (this._char !== '<' && this._char !== undefined) {
  291. if (this._char === ']' && this._match(']]>')) {
  292. this._unexpected();
  293. }
  294. data.push(this._content());
  295. if (data.length > 65536) {
  296. this._error('Invalid character data buffer size.');
  297. }
  298. }
  299. if (data.length > 0) {
  300. const content = data.splice(0, data.length).join('');
  301. if (content.trim().length > 0) {
  302. const node = document.createTextNode(content);
  303. this._appendChild(node);
  304. }
  305. }
  306. continue;
  307. }
  308. if (!this._whitespace(0)) {
  309. this._unexpected();
  310. }
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. _internalSubset(terminal) {
  317. for (;;) {
  318. this._start = this._position;
  319. switch (this._char) {
  320. case '<': {
  321. this._next();
  322. switch (this._char) {
  323. case '?': {
  324. this._processingInstruction();
  325. break;
  326. }
  327. case '!': {
  328. this._next();
  329. if (this._match('--')) {
  330. this._parameterEntities = false;
  331. this._characterData = true;
  332. this._comment();
  333. this._parameterEntities = true;
  334. }
  335. else if (this._match('ENTITY')) {
  336. const documentType = this._node();
  337. this._assert(documentType.nodeType === xml.NodeType.DocumentType);
  338. this._parameterEntities = false;
  339. this._whitespace(1);
  340. const parameter = this._char === '%';
  341. if (parameter) {
  342. this._next();
  343. this._whitespace(1);
  344. }
  345. this._parameterEntities = true;
  346. const name = this._entityName();
  347. const node = documentType.createEntity(name);
  348. let whitespace = this._whitespace(0);
  349. if (whitespace && (this._char === '"' || this._char === "'")) {
  350. node.value = this._entityValue();
  351. this._whitespace(0);
  352. }
  353. else {
  354. if (whitespace && this._match('SYSTEM')) {
  355. this._whitespace(1);
  356. node.systemId = this._systemLiteral();
  357. whitespace = this._whitespace(0);
  358. }
  359. else if (whitespace && this._match('PUBLIC')) {
  360. this._whitespace(1);
  361. node.publicId = this._pubidLiteral();
  362. this._whitespace(1);
  363. node.systemId = this._systemLiteral();
  364. whitespace = this._whitespace(0);
  365. }
  366. else {
  367. this._unexpected();
  368. }
  369. if (whitespace && !parameter) {
  370. if (this._match('NDATA')) {
  371. this._whitespace(1);
  372. const name = this._name();
  373. this._assert(name !== null);
  374. node.notationName = name;
  375. this._whitespace(0);
  376. }
  377. }
  378. }
  379. this._expect('>');
  380. if (parameter) {
  381. documentType.parameterEntities.setNamedItem(node);
  382. }
  383. else {
  384. this._appendChild(node);
  385. }
  386. }
  387. else if (this._match('ELEMENT')) {
  388. const documentType = this._node();
  389. this._assert(documentType.nodeType === xml.NodeType.DocumentType);
  390. this._whitespace(1);
  391. const name = this._name();
  392. this._assert(name !== null);
  393. this._whitespace(1);
  394. const elementType = this._elementType(name);
  395. if (this._match('EMPTY')) {
  396. this._whitespace(0);
  397. }
  398. else if (this._match('ANY')) {
  399. this._whitespace(0);
  400. }
  401. else {
  402. this._expect('(');
  403. this._whitespace(0);
  404. if (this._match('#PCDATA')) {
  405. elementType.characterData = true;
  406. this._whitespace(0);
  407. if (this._match(')')) {
  408. this._match('*');
  409. }
  410. else {
  411. this._whitespace(0);
  412. while (this._match('|')) {
  413. this._whitespace(0);
  414. const name = this._name();
  415. this._assert(name);
  416. this._whitespace(0);
  417. }
  418. this._expect(')*');
  419. }
  420. }
  421. else {
  422. this._elementChildren();
  423. }
  424. }
  425. this._whitespace(0);
  426. this._expect('>');
  427. }
  428. else if (this._match('ATTLIST')) {
  429. const documentType = this._node();
  430. this._assert(documentType.nodeType === xml.NodeType.DocumentType);
  431. this._whitespace(1);
  432. const name = this._name();
  433. this._assert(name !== null);
  434. const elementType = this._elementType(name);
  435. while (this._whitespace(0)) {
  436. const attributeType = this._attributeDefinition();
  437. if (!attributeType) {
  438. break;
  439. }
  440. elementType.attributes.setNamedItem(attributeType);
  441. }
  442. this._whitespace(0);
  443. this._expect('>');
  444. }
  445. else if (this._match('NOTATION')) {
  446. this._assert(this._nodeType() === xml.NodeType.DocumentType);
  447. const notation = { systemId: null, publicId: null };
  448. this._whitespace(1);
  449. notation.name = this._entityName();
  450. let whitespace = this._whitespace(0);
  451. if (whitespace && this._match('SYSTEM')) {
  452. this._whitespace(1);
  453. notation.systemId = this._systemLiteral();
  454. whitespace = this._whitespace(0);
  455. }
  456. if (whitespace && this._match('PUBLIC')) {
  457. this._whitespace(1);
  458. notation.publicId = this._pubidLiteral();
  459. if (this._whitespace(0) && (this._char === '"') || this._char === "'") {
  460. notation.systemId = this._systemLiteral();
  461. this._whitespace(0);
  462. }
  463. }
  464. this._assert(notation.systemId || notation.publicId);
  465. this._expect('>');
  466. }
  467. else if (this._match('[')) {
  468. this._whitespace(0);
  469. if (this._match('INCLUDE')) {
  470. this._assert(this._context.length > 0);
  471. this._whitespace(0);
  472. this._expect('[');
  473. this._internalSubset(']');
  474. this._expect(']]>');
  475. }
  476. else if (this._match('IGNORE')) {
  477. this._whitespace(0);
  478. this._expect('[');
  479. this._ignoreSectContents();
  480. }
  481. }
  482. else {
  483. this._unexpected();
  484. }
  485. break;
  486. }
  487. default: {
  488. break;
  489. }
  490. }
  491. break;
  492. }
  493. case '%': {
  494. this._resolveParameterEntityReference();
  495. break;
  496. }
  497. default: {
  498. if (this._char === terminal) {
  499. return;
  500. }
  501. if (!this._whitespace(0)) {
  502. this._unexpected();
  503. }
  504. break;
  505. }
  506. }
  507. }
  508. }
  509. _ignoreSectContents() {
  510. while (!this._match(']]>')) {
  511. if (this._match('<![')) {
  512. this._ignoreSectContents();
  513. }
  514. else {
  515. this._next();
  516. }
  517. }
  518. }
  519. _push(value) {
  520. this._stack.push(value);
  521. }
  522. _pop() {
  523. return this._stack.pop();
  524. }
  525. _node() {
  526. return this._stack[this._stack.length - 1];
  527. }
  528. _document() {
  529. return this._stack[0];
  530. }
  531. _nodeType() {
  532. return this._node().nodeType;
  533. }
  534. _appendChild(newChild) {
  535. return this._node().appendChild(newChild);
  536. }
  537. _lookupNamespaceURI(prefix) {
  538. return this._node().lookupNamespaceURI(prefix);
  539. }
  540. _name() {
  541. const position = this._position;
  542. const name = [];
  543. const c = this._char.codePointAt(0);
  544. if (this._nameStartCharRegExp.test(this._char) || (c >= 0x10000 && c <= 0xEFFFF)) {
  545. name.push(this._char);
  546. this._next();
  547. if (this._char !== undefined) {
  548. let c = this._char.codePointAt(0);
  549. while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) {
  550. name.push(this._char);
  551. this._next();
  552. if (this._char === undefined || this._implicitSpace) {
  553. break;
  554. }
  555. c = this._char.codePointAt(0);
  556. }
  557. }
  558. }
  559. if (name.length > 0) {
  560. return name.join('');
  561. }
  562. this._seek(position);
  563. return null;
  564. }
  565. _nmtoken() {
  566. const position = this._position;
  567. const name = [];
  568. let c = this._char.codePointAt(0);
  569. while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) {
  570. name.push(this._char);
  571. this._next();
  572. if (this._char === undefined) {
  573. break;
  574. }
  575. c = this._char.codePointAt(0);
  576. }
  577. if (name.length > 0) {
  578. return name.join('');
  579. }
  580. this._seek(position);
  581. return null;
  582. }
  583. _entityName() {
  584. const position = this._position;
  585. const name = this._name();
  586. if (name === null) {
  587. this._error('Expected entity name', position);
  588. }
  589. if (!name.endsWith(':') && name.indexOf(':') !== -1) {
  590. this._error('Invalid colon in entity name', position);
  591. }
  592. return name;
  593. }
  594. _entityValue() {
  595. const quote = this._char;
  596. this._parameterEntities = false;
  597. this._characterData = true;
  598. const decoder = this._decoder;
  599. const position = this._position;
  600. this._next();
  601. while (this._char !== quote) {
  602. if (this._char === undefined) {
  603. this._unexpected();
  604. }
  605. this._next();
  606. }
  607. const end = this._position;
  608. this._parameterEntities = true;
  609. this._seek(position);
  610. this._next();
  611. const data = [];
  612. while (this._position !== end || this._decoder !== decoder) {
  613. if (this._char === undefined) {
  614. this._unexpected();
  615. }
  616. if (this._char === '%') {
  617. if (this._context.length === 0) {
  618. this._error('Invalid parameter entity reference in internal subset');
  619. }
  620. this._assert();
  621. this._resolveParameterEntityReference();
  622. continue;
  623. }
  624. if (this._char === '&') {
  625. data.push(this._entityReference());
  626. this._expect(';');
  627. continue;
  628. }
  629. data.push(this._char);
  630. this._next();
  631. }
  632. this._next();
  633. this._parameterEntities = true;
  634. this._characterData = false;
  635. return data.join('');
  636. }
  637. _elementType(name) {
  638. const documentType = this._document().documentType;
  639. let elementType = documentType.elements.getNamedItem(name);
  640. if (!elementType) {
  641. elementType = { localName: name, characterData: false, attributes: new xml.NamedNodeMap() };
  642. documentType.elements.setNamedItem(elementType);
  643. }
  644. return elementType;
  645. }
  646. _elementChildren() {
  647. let separator = undefined;
  648. const choice = new Set();
  649. for (;;) {
  650. const name = this._name();
  651. if (name) {
  652. this._assert(separator !== '|' || !choice.has(name));
  653. choice.add(name);
  654. this._match('?') || this._match('*') || this._match('+');
  655. this._whitespace(0);
  656. }
  657. else if (this._match('(')) {
  658. this._elementChildren();
  659. this._whitespace(0);
  660. }
  661. else {
  662. this._unexpected();
  663. }
  664. if (this._match(')')) {
  665. break;
  666. }
  667. if (separator && separator !== this._char) {
  668. this._unexpected();
  669. }
  670. if (this._char !== '|' && this._char !== ',') {
  671. this._unexpected();
  672. }
  673. separator = this._char;
  674. this._next();
  675. this._whitespace(0);
  676. }
  677. this._match('?') || this._match('*') || this._match('+');
  678. }
  679. _attributeDefinition() {
  680. this._whitespace(0);
  681. const name = this._name();
  682. if (name) {
  683. this._whitespace(1);
  684. if (this._match('CDATA') || this._match('IDREFS') || this._match('IDREF') || this._match('ID') || this._match('ENTITIES') || this._match('ENTITY') || this._match('NMTOKENS') || this._match('NMTOKEN') ||
  685. this._enumeratedType()) {
  686. this._whitespace(1);
  687. if (!this._match('#REQUIRED') && !this._match('#IMPLIED')) {
  688. if (this._match('#FIXED')) {
  689. this._whitespace(1);
  690. }
  691. this._parameterEntities = false;
  692. this._attributeValue();
  693. this._parameterEntities = true;
  694. }
  695. return { localName: name };
  696. }
  697. this._assert(false);
  698. }
  699. return null;
  700. }
  701. _enumeratedType() {
  702. if (this._match('NOTATION')) {
  703. this._whitespace(1);
  704. this._expect('(');
  705. do {
  706. this._whitespace(0);
  707. const name = this._name();
  708. this._assert(name);
  709. this._whitespace(0);
  710. }
  711. while (this._match('|'));
  712. this._expect(')');
  713. return true;
  714. }
  715. if (this._match('(')) {
  716. do {
  717. this._whitespace(0);
  718. const name = this._nmtoken();
  719. this._assert(name);
  720. this._whitespace(0);
  721. }
  722. while (this._match('|'));
  723. this._expect(')');
  724. return true;
  725. }
  726. return false;
  727. }
  728. _content() {
  729. const c = this._char !== '&' ? this._char : this._resolveEntityReference();
  730. if (c === undefined) {
  731. return '';
  732. }
  733. const code = c.codePointAt(0);
  734. if ((!this._charRegExp.test(c) && (code < 0x10000 || c > 0x10FFFF))) {
  735. this._unexpected();
  736. }
  737. this._next();
  738. return c;
  739. }
  740. _attributeValue() {
  741. const quote = this._char;
  742. if (quote !== '"' && quote !== "'") {
  743. this._unexpected();
  744. }
  745. this._characterData = true;
  746. const decoder = this._decoder;
  747. const position = this._position;
  748. this._next();
  749. while (this._char !== quote) {
  750. if (this._char === undefined || this._char === '<') {
  751. this._unexpected();
  752. }
  753. this._next();
  754. }
  755. const end = this._position;
  756. this._characterData = false;
  757. this._seek(position);
  758. this._next();
  759. const data = [];
  760. while (this._position !== end || this._decoder !== decoder) {
  761. if (this._char === undefined && this._context.length > 0) {
  762. this._popContext();
  763. continue;
  764. }
  765. if (this._char === '<') {
  766. this._unexpected();
  767. }
  768. data.push(this._content());
  769. if (data.length > 65536) {
  770. this._error('Invalid character data buffer size.');
  771. }
  772. }
  773. this._characterData = true;
  774. this._next();
  775. return data.join('');
  776. }
  777. _validateNamespace(value) {
  778. if (value && (value.startsWith('#') || value.indexOf(':') === -1)) {
  779. return false;
  780. }
  781. if (this._version > 0) {
  782. return true;
  783. }
  784. return /^[A-Za-z0-9-._~:/?#[\]@!$&'()*+,;%=]*$/.exec(value) !== null;
  785. }
  786. _pubidLiteral() {
  787. const quote = this._char;
  788. if (quote !== '"' && quote !== "'") {
  789. this._unexpected();
  790. }
  791. this._next();
  792. const data = [];
  793. while (this._char !== quote) {
  794. if (/[a-zA-Z0-9-'()+,./:=?;!*#@$_%]/.test(this._char) || this._char === ' ' || this._char === '\r' || this._char === '\n') {
  795. data.push(this._char);
  796. this._next();
  797. if (this._char === undefined) {
  798. this._unexpected();
  799. }
  800. continue;
  801. }
  802. this._unexpected();
  803. }
  804. this._next();
  805. return data.join('');
  806. }
  807. _systemLiteral() {
  808. const quote = this._char;
  809. if (quote !== '"' && quote !== "'") {
  810. this._unexpected();
  811. }
  812. this._next();
  813. const data = [];
  814. while (this._char !== quote) {
  815. data.push(this._char);
  816. this._next();
  817. if (this._char === undefined) {
  818. this._unexpected();
  819. }
  820. }
  821. this._next();
  822. const value = data.join('');
  823. if (value.indexOf('#') >= 0) {
  824. this._unexpected();
  825. }
  826. const match = /(.*\/)[^/]*/.exec(this._base);
  827. return (match ? match[1] : '') + value;
  828. }
  829. _terminal(terminal) {
  830. const data = [];
  831. while (!this._match(terminal)) {
  832. if (this._char === undefined) {
  833. this._unexpected();
  834. }
  835. const c = this._char.codePointAt(0);
  836. if (c !== 0x09 && c !== 0x0A && c !== 0x0D && (c < 0x20 || c > 0xD7FF) && (c < 0xE000 || c > 0xFFFD) && (c < 0x10000 || c > 0x10FFFF)) {
  837. this._unexpected();
  838. }
  839. data.push(this._char);
  840. this._next();
  841. }
  842. return data.join('');
  843. }
  844. _resolveParameterEntityReference() {
  845. const position = this._position;
  846. this._next();
  847. const name = this._name();
  848. this._assert(name !== null);
  849. if (this._char === ';') {
  850. const entity = this._document().documentType.parameterEntities.getNamedItem(name);
  851. if (entity) {
  852. const implicitSpace = !this._entity && !this._context.some((context) => context.entity);
  853. if (entity.systemId) {
  854. this._pushResource(entity.systemId, name, false);
  855. }
  856. else {
  857. this._pushString(entity.value, name, false);
  858. }
  859. if (implicitSpace) {
  860. this._implicitSpace = true;
  861. }
  862. return;
  863. }
  864. this._error("Undefined ENTITY '" + name + "'", position);
  865. }
  866. this._unexpected();
  867. }
  868. _resolveEntityReference() {
  869. const position = this._position;
  870. let entity = this._entityReference();
  871. const name = entity.substring(1, entity.length - 1);
  872. if (name.startsWith('#x')) {
  873. const value = parseInt(name.substring(2), 16);
  874. return String.fromCodePoint(value);
  875. }
  876. else if (name.startsWith('#')) {
  877. const value = parseInt(name.substring(1), 10);
  878. return String.fromCodePoint(value);
  879. }
  880. else if (this._entities.has(name)) {
  881. return this._entities.get(name);
  882. }
  883. const documentType = this._document().documentType;
  884. entity = documentType ? documentType.entities.getNamedItem(name) : null;
  885. if (entity) {
  886. if (entity.systemId) {
  887. this._pushResource(entity.systemId, name, true);
  888. }
  889. else {
  890. this._pushString(entity.value, name, true);
  891. }
  892. }
  893. else if (this._context.length !== 0 || !documentType || documentType.parameterEntities.length === 0) {
  894. this._error("Undefined ENTITY '" + name + "'", position);
  895. }
  896. return undefined;
  897. }
  898. /* eslint-disable consistent-return */
  899. _entityReference() {
  900. if (this._char === '&') {
  901. const position = this._position;
  902. this._next();
  903. if (this._match('#x')) {
  904. const data = [];
  905. while (/[0-9a-fA-F]/.test(this._char)) {
  906. data.push(this._char);
  907. this._next();
  908. if (this._char === undefined) {
  909. this._unexpected();
  910. }
  911. }
  912. this._assert(this._char === ';');
  913. if (data.length > 0) {
  914. const text = data.join('');
  915. const value = parseInt(text, 16);
  916. this._assert(value <= 0x10FFFF, "Invalid value '&#x" + text + ";'", position);
  917. return '&#x' + text + ';';
  918. }
  919. }
  920. else if (this._match('#')) {
  921. const data = [];
  922. while (/[0-9]/.test(this._char)) {
  923. data.push(this._char);
  924. this._next();
  925. if (this._char === undefined) {
  926. this._unexpected();
  927. }
  928. }
  929. this._assert(this._char === ';');
  930. if (data.length > 0) {
  931. const text = data.join('');
  932. const value = parseInt(text, 10);
  933. this._assert(value <= 0x10FFFF, "Invalid value '&#" + text + ";'", position);
  934. return '&#' + text + ';';
  935. }
  936. }
  937. else {
  938. const name = this._name();
  939. this._assert(name !== null);
  940. this._assert(this._char === ';');
  941. return '&' + name + ';';
  942. }
  943. }
  944. this._unexpected();
  945. }
  946. /* eslint-enable consistent-return */
  947. _comment() {
  948. const data = this._terminal('--');
  949. const node = this._document().createComment(data);
  950. this._appendChild(node);
  951. this._expect('>');
  952. }
  953. _processingInstruction() {
  954. this._next();
  955. const name = this._entityName();
  956. let whitespace = this._char === '?' ? false : this._whitespace(1);
  957. const position = this._position;
  958. const data = this._terminal('?>');
  959. if (name.toLowerCase() === 'xml') {
  960. this._seek(position);
  961. this._assert(name === 'xml', "'" + name + "' must be lower case");
  962. this._assert(this._start === this._prolog, "Prolog must start with XML declaration", this._start);
  963. this._assert(typeof this._data !== 'string', 'Invalid text declaration', this._start);
  964. const obj = { version: '', encoding: '', standalone: 'no' };
  965. for (const name of Object.keys(obj)) {
  966. const expect = (name == 'version' && this._context.length === 0) || (name == 'encoding' && this._context.length > 0);
  967. if ((whitespace || expect) && (expect ? this._expect(name) : this._match(name))) {
  968. this._whitespace(0);
  969. this._expect('=');
  970. this._whitespace(0);
  971. obj[name] = this._attributeValue();
  972. whitespace = this._whitespace(0);
  973. }
  974. }
  975. this._expect('?>');
  976. obj.encoding = obj.encoding.toLowerCase();
  977. if (this._decoder.encoding && obj.encoding !== this._decoder.encoding) {
  978. const position = this._position;
  979. this._decoder = text.Decoder.open(this._data, obj.encoding);
  980. this._seek(position);
  981. }
  982. if (obj.version.length > 0) {
  983. const match = /^(\d)\.(\d)$/.exec(obj.version);
  984. this._assert(match && match[1] === '1', "Invalid XML version '" + obj.version + "'");
  985. const version = Number.parseInt(match[2], 10);
  986. if (version > this._version) {
  987. /* eslint-disable */
  988. this._charRegExp = /[\x01-\uD7FF\uE000-\uFFFD]/;
  989. /* eslint-enable */
  990. this._version = version;
  991. }
  992. this._assert(this._context.length === 0 || this._context.some((context) => context.version >= this._version));
  993. }
  994. this._assert(obj.standalone === 'no' || (obj.standalone === 'yes' && !this._entity && this._context.length === 0));
  995. this._standalone = obj.standalone === 'yes';
  996. }
  997. const node = this._document().createProcessingInstruction(name, data);
  998. this._appendChild(node);
  999. }
  1000. _whitespace(count) {
  1001. const position = this._position;
  1002. let index = 0;
  1003. if (this._implicitSpace) {
  1004. index++;
  1005. this._implicitSpace = false;
  1006. }
  1007. while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t' || (this._version > 0 && this._char === '\x85')) {
  1008. index++;
  1009. this._next();
  1010. }
  1011. if (index < count) {
  1012. this._seek(position);
  1013. this._unexpected();
  1014. }
  1015. return index > 0;
  1016. }
  1017. _pushResource(identifier, entity, stop) {
  1018. const content = this._callback(identifier);
  1019. this._pushBuffer(content, identifier, entity, stop);
  1020. }
  1021. _pushBuffer(data, base, entity, stop) {
  1022. const signature = text.Decoder.open(data);
  1023. const decoder = signature.encoding === 'utf-8' ? text.Decoder.open(data, 'utf-8') : signature;
  1024. this._pushContext(decoder, data, base, entity, stop, false);
  1025. this._data = data;
  1026. }
  1027. _pushString(value, entity, stop) {
  1028. const decoder = text.Decoder.open(value);
  1029. this._pushContext(decoder, value, this._base, entity, stop);
  1030. }
  1031. _pushContext(decoder, data, base, entity, stop) {
  1032. if (this._context.some((context) => context && context.base === base && context.entity === entity)) {
  1033. this._assert(!entity, "Recursive entity '" + entity + "'");
  1034. this._assert(!base, "Recursive base '" + base + "'");
  1035. }
  1036. if (base.length !== 0 || entity.length !== 0) {
  1037. this._context.push(this._state);
  1038. }
  1039. this._stop = stop;
  1040. this._entity = entity;
  1041. this._base = base;
  1042. this._data = data;
  1043. this._decoder = decoder;
  1044. this._prolog = this._decoder.position;
  1045. this._char = '';
  1046. this._next();
  1047. }
  1048. _popContext() {
  1049. const entity = this._entity;
  1050. this._state = this._context.pop();
  1051. if (entity) {
  1052. this._expect(';');
  1053. this._implicitSpace = !this._context.some((context) => context.entity);
  1054. }
  1055. }
  1056. get _state() {
  1057. return {
  1058. base: this._base,
  1059. data: this._data,
  1060. decoder: this._decoder,
  1061. position: this._position,
  1062. version: this._version,
  1063. entity: this._entity,
  1064. prolog: this._prolog,
  1065. stop: this._stop,
  1066. };
  1067. }
  1068. set _state(value) {
  1069. this._stop = value.stop;
  1070. this._base = value.base;
  1071. this._data = value.data;
  1072. this._decoder = value.decoder;
  1073. this._seek(value.position);
  1074. this._version = value.version;
  1075. this._entity = value.entity;
  1076. this._prolog = value.prolog;
  1077. }
  1078. _next() {
  1079. if (this._char === undefined) {
  1080. this._unexpected();
  1081. }
  1082. this._position = this._decoder.position;
  1083. this._char = this._decoder.decode();
  1084. this._implicitSpace = false;
  1085. if (this._parameterEntities && this._char === '%' && (this._entity || this._base)) {
  1086. this._resolveParameterEntityReference();
  1087. }
  1088. if (!this._characterData) {
  1089. if (this._char === '&' && (this._entity || this._base)) {
  1090. const c = this._resolveEntityReference();
  1091. if (c !== undefined) {
  1092. this._char = c;
  1093. }
  1094. }
  1095. }
  1096. if (this._char === '\uffff' || this._char === '\ufffe' || (this._version > 0 && this._char >= '\x7f' && this._char <= '\x9f' && this._char != '\x85')) {
  1097. this._unexpected();
  1098. }
  1099. if (this._char === undefined) {
  1100. if (!this._stop && this._context.length > 0) {
  1101. this._popContext();
  1102. }
  1103. }
  1104. }
  1105. _seek(position) {
  1106. this._decoder.position = position;
  1107. this._char = '';
  1108. this._next();
  1109. }
  1110. _expect(value) {
  1111. if (!this._match(value)) {
  1112. this._unexpected();
  1113. }
  1114. return true;
  1115. }
  1116. _match(value) {
  1117. if (this._char !== value[0]) {
  1118. return false;
  1119. }
  1120. if (value.length === 1) {
  1121. this._next();
  1122. return true;
  1123. }
  1124. if (this._context.length === 0) {
  1125. const position = this._position;
  1126. for (let i = 0; i < value.length; i++) {
  1127. if (this._char !== value[i]) {
  1128. this._seek(position);
  1129. return false;
  1130. }
  1131. this._next();
  1132. }
  1133. return true;
  1134. }
  1135. const context = Array.from(this._context);
  1136. const state = this._state;
  1137. for (let i = 0; i < value.length; i++) {
  1138. if (this._char !== value[i]) {
  1139. this._context = context;
  1140. this._state = state;
  1141. return false;
  1142. }
  1143. this._next();
  1144. }
  1145. return true;
  1146. }
  1147. _assert(value, message, position) {
  1148. if (value === false || value === undefined || value === null) {
  1149. this._error(message, position);
  1150. }
  1151. }
  1152. _error(message, position) {
  1153. if (position) {
  1154. this._parameterEntities = false;
  1155. this._characterData = true;
  1156. this._seek(position);
  1157. }
  1158. if (message) {
  1159. throw new xml.Error(message + this._location());
  1160. }
  1161. this._unexpected();
  1162. }
  1163. _unexpected() {
  1164. let c = this._char;
  1165. if (c === undefined) {
  1166. throw new xml.Error('Unexpected end of XML input.');
  1167. }
  1168. else if (c === '"') {
  1169. c = 'string';
  1170. }
  1171. else if ((c >= '0' && c <= '9') || c === '-') {
  1172. c = 'number';
  1173. }
  1174. else {
  1175. if (c < ' ' || c > '\x7F') {
  1176. c = c.codePointAt(0);
  1177. if (c < 0x0100) {
  1178. c = '\\x' + ('0' + c.toString(16)).slice(-2);
  1179. }
  1180. else if (c < 0x010000) {
  1181. c = '\\u' + ('000' + c.toString(16)).slice(-4);
  1182. }
  1183. else {
  1184. c = '\\u' + ('00000' + c.toString(16)).slice(-6);
  1185. }
  1186. }
  1187. c = "token '" + c + "'";
  1188. }
  1189. this._error('Unexpected ' + c);
  1190. }
  1191. _location() {
  1192. while (typeof this._data === 'string') {
  1193. this._popContext();
  1194. }
  1195. this._parameterEntities = false;
  1196. this._characterData = true;
  1197. let line = 1;
  1198. let column = 1;
  1199. this._decoder.position = 0;
  1200. let c;
  1201. do {
  1202. if (this._decoder.position === this._position) {
  1203. break;
  1204. }
  1205. c = this._decoder.decode();
  1206. if (c === '\n') {
  1207. line++;
  1208. column = 1;
  1209. }
  1210. else {
  1211. column++;
  1212. }
  1213. }
  1214. while (c !== undefined);
  1215. return ' at ' + (this._base ? this._base + ':' : '') + line.toString() + ':' + column.toString() + '.';
  1216. }
  1217. };
  1218. xml.NodeList = class extends Array {
  1219. constructor() {
  1220. super();
  1221. }
  1222. item(index) {
  1223. return this[index] || null;
  1224. }
  1225. };
  1226. xml.Node = class {
  1227. constructor(document, nodeType) {
  1228. this._ownerDocument = document;
  1229. this._nodeType = nodeType;
  1230. this._childNodes = new xml.NodeList();
  1231. }
  1232. get ownerDocument() {
  1233. return this._ownerDocument;
  1234. }
  1235. get nodeType() {
  1236. return this._nodeType;
  1237. }
  1238. get localName() {
  1239. throw new xml.Error('Not implemented.');
  1240. }
  1241. get namespaceURI() {
  1242. return null;
  1243. }
  1244. get childNodes() {
  1245. return this._childNodes;
  1246. }
  1247. get parentNode() {
  1248. return this._parentNode;
  1249. }
  1250. set parentNode(value) {
  1251. this._parentNode = value;
  1252. }
  1253. get firstChild() {
  1254. return this._firstChild;
  1255. }
  1256. set firstChild(value) {
  1257. this._firstChild = value;
  1258. }
  1259. get lastChild() {
  1260. return this._lastChild || null;
  1261. }
  1262. set lastChild(value) {
  1263. this._lastChild = value;
  1264. }
  1265. get previousSibling() {
  1266. return this._previousSibling;
  1267. }
  1268. set previousSibling(value) {
  1269. this._previousSibling = value;
  1270. }
  1271. get nextSibling() {
  1272. return this._nextSibling;
  1273. }
  1274. set nextSibling(value) {
  1275. this._nextSibling = value;
  1276. }
  1277. appendChild(newChild) {
  1278. this.firstChild = this.firstChild || newChild;
  1279. newChild.previousSibling = this.lastChild;
  1280. if (newChild.previousSibling) {
  1281. newChild.previousSibling.nextSibling = newChild;
  1282. }
  1283. this.lastChild = newChild;
  1284. this.childNodes[this.childNodes.length] = newChild;
  1285. newChild.parentNode = this;
  1286. }
  1287. lookupNamespaceURI(prefix) {
  1288. switch (prefix) {
  1289. case 'xml':
  1290. return 'http://www.w3.org/XML/1998/namespace';
  1291. case 'xmlns':
  1292. return 'http://www.w3.org/2000/xmlns/';
  1293. default:
  1294. return null;
  1295. }
  1296. }
  1297. };
  1298. xml.Element = class extends xml.Node {
  1299. constructor(document, namespaceURI, qualifiedName) {
  1300. super(document, xml.NodeType.Element);
  1301. this._namespaces = new Map();
  1302. this._attributes = new xml.NamedNodeMap();
  1303. this._namespaceURI = namespaceURI;
  1304. if (namespaceURI === null) {
  1305. this._prefix = null;
  1306. this._localName = qualifiedName;
  1307. }
  1308. else {
  1309. const pair = xml.Utility.split(qualifiedName);
  1310. this._prefix = pair[0];
  1311. this._localName = pair[1];
  1312. }
  1313. }
  1314. get localName() {
  1315. return this._localName;
  1316. }
  1317. get prefix() {
  1318. return this._prefix;
  1319. }
  1320. get namespaceURI() {
  1321. return this._namespaceURI;
  1322. }
  1323. get attributes() {
  1324. return this._attributes;
  1325. }
  1326. get textContent() {
  1327. return this.childNodes.map((node) => node.nodeType === xml.NodeType.ProcessingInstruction || node.nodeType === xml.NodeType.Comment ? '' : node.textContent).join('');
  1328. }
  1329. getElementsByTagName(tagName) {
  1330. const list = new xml.NodeList();
  1331. let node = this.firstChild;
  1332. while (node) {
  1333. if (node.nodeType === xml.NodeType.Element && (tagName === '*' || tagName === (node.prefix ? node.prefix + ':' + node.localName : node.localName))) {
  1334. list.push(node);
  1335. }
  1336. node = node.nextSibling;
  1337. }
  1338. return list;
  1339. }
  1340. getAttribute(name) {
  1341. const node = this.getAttributeNode(name);
  1342. return node ? node.value || '' : '';
  1343. }
  1344. getAttributeNode(name) {
  1345. return this.attributes.getNamedItem(name);
  1346. }
  1347. setAttributeNode(node) {
  1348. const oldNode = this.attributes.setNamedItem(node);
  1349. if (node.namespaceURI === 'http://www.w3.org/2000/xmlns/') {
  1350. const prefix = node.prefix ? node.localName : '';
  1351. this._namespaces.set(prefix, node.value);
  1352. }
  1353. return oldNode;
  1354. }
  1355. lookupNamespaceURI(prefix) {
  1356. if (this._namespaces.has(prefix)) {
  1357. return this._namespaces.get(prefix);
  1358. }
  1359. if (this.parentNode) {
  1360. return this.parentNode.lookupNamespaceURI(prefix);
  1361. }
  1362. return super.lookupNamespaceURI(prefix);
  1363. }
  1364. };
  1365. xml.Attribute = class extends xml.Node {
  1366. constructor(document, namespaceURI, qualifiedName) {
  1367. super(document, xml.NodeType.Attribute);
  1368. this._namespaceURI = namespaceURI;
  1369. if (namespaceURI === null) {
  1370. this._prefix = null;
  1371. this._localName = qualifiedName;
  1372. }
  1373. else {
  1374. const pair = xml.Utility.split(qualifiedName);
  1375. this._prefix = pair[0];
  1376. this._localName = pair[1];
  1377. }
  1378. }
  1379. get ownerElement() {
  1380. return this._ownerElement;
  1381. }
  1382. set ownerElement(value) {
  1383. this._ownerElement = value;
  1384. }
  1385. get localName() {
  1386. return this._localName;
  1387. }
  1388. get prefix() {
  1389. return this._prefix;
  1390. }
  1391. get namespaceURI() {
  1392. return this._namespaceURI;
  1393. }
  1394. get value() {
  1395. return this._value;
  1396. }
  1397. set value(value) {
  1398. this._value = value;
  1399. }
  1400. };
  1401. xml.CharacterData = class extends xml.Node {
  1402. constructor(document, nodeType, data) {
  1403. super(document, nodeType);
  1404. this._data = data;
  1405. }
  1406. get data() {
  1407. return this._data;
  1408. }
  1409. get textContent() {
  1410. return this._data;
  1411. }
  1412. };
  1413. xml.Text = class extends xml.CharacterData {
  1414. constructor(document, data) {
  1415. super(document, xml.NodeType.Text, data);
  1416. }
  1417. get localName() {
  1418. return '#text';
  1419. }
  1420. };
  1421. xml.CDataSection = class extends xml.CharacterData {
  1422. constructor(document, data) {
  1423. super(document, xml.NodeType.CDATA, data);
  1424. }
  1425. };
  1426. xml.Entity = class extends xml.Node {
  1427. constructor(document, name) {
  1428. super(document, xml.NodeType.Entity);
  1429. this._name = name;
  1430. this._publicId = '';
  1431. this._systemId = '';
  1432. this._notationName = '';
  1433. this._value = '';
  1434. }
  1435. get localName() {
  1436. return this._name;
  1437. }
  1438. get publicId() {
  1439. return this._publicId;
  1440. }
  1441. set publicId(value) {
  1442. this._publicId = value;
  1443. }
  1444. get systemId() {
  1445. return this._systemId;
  1446. }
  1447. set systemId(value) {
  1448. this._systemId = value;
  1449. }
  1450. get notationName() {
  1451. return this._notationName;
  1452. }
  1453. set notationName(value) {
  1454. this._notationName = value;
  1455. }
  1456. set value(value) {
  1457. this._value = value;
  1458. }
  1459. get value() {
  1460. return this._value;
  1461. }
  1462. };
  1463. xml.ProcessingInstruction = class extends xml.Node {
  1464. constructor(document, target, data) {
  1465. super(document, xml.NodeType.ProcessingInstruction);
  1466. this._target = target;
  1467. this._data = data;
  1468. }
  1469. get localName() {
  1470. return this._target;
  1471. }
  1472. get target() {
  1473. return this._target;
  1474. }
  1475. get data() {
  1476. return this._data;
  1477. }
  1478. };
  1479. xml.Comment = class extends xml.CharacterData {
  1480. constructor(document, data) {
  1481. super(document, xml.NodeType.Comment, data);
  1482. }
  1483. get localName() {
  1484. return '#comment';
  1485. }
  1486. };
  1487. xml.Document = class extends xml.Node {
  1488. constructor() {
  1489. super(null, xml.NodeType.Document);
  1490. this._documentElement = null;
  1491. this._documentType = null;
  1492. }
  1493. get documentElement() {
  1494. return this._documentElement;
  1495. }
  1496. get documentType() {
  1497. return this._documentType;
  1498. }
  1499. appendChild(newChild) {
  1500. super.appendChild(newChild);
  1501. if (newChild.nodeType === xml.NodeType.Element) {
  1502. this._documentElement = newChild;
  1503. }
  1504. if (newChild.nodeType === xml.NodeType.DocumentType) {
  1505. this._documentType = newChild;
  1506. }
  1507. }
  1508. createElement(localName) {
  1509. return new xml.Element(this, null, localName);
  1510. }
  1511. createElementNS(namespaceURI, qualifiedName) {
  1512. return new xml.Element(this, namespaceURI, qualifiedName);
  1513. }
  1514. createAttribute(localName) {
  1515. return new xml.Attribute(this, null, localName);
  1516. }
  1517. createAttributeNS(namespaceURI, qualifiedName) {
  1518. return new xml.Attribute(this, namespaceURI, qualifiedName);
  1519. }
  1520. createTextNode(data) {
  1521. return new xml.Text(this, data);
  1522. }
  1523. createCDATASection(data) {
  1524. return new xml.CDataSection(this, data);
  1525. }
  1526. createProcessingInstruction(target, data) {
  1527. return new xml.ProcessingInstruction(this, target, data);
  1528. }
  1529. createComment(data) {
  1530. return new xml.Comment(this, data);
  1531. }
  1532. createDocumentType(qualifiedName, publicId, systemId) {
  1533. return new xml.DocumentType(this, qualifiedName, publicId, systemId);
  1534. }
  1535. };
  1536. xml.DocumentType = class extends xml.Node {
  1537. constructor(document, qualifiedName, publicId, systemId) {
  1538. super(document, xml.NodeType.DocumentType);
  1539. this._name = qualifiedName;
  1540. this._publicId = publicId;
  1541. this._systemId = systemId;
  1542. this._entities = new xml.NamedNodeMap();
  1543. }
  1544. get name() {
  1545. return this._name;
  1546. }
  1547. get publicId() {
  1548. return this._publicId;
  1549. }
  1550. get systemId() {
  1551. return this._systemId;
  1552. }
  1553. get entities() {
  1554. return this._entities;
  1555. }
  1556. appendChild(newChild) {
  1557. if (newChild.nodeType === xml.NodeType.Entity) {
  1558. this._entities.setNamedItem(newChild);
  1559. }
  1560. }
  1561. createEntity(name) {
  1562. return new xml.Entity(this.ownerDocument, name);
  1563. }
  1564. };
  1565. xml.NamedNodeMap = class extends Array {
  1566. getNamedItem(qualifiedName) {
  1567. for (let i = this.length - 1; i >= 0; i--) {
  1568. const node = this[i];
  1569. const key = node.prefix ? node.prefix + ':' + node.localName : node.localName;
  1570. if (qualifiedName == key) {
  1571. return node;
  1572. }
  1573. }
  1574. return null;
  1575. }
  1576. getNamedItemNS(namespaceURI, localName) {
  1577. for (let i = this.length - 1; i >= 0; i--) {
  1578. const node = this[i];
  1579. if (localName === node.localName && namespaceURI == node.namespaceURI) {
  1580. return node;
  1581. }
  1582. }
  1583. return null;
  1584. }
  1585. setNamedItem(node) {
  1586. const qualifiedName = node.prefix ? node.prefix + ':' + node.localName : node.localName;
  1587. for (let i = this.length - 1; i >= 0; i--) {
  1588. const node = this[i];
  1589. const key = node.prefix ? node.prefix + ':' + node.localName : node.localName;
  1590. if (qualifiedName == key) {
  1591. const oldNode = this[i];
  1592. this[i] = node;
  1593. return oldNode;
  1594. }
  1595. }
  1596. this.push(node);
  1597. return null;
  1598. }
  1599. };
  1600. xml.NodeType = {
  1601. None: 0,
  1602. Element: 1,
  1603. Attribute: 2,
  1604. Text: 3,
  1605. CDATA: 4,
  1606. EntityReference: 5,
  1607. Entity: 6,
  1608. ProcessingInstruction: 7,
  1609. Comment: 8,
  1610. Document: 9,
  1611. DocumentType: 10,
  1612. DocumentFragment: 11,
  1613. Notation: 12
  1614. };
  1615. xml.Utility = class {
  1616. static split(name) {
  1617. const index = name.indexOf(':');
  1618. if (index < 0 || index === name.length - 1) {
  1619. return [ null, name ];
  1620. }
  1621. const localName = name.substring(index + 1);
  1622. const c = localName.codePointAt(0);
  1623. if (localName.indexOf(':') !== -1 || !xml.Utility.nameStartCharRegExp.test(String.fromCodePoint(c)) && (c < 0x10000 || c > 0xEFFFF)) {
  1624. return [ null, name ];
  1625. }
  1626. const prefix = name.substring(0, index);
  1627. return [ prefix, localName ];
  1628. }
  1629. };
  1630. xml.Error = class extends Error {
  1631. constructor(message) {
  1632. super(message);
  1633. this.name = 'XML Error';
  1634. }
  1635. };
  1636. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  1637. module.exports.TextReader = xml.TextReader;
  1638. }