xml_spec.js 3.8 KB

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