xfa_serialize_data_spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright 2021 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 { $uid } from "../../src/core/xfa/xfa_object.js";
  16. import { DataHandler } from "../../src/core/xfa/data.js";
  17. import { searchNode } from "../../src/core/xfa/som.js";
  18. import { XFAParser } from "../../src/core/xfa/parser.js";
  19. describe("Data serializer", function () {
  20. it("should serialize data with an annotationStorage", function () {
  21. const xml = `
  22. <?xml version="1.0"?>
  23. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  24. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  25. <foo>bar</foo>
  26. <xfa:data>
  27. <Receipt>
  28. <Page>1</Page>
  29. <Detail PartNo="GS001">
  30. <Description>Giant Slingshot</Description>
  31. <Units>1</Units>
  32. <Unit_Price>250.00</Unit_Price>
  33. <Total_Price>250.00</Total_Price>
  34. <àé></àé>
  35. </Detail>
  36. <Page>2</Page>
  37. <Detail PartNo="RRB-LB">
  38. <Description>Road Runner Bait, large bag</Description>
  39. <Units>5</Units>
  40. <Unit_Price>12.00</Unit_Price>
  41. <Total_Price>60.00</Total_Price>
  42. </Detail>
  43. <Sub_Total>310.00</Sub_Total>
  44. <Tax>24.80</Tax>
  45. <Total_Price>334.80</Total_Price>
  46. </Receipt>
  47. </xfa:data>
  48. <bar>foo</bar>
  49. </xfa:datasets>
  50. </xdp:xdp>
  51. `;
  52. const root = new XFAParser().parse(xml);
  53. const data = root.datasets.data;
  54. const dataHandler = new DataHandler(root, data);
  55. const storage = new Map();
  56. for (const [path, value] of [
  57. ["Receipt.Detail[0].Units", "12&3"],
  58. ["Receipt.Detail[0].Unit_Price", "456>"],
  59. ["Receipt.Detail[0].Total_Price", "789"],
  60. ["Receipt.Detail[0].àé", "1011"],
  61. ["Receipt.Detail[1].PartNo", "foo-bar😀"],
  62. ["Receipt.Detail[1].Description", "hello world"],
  63. ]) {
  64. storage.set(searchNode(root, data, path)[0][$uid], { value });
  65. }
  66. const serialized = dataHandler.serialize(storage);
  67. const expected = `<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"><foo>bar</foo><bar>foo</bar><xfa:data><Receipt><Page>1</Page><Detail PartNo="GS001"><Description>Giant Slingshot</Description><Units>12&amp;3</Units><Unit_Price>456&gt;</Unit_Price><Total_Price>789</Total_Price><\xC3\xA0\xC3\xA9>1011</\xC3\xA0\xC3\xA9></Detail><Page>2</Page><Detail PartNo="foo-bar&#x1F600;"><Description>hello world</Description><Units>5</Units><Unit_Price>12.00</Unit_Price><Total_Price>60.00</Total_Price></Detail><Sub_Total>310.00</Sub_Total><Tax>24.80</Tax><Total_Price>334.80</Total_Price></Receipt></xfa:data></xfa:datasets>`;
  68. expect(serialized).toEqual(expected);
  69. });
  70. });