2
0

bind.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Binder = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. var _template = require("./template.js");
  29. var _som = require("./som.js");
  30. var _namespaces = require("./namespaces.js");
  31. var _util = require("../../shared/util.js");
  32. function createText(content) {
  33. const node = new _template.Text({});
  34. node[_xfa_object.$content] = content;
  35. return node;
  36. }
  37. class Binder {
  38. constructor(root) {
  39. this.root = root;
  40. this.datasets = root.datasets;
  41. if (root.datasets && root.datasets.data) {
  42. this.emptyMerge = false;
  43. this.data = root.datasets.data;
  44. } else {
  45. this.emptyMerge = true;
  46. this.data = new _xfa_object.XmlObject(_namespaces.NamespaceIds.datasets.id, "data");
  47. }
  48. this.root.form = this.form = root.template[_xfa_object.$clone]();
  49. }
  50. _isConsumeData() {
  51. return !this.emptyMerge && this._mergeMode;
  52. }
  53. _isMatchTemplate() {
  54. return !this._isConsumeData();
  55. }
  56. bind() {
  57. this._bindElement(this.form, this.data);
  58. return this.form;
  59. }
  60. getData() {
  61. return this.data;
  62. }
  63. _bindValue(formNode, data, picture) {
  64. if (formNode[_xfa_object.$hasSettableValue]()) {
  65. if (data[_xfa_object.$isDataValue]()) {
  66. const value = data[_xfa_object.$getDataValue]();
  67. formNode[_xfa_object.$setValue](createText(value));
  68. formNode[_xfa_object.$data] = data;
  69. } else if (formNode instanceof _template.Field && formNode.ui && formNode.ui.choiceList && formNode.ui.choiceList.open === "multiSelect") {
  70. const value = data[_xfa_object.$getChildren]().map(child => child[_xfa_object.$content].trim()).join("\n");
  71. formNode[_xfa_object.$setValue](createText(value));
  72. formNode[_xfa_object.$data] = data;
  73. } else if (this._isConsumeData()) {
  74. (0, _util.warn)(`XFA - Nodes haven't the same type.`);
  75. }
  76. } else if (!data[_xfa_object.$isDataValue]() || this._isMatchTemplate()) {
  77. this._bindElement(formNode, data);
  78. formNode[_xfa_object.$data] = data;
  79. } else {
  80. (0, _util.warn)(`XFA - Nodes haven't the same type.`);
  81. }
  82. }
  83. _findDataByNameToConsume(name, isValue, dataNode, global) {
  84. if (!name) {
  85. return null;
  86. }
  87. let generator, match;
  88. for (let i = 0; i < 3; i++) {
  89. generator = dataNode[_xfa_object.$getRealChildrenByNameIt](name, false, true);
  90. while (true) {
  91. match = generator.next().value;
  92. if (!match) {
  93. break;
  94. }
  95. if (isValue === match[_xfa_object.$isDataValue]()) {
  96. return match;
  97. }
  98. }
  99. if (dataNode[_xfa_object.$namespaceId] === _namespaces.NamespaceIds.datasets.id && dataNode[_xfa_object.$nodeName] === "data") {
  100. break;
  101. }
  102. dataNode = dataNode[_xfa_object.$getParent]();
  103. }
  104. if (!global) {
  105. return null;
  106. }
  107. generator = this.data[_xfa_object.$getRealChildrenByNameIt](name, false, false);
  108. while (true) {
  109. match = generator.next().value;
  110. if (!match) {
  111. break;
  112. }
  113. if (match[_xfa_object.$global]) {
  114. return match;
  115. }
  116. }
  117. generator = this.data[_xfa_object.$getAttributeIt](name, true);
  118. match = generator.next().value;
  119. if (match && match[_xfa_object.$isDataValue]()) {
  120. return match;
  121. }
  122. return null;
  123. }
  124. _setProperties(formNode, dataNode) {
  125. if (!formNode.hasOwnProperty("setProperty")) {
  126. return;
  127. }
  128. for (const {
  129. ref,
  130. target,
  131. connection
  132. } of formNode.setProperty.children) {
  133. if (connection) {
  134. continue;
  135. }
  136. if (!ref) {
  137. continue;
  138. }
  139. const [node] = (0, _som.searchNode)(this.root, dataNode, ref, false, false);
  140. if (!node) {
  141. (0, _util.warn)(`XFA - Invalid reference: ${ref}.`);
  142. continue;
  143. }
  144. if (!node[_xfa_object.$isDescendent](this.data)) {
  145. (0, _util.warn)(`XFA - Invalid node: must be a data node.`);
  146. continue;
  147. }
  148. const [targetNode] = (0, _som.searchNode)(this.root, formNode, target, false, false);
  149. if (!targetNode) {
  150. (0, _util.warn)(`XFA - Invalid target: ${target}.`);
  151. continue;
  152. }
  153. if (!targetNode[_xfa_object.$isDescendent](formNode)) {
  154. (0, _util.warn)(`XFA - Invalid target: must be a property or subproperty.`);
  155. continue;
  156. }
  157. const targetParent = targetNode[_xfa_object.$getParent]();
  158. if (targetNode instanceof _template.SetProperty || targetParent instanceof _template.SetProperty) {
  159. (0, _util.warn)(`XFA - Invalid target: cannot be a setProperty or one of its properties.`);
  160. continue;
  161. }
  162. if (targetNode instanceof _template.BindItems || targetParent instanceof _template.BindItems) {
  163. (0, _util.warn)(`XFA - Invalid target: cannot be a bindItems or one of its properties.`);
  164. continue;
  165. }
  166. const content = node[_xfa_object.$text]();
  167. const name = targetNode[_xfa_object.$nodeName];
  168. if (targetNode instanceof _xfa_object.XFAAttribute) {
  169. const attrs = Object.create(null);
  170. attrs[name] = content;
  171. const obj = Reflect.construct(Object.getPrototypeOf(targetParent).constructor, [attrs]);
  172. targetParent[name] = obj[name];
  173. continue;
  174. }
  175. if (!targetNode.hasOwnProperty(_xfa_object.$content)) {
  176. (0, _util.warn)(`XFA - Invalid node to use in setProperty`);
  177. continue;
  178. }
  179. targetNode[_xfa_object.$data] = node;
  180. targetNode[_xfa_object.$content] = content;
  181. targetNode[_xfa_object.$finalize]();
  182. }
  183. }
  184. _bindItems(formNode, dataNode) {
  185. if (!formNode.hasOwnProperty("items") || !formNode.hasOwnProperty("bindItems") || formNode.bindItems.isEmpty()) {
  186. return;
  187. }
  188. for (const item of formNode.items.children) {
  189. formNode[_xfa_object.$removeChild](item);
  190. }
  191. formNode.items.clear();
  192. const labels = new _template.Items({});
  193. const values = new _template.Items({});
  194. formNode[_xfa_object.$appendChild](labels);
  195. formNode.items.push(labels);
  196. formNode[_xfa_object.$appendChild](values);
  197. formNode.items.push(values);
  198. for (const {
  199. ref,
  200. labelRef,
  201. valueRef,
  202. connection
  203. } of formNode.bindItems.children) {
  204. if (connection) {
  205. continue;
  206. }
  207. if (!ref) {
  208. continue;
  209. }
  210. const nodes = (0, _som.searchNode)(this.root, dataNode, ref, false, false);
  211. if (!nodes) {
  212. (0, _util.warn)(`XFA - Invalid reference: ${ref}.`);
  213. continue;
  214. }
  215. for (const node of nodes) {
  216. if (!node[_xfa_object.$isDescendent](this.datasets)) {
  217. (0, _util.warn)(`XFA - Invalid ref (${ref}): must be a datasets child.`);
  218. continue;
  219. }
  220. const [labelNode] = (0, _som.searchNode)(this.root, node, labelRef, true, false);
  221. if (!labelNode) {
  222. (0, _util.warn)(`XFA - Invalid label: ${labelRef}.`);
  223. continue;
  224. }
  225. if (!labelNode[_xfa_object.$isDescendent](this.datasets)) {
  226. (0, _util.warn)(`XFA - Invalid label: must be a datasets child.`);
  227. continue;
  228. }
  229. const [valueNode] = (0, _som.searchNode)(this.root, node, valueRef, true, false);
  230. if (!valueNode) {
  231. (0, _util.warn)(`XFA - Invalid value: ${valueRef}.`);
  232. continue;
  233. }
  234. if (!valueNode[_xfa_object.$isDescendent](this.datasets)) {
  235. (0, _util.warn)(`XFA - Invalid value: must be a datasets child.`);
  236. continue;
  237. }
  238. const label = createText(labelNode[_xfa_object.$text]());
  239. const value = createText(valueNode[_xfa_object.$text]());
  240. labels[_xfa_object.$appendChild](label);
  241. labels.text.push(label);
  242. values[_xfa_object.$appendChild](value);
  243. values.text.push(value);
  244. }
  245. }
  246. }
  247. _bindOccurrences(formNode, matches, picture) {
  248. let baseClone;
  249. if (matches.length > 1) {
  250. baseClone = formNode[_xfa_object.$clone]();
  251. }
  252. this._bindValue(formNode, matches[0], picture);
  253. this._setProperties(formNode, matches[0]);
  254. this._bindItems(formNode, matches[0]);
  255. if (matches.length === 1) {
  256. return;
  257. }
  258. const parent = formNode[_xfa_object.$getParent]();
  259. const name = formNode[_xfa_object.$nodeName];
  260. const pos = parent[_xfa_object.$indexOf](formNode);
  261. for (let i = 1, ii = matches.length; i < ii; i++) {
  262. const match = matches[i];
  263. const clone = baseClone[_xfa_object.$clone]();
  264. clone.occur.min = 1;
  265. clone.occur.max = 1;
  266. clone.occur.initial = 1;
  267. parent[name].push(clone);
  268. parent[_xfa_object.$insertAt](pos + i, clone);
  269. this._bindValue(clone, match, picture);
  270. this._setProperties(clone, match);
  271. this._bindItems(clone, match);
  272. }
  273. }
  274. _createOccurrences(formNode) {
  275. if (!this.emptyMerge) {
  276. return;
  277. }
  278. const {
  279. occur
  280. } = formNode;
  281. if (!occur || occur.initial <= 1) {
  282. return;
  283. }
  284. const parent = formNode[_xfa_object.$getParent]();
  285. const name = formNode[_xfa_object.$nodeName];
  286. for (let i = 0, ii = occur.initial; i < ii; i++) {
  287. const clone = formNode[_xfa_object.$clone]();
  288. clone.occur.min = 1;
  289. clone.occur.max = 1;
  290. clone.occur.initial = 1;
  291. parent[name].push(clone);
  292. parent[_xfa_object.$appendChild](clone);
  293. }
  294. }
  295. _getOccurInfo(formNode) {
  296. const {
  297. occur
  298. } = formNode;
  299. const dataName = formNode.name;
  300. if (!occur || !dataName) {
  301. return [1, 1];
  302. }
  303. const max = occur.max === -1 ? Infinity : occur.max;
  304. return [occur.min, max];
  305. }
  306. _bindElement(formNode, dataNode) {
  307. const uselessNodes = [];
  308. this._createOccurrences(formNode);
  309. for (const child of formNode[_xfa_object.$getChildren]()) {
  310. if (child[_xfa_object.$data]) {
  311. continue;
  312. }
  313. if (this._mergeMode === undefined && child[_xfa_object.$nodeName] === "subform") {
  314. this._mergeMode = child.mergeMode === "consumeData";
  315. }
  316. let global = false;
  317. let picture = null;
  318. let ref = null;
  319. let match = null;
  320. if (child.bind) {
  321. switch (child.bind.match) {
  322. case "none":
  323. this._bindElement(child, dataNode);
  324. continue;
  325. case "global":
  326. global = true;
  327. break;
  328. case "dataRef":
  329. if (!child.bind.ref) {
  330. (0, _util.warn)(`XFA - ref is empty in node ${child[_xfa_object.$nodeName]}.`);
  331. this._bindElement(child, dataNode);
  332. continue;
  333. }
  334. ref = child.bind.ref;
  335. break;
  336. default:
  337. break;
  338. }
  339. if (child.bind.picture) {
  340. picture = child.bind.picture[_xfa_object.$content];
  341. }
  342. }
  343. const [min, max] = this._getOccurInfo(child);
  344. if (ref) {
  345. match = (0, _som.searchNode)(this.root, dataNode, ref, true, false);
  346. if (match === null) {
  347. match = (0, _som.createDataNode)(this.data, dataNode, ref);
  348. if (this._isConsumeData()) {
  349. match[_xfa_object.$consumed] = true;
  350. }
  351. match = [match];
  352. } else {
  353. if (this._isConsumeData()) {
  354. match = match.filter(node => !node[_xfa_object.$consumed]);
  355. }
  356. if (match.length > max) {
  357. match = match.slice(0, max);
  358. } else if (match.length === 0) {
  359. match = null;
  360. }
  361. if (match && this._isConsumeData()) {
  362. match.forEach(node => {
  363. node[_xfa_object.$consumed] = true;
  364. });
  365. }
  366. }
  367. } else {
  368. if (!child.name) {
  369. this._bindElement(child, dataNode);
  370. continue;
  371. }
  372. if (this._isConsumeData()) {
  373. const matches = [];
  374. while (matches.length < max) {
  375. const found = this._findDataByNameToConsume(child.name, child[_xfa_object.$hasSettableValue](), dataNode, global);
  376. if (!found) {
  377. break;
  378. }
  379. found[_xfa_object.$consumed] = true;
  380. matches.push(found);
  381. }
  382. match = matches.length > 0 ? matches : null;
  383. } else {
  384. match = dataNode[_xfa_object.$getRealChildrenByNameIt](child.name, false, false).next().value;
  385. if (!match) {
  386. match = new _xfa_object.XmlObject(dataNode[_xfa_object.$namespaceId], child.name);
  387. dataNode[_xfa_object.$appendChild](match);
  388. }
  389. match = [match];
  390. }
  391. }
  392. if (match) {
  393. if (match.length < min) {
  394. (0, _util.warn)(`XFA - Must have at least ${min} occurrences: ${formNode[_xfa_object.$nodeName]}.`);
  395. continue;
  396. }
  397. this._bindOccurrences(child, match, picture);
  398. } else if (min > 0) {
  399. this._setProperties(child, dataNode);
  400. this._bindItems(child, dataNode);
  401. this._bindElement(child, dataNode);
  402. } else {
  403. uselessNodes.push(child);
  404. }
  405. }
  406. uselessNodes.forEach(node => node[_xfa_object.$getParent]()[_xfa_object.$removeChild](node));
  407. }
  408. }
  409. exports.Binder = Binder;