xfa_serialize_data_spec.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 _xfa_object = require("../../core/xfa/xfa_object.js");
  24. var _data = require("../../core/xfa/data.js");
  25. var _som = require("../../core/xfa/som.js");
  26. var _parser = require("../../core/xfa/parser.js");
  27. describe("Data serializer", function () {
  28. it("should serialize data with an annotationStorage", function () {
  29. const xml = `
  30. <?xml version="1.0"?>
  31. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  32. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  33. <foo>bar</foo>
  34. <xfa:data>
  35. <Receipt>
  36. <Page>1</Page>
  37. <Detail PartNo="GS001">
  38. <Description>Giant Slingshot</Description>
  39. <Units>1</Units>
  40. <Unit_Price>250.00</Unit_Price>
  41. <Total_Price>250.00</Total_Price>
  42. <àé></àé>
  43. </Detail>
  44. <Page>2</Page>
  45. <Detail PartNo="RRB-LB">
  46. <Description>Road Runner Bait, large bag</Description>
  47. <Units>5</Units>
  48. <Unit_Price>12.00</Unit_Price>
  49. <Total_Price>60.00</Total_Price>
  50. </Detail>
  51. <Sub_Total>310.00</Sub_Total>
  52. <Tax>24.80</Tax>
  53. <Total_Price>334.80</Total_Price>
  54. </Receipt>
  55. </xfa:data>
  56. <bar>foo</bar>
  57. </xfa:datasets>
  58. </xdp:xdp>
  59. `;
  60. const root = new _parser.XFAParser().parse(xml);
  61. const data = root.datasets.data;
  62. const dataHandler = new _data.DataHandler(root, data);
  63. const storage = new Map();
  64. for (const [path, value] of [["Receipt.Detail[0].Units", "12&3"], ["Receipt.Detail[0].Unit_Price", "456>"], ["Receipt.Detail[0].Total_Price", "789"], ["Receipt.Detail[0].àé", "1011"], ["Receipt.Detail[1].PartNo", "foo-bar😀"], ["Receipt.Detail[1].Description", "hello world"]]) {
  65. storage.set((0, _som.searchNode)(root, data, path)[0][_xfa_object.$uid], {
  66. value
  67. });
  68. }
  69. const serialized = dataHandler.serialize(storage);
  70. 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>`;
  71. expect(serialized).toEqual(expected);
  72. });
  73. });