2
0

bind.js 15 KB

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