xml.js 62 KB

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