data.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DataHandler = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. class DataHandler {
  29. constructor(root, data) {
  30. this.data = data;
  31. this.dataset = root.datasets || null;
  32. }
  33. serialize(storage) {
  34. const stack = [[-1, this.data[_xfa_object.$getChildren]()]];
  35. while (stack.length > 0) {
  36. const last = stack.at(-1);
  37. const [i, children] = last;
  38. if (i + 1 === children.length) {
  39. stack.pop();
  40. continue;
  41. }
  42. const child = children[++last[0]];
  43. const storageEntry = storage.get(child[_xfa_object.$uid]);
  44. if (storageEntry) {
  45. child[_xfa_object.$setValue](storageEntry);
  46. } else {
  47. const attributes = child[_xfa_object.$getAttributes]();
  48. for (const value of attributes.values()) {
  49. const entry = storage.get(value[_xfa_object.$uid]);
  50. if (entry) {
  51. value[_xfa_object.$setValue](entry);
  52. break;
  53. }
  54. }
  55. }
  56. const nodes = child[_xfa_object.$getChildren]();
  57. if (nodes.length > 0) {
  58. stack.push([-1, nodes]);
  59. }
  60. }
  61. const buf = [`<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">`];
  62. if (this.dataset) {
  63. for (const child of this.dataset[_xfa_object.$getChildren]()) {
  64. if (child[_xfa_object.$nodeName] !== "data") {
  65. child[_xfa_object.$toString](buf);
  66. }
  67. }
  68. }
  69. this.data[_xfa_object.$toString](buf);
  70. buf.push("</xfa:datasets>");
  71. return buf.join("");
  72. }
  73. }
  74. exports.DataHandler = DataHandler;