xml_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { SimpleXMLParser, XMLParserBase } from "../../src/core/xml_parser.js";
  16. import { parseXFAPath } from "../../src/core/core_utils.js";
  17. describe("XML", function () {
  18. describe("searchNode", function () {
  19. it("should search a node with a given path in xml tree", function () {
  20. const xml = `
  21. <a>
  22. <b>
  23. <c a="123"/>
  24. <d/>
  25. <e>
  26. <f>
  27. <g a="321"/>
  28. </f>
  29. </e>
  30. <c a="456"/>
  31. <c a="789"/>
  32. <h/>
  33. <c a="101112"/>
  34. </b>
  35. <h>
  36. <i/>
  37. <j/>
  38. <k>
  39. <g a="654"/>
  40. </k>
  41. </h>
  42. <b>
  43. <g a="987"/>
  44. <h/>
  45. <g a="121110"/>
  46. </b>
  47. </a>`;
  48. const root = new SimpleXMLParser({ hasAttributes: true }).parseFromString(
  49. xml
  50. ).documentElement;
  51. function getAttr(path) {
  52. return root.searchNode(parseXFAPath(path), 0).attributes[0].value;
  53. }
  54. expect(getAttr("b.g")).toEqual("321");
  55. expect(getAttr("e.f.g")).toEqual("321");
  56. expect(getAttr("e.g")).toEqual("321");
  57. expect(getAttr("g")).toEqual("321");
  58. expect(getAttr("h.g")).toEqual("654");
  59. expect(getAttr("b[0].g")).toEqual("321");
  60. expect(getAttr("b[1].g")).toEqual("987");
  61. expect(getAttr("b[1].g[0]")).toEqual("987");
  62. expect(getAttr("b[1].g[1]")).toEqual("121110");
  63. expect(getAttr("c")).toEqual("123");
  64. expect(getAttr("c[1]")).toEqual("456");
  65. expect(getAttr("c[2]")).toEqual("789");
  66. expect(getAttr("c[3]")).toEqual("101112");
  67. });
  68. it("should dump a xml tree", function () {
  69. const xml = `
  70. <a>
  71. <b>
  72. <c a="123"/>
  73. <d>hello</d>
  74. <e>
  75. <f>
  76. <g a="321"/>
  77. </f>
  78. </e>
  79. <c a="456"/>
  80. <c a="789"/>
  81. <h/>
  82. <c a="101112"/>
  83. </b>
  84. <h>
  85. <i/>
  86. <j/>
  87. <k>&#xA;W&#x1F602;rld&#xA;<g a="654"/>
  88. </k>
  89. </h>
  90. <b>
  91. <g a="987"/>
  92. <h/>
  93. <g a="121110"/>
  94. </b>
  95. </a>`;
  96. const root = new SimpleXMLParser({ hasAttributes: true }).parseFromString(
  97. xml
  98. ).documentElement;
  99. const buffer = [];
  100. root.dump(buffer);
  101. expect(buffer.join("").replace(/\s+/g, "")).toEqual(
  102. xml.replace(/\s+/g, "")
  103. );
  104. });
  105. });
  106. it("should parse processing instructions", function () {
  107. const xml = `
  108. <a>
  109. <?foo bar?>
  110. <?foo bar oof?>
  111. <?foo?>
  112. </a>`;
  113. const pi = [];
  114. class MyParser extends XMLParserBase {
  115. onPi(name, value) {
  116. pi.push([name, value]);
  117. }
  118. }
  119. new MyParser().parseXml(xml);
  120. expect(pi).toEqual([
  121. ["foo", "bar"],
  122. ["foo", "bar oof"],
  123. ["foo", ""],
  124. ]);
  125. });
  126. });