xfa_serialize_data_spec.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 _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. </Detail>
  43. <Page>2</Page>
  44. <Detail PartNo="RRB-LB">
  45. <Description>Road Runner Bait, large bag</Description>
  46. <Units>5</Units>
  47. <Unit_Price>12.00</Unit_Price>
  48. <Total_Price>60.00</Total_Price>
  49. </Detail>
  50. <Sub_Total>310.00</Sub_Total>
  51. <Tax>24.80</Tax>
  52. <Total_Price>334.80</Total_Price>
  53. </Receipt>
  54. </xfa:data>
  55. <bar>foo</bar>
  56. </xfa:datasets>
  57. </xdp:xdp>
  58. `;
  59. const root = new _parser.XFAParser().parse(xml);
  60. const data = root.datasets.data;
  61. const dataHandler = new _data.DataHandler(root, data);
  62. const storage = new Map();
  63. 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[1].PartNo", "foo-bar😀"], ["Receipt.Detail[1].Description", "hello world"]]) {
  64. storage.set((0, _som.searchNode)(root, data, path)[0][_xfa_object.$uid], {
  65. value
  66. });
  67. }
  68. const serialized = dataHandler.serialize(storage);
  69. 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></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>`;
  70. expect(serialized).toEqual(expected);
  71. });
  72. });