xfa_parser_spec.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 _bind = require("../../core/xfa/bind.js");
  25. var _som = require("../../core/xfa/som.js");
  26. var _parser = require("../../core/xfa/parser.js");
  27. describe("XFAParser", function () {
  28. describe("Parse XFA", function () {
  29. it("should parse a xfa document and create an object to represent it", function () {
  30. const xml = `
  31. <?xml version="1.0"?>
  32. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" uuid="1234" invalid="foo">
  33. <config xmlns="http://www.xfa.org/schema/xci/3.1/">
  34. <present>
  35. <pdf name="hello">
  36. <adobeExtensionLevel>
  37. 7
  38. </adobeExtensionLevel>
  39. </pdf>
  40. <invalid><a>foobar</a></invalid>
  41. </present>
  42. <acrobat>
  43. <submitUrl>http://a.b.c</submitUrl>
  44. <acrobat7>
  45. <dynamicRender>
  46. forbidden
  47. </dynamicRender>
  48. </acrobat7>
  49. <autoSave>enabled</autoSave>
  50. <submitUrl>
  51. http://d.e.f
  52. </submitUrl>
  53. <submitUrl>http://g.h.i</submitUrl>
  54. <validate>foobar</validate>
  55. </acrobat>
  56. </config>
  57. <template baseProfile="full" xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  58. <extras>
  59. <float>1.23</float>
  60. <boolean>1</boolean>
  61. <integer>314</integer>
  62. <float>2.71</float>
  63. </extras>
  64. <subform>
  65. <proto>
  66. <area x="hello" y="-3.14in" relevant="-foo +bar" />
  67. <color value="111, 222, 123" />
  68. <color value="111, abc, 123" />
  69. <medium imagingBBox="1,2in,3.4cm,5.67px" />
  70. <medium imagingBBox="1,2in,-3cm,4px" />
  71. </proto>
  72. </subform>
  73. </template>
  74. </xdp:xdp>
  75. `;
  76. const attributes = {
  77. id: "",
  78. name: "",
  79. use: "",
  80. usehref: ""
  81. };
  82. const mediumAttributes = {
  83. id: "",
  84. long: 0,
  85. orientation: "portrait",
  86. short: 0,
  87. stock: "",
  88. trayIn: "auto",
  89. trayOut: "auto",
  90. use: "",
  91. usehref: ""
  92. };
  93. const colorAttributes = {
  94. cSpace: "SRGB",
  95. id: "",
  96. use: "",
  97. usehref: ""
  98. };
  99. const root = new _parser.XFAParser().parse(xml);
  100. const expected = {
  101. uuid: "1234",
  102. timeStamp: "",
  103. template: {
  104. baseProfile: "full",
  105. extras: { ...attributes,
  106. float: [{ ...attributes,
  107. $content: 1.23
  108. }, { ...attributes,
  109. $content: 2.71
  110. }],
  111. boolean: { ...attributes,
  112. $content: 1
  113. },
  114. integer: { ...attributes,
  115. $content: 314
  116. }
  117. },
  118. subform: {
  119. access: "open",
  120. allowMacro: 0,
  121. anchorType: "topLeft",
  122. colSpan: 1,
  123. columnWidths: [0],
  124. h: "",
  125. hAlign: "left",
  126. id: "",
  127. layout: "position",
  128. locale: "",
  129. maxH: 0,
  130. maxW: 0,
  131. mergeMode: "consumeData",
  132. minH: 0,
  133. minW: 0,
  134. name: "",
  135. presence: "visible",
  136. relevant: [],
  137. restoreState: "manual",
  138. scope: "name",
  139. use: "",
  140. usehref: "",
  141. w: "",
  142. x: 0,
  143. y: 0,
  144. proto: {
  145. area: { ...attributes,
  146. colSpan: 1,
  147. x: 0,
  148. y: -226.08,
  149. relevant: [{
  150. excluded: true,
  151. viewname: "foo"
  152. }, {
  153. excluded: false,
  154. viewname: "bar"
  155. }]
  156. },
  157. color: [{ ...colorAttributes,
  158. value: {
  159. r: 111,
  160. g: 222,
  161. b: 123
  162. }
  163. }, { ...colorAttributes,
  164. value: {
  165. r: 111,
  166. g: 0,
  167. b: 123
  168. }
  169. }],
  170. medium: [{ ...mediumAttributes,
  171. imagingBBox: {
  172. x: 1,
  173. y: 144,
  174. width: 96.3779527559055,
  175. height: 5.67
  176. }
  177. }, { ...mediumAttributes,
  178. imagingBBox: {
  179. x: -1,
  180. y: -1,
  181. width: -1,
  182. height: -1
  183. }
  184. }]
  185. }
  186. }
  187. },
  188. config: {
  189. acrobat: {
  190. acrobat7: {
  191. dynamicRender: {
  192. $content: "forbidden"
  193. }
  194. },
  195. autoSave: {
  196. $content: "enabled"
  197. },
  198. validate: {
  199. $content: "preSubmit"
  200. },
  201. submitUrl: [{
  202. $content: "http://a.b.c"
  203. }, {
  204. $content: "http://d.e.f"
  205. }, {
  206. $content: "http://g.h.i"
  207. }]
  208. },
  209. present: {
  210. pdf: {
  211. name: "hello",
  212. adobeExtensionLevel: {
  213. $content: 7
  214. }
  215. }
  216. }
  217. }
  218. };
  219. expect(root[_xfa_object.$dump]()).toEqual(expected);
  220. });
  221. it("should parse a xfa document and check namespaces", function () {
  222. const xml = `
  223. <?xml version="1.0"?>
  224. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  225. <config xmlns:foo="http:/www.foo.com" xmlns="http://www.xfa.org/schema/xci/3.1/">
  226. <present xmlns="http://www.mozilla.org">
  227. <pdf name="hello">
  228. <adobeExtensionLevel>
  229. 7
  230. </adobeExtensionLevel>
  231. </pdf>
  232. </present>
  233. <acrobat>
  234. <foo:submitUrl>http://a.b.c</foo:submitUrl>
  235. <submitUrl>http://c.b.a</submitUrl>
  236. </acrobat>
  237. </config>
  238. <template baseProfile="full" xmlns="http://www.allizom.org">
  239. <extras>
  240. <float>1.23</float>
  241. </extras>
  242. </template>
  243. </xdp:xdp>
  244. `;
  245. const root = new _parser.XFAParser().parse(xml);
  246. const expected = {
  247. uuid: "",
  248. timeStamp: "",
  249. config: {
  250. acrobat: {
  251. submitUrl: {
  252. $content: "http://c.b.a"
  253. }
  254. }
  255. }
  256. };
  257. expect(root[_xfa_object.$dump]()).toEqual(expected);
  258. });
  259. it("should parse a xfa document and apply some prototypes", function () {
  260. const xml = `
  261. <?xml version="1.0"?>
  262. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  263. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  264. <subform>
  265. <proto>
  266. <font id="id1" typeface="Foo" size="123pt" weight="bold" posture="italic">
  267. <fill>
  268. <color value="1,2,3"/>
  269. </fill>
  270. </font>
  271. </proto>
  272. <field>
  273. <font use="#id1"/>
  274. </field>
  275. <field>
  276. <font use="#id1" size="456pt" weight="bold" posture="normal">
  277. <fill>
  278. <color value="4,5,6"/>
  279. </fill>
  280. <extras id="id2"/>
  281. </font>
  282. </field>
  283. </subform>
  284. </template>
  285. </xdp:xdp>
  286. `;
  287. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  288. let font = root.template.subform.field[0].font;
  289. expect(font.typeface).toEqual("Foo");
  290. expect(font.overline).toEqual(0);
  291. expect(font.size).toEqual(123);
  292. expect(font.weight).toEqual("bold");
  293. expect(font.posture).toEqual("italic");
  294. expect(font.fill.color.value).toEqual({
  295. r: 1,
  296. g: 2,
  297. b: 3
  298. });
  299. expect(font.extras).toEqual(undefined);
  300. font = root.template.subform.field[1].font;
  301. expect(font.typeface).toEqual("Foo");
  302. expect(font.overline).toEqual(0);
  303. expect(font.size).toEqual(456);
  304. expect(font.weight).toEqual("bold");
  305. expect(font.posture).toEqual("normal");
  306. expect(font.fill.color.value).toEqual({
  307. r: 4,
  308. g: 5,
  309. b: 6
  310. });
  311. expect(font.extras.id).toEqual("id2");
  312. });
  313. it("should parse a xfa document with xhtml", function () {
  314. const xml = `
  315. <?xml version="1.0"?>
  316. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  317. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  318. <extras>
  319. <text>
  320. <body xmlns="http://www.w3.org/1999/xhtml">
  321. <p style="foo: bar; text-indent:0.5in; line-height:11px;bar:foo;tab-stop: left 0.5in">
  322. The first line of this paragraph is indented a half-inch.<br/>
  323. Successive lines are not indented.<br/>
  324. This is the last line of the paragraph.<br/>
  325. </p>
  326. </body>
  327. </text>
  328. </extras>
  329. </template>
  330. </xdp:xdp>
  331. `;
  332. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  333. const p = root.template.extras.text.$content[_xfa_object.$getChildren]()[0];
  334. expect(p.style).toEqual("text-indent:0.5in;line-height:11px;tab-stop:left 0.5in");
  335. expect(p[_xfa_object.$text]()).toEqual(["The first line of this paragraph is indented a half-inch.\n", "Successive lines are not indented.\n", "This is the last line of the paragraph.\n"].join(""));
  336. });
  337. it("should parse a xfa document and apply some prototypes with cycle", function () {
  338. const xml = `
  339. <?xml version="1.0"?>
  340. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  341. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  342. <subform>
  343. <proto>
  344. <subform id="id1">
  345. <subform use="#id1"/>
  346. </subform>
  347. </proto>
  348. </subform>
  349. <subform use="#id1"/>
  350. </template>
  351. </xdp:xdp>
  352. `;
  353. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  354. const subform = root.template.subform[1];
  355. expect(subform.id).toEqual("id1");
  356. expect(subform.subform.id).toEqual("id1");
  357. });
  358. it("should parse a xfa document and apply some nested prototypes", function () {
  359. const xml = `
  360. <?xml version="1.0"?>
  361. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  362. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  363. <subform>
  364. <proto>
  365. <color id="RED" value="7, 8, 9"/>
  366. <font id="HELV" typeface="helvetica" size="31pt" weight="normal" posture="italic"> </font>
  367. <font id="HELV-RED" use="#HELV">
  368. <fill>
  369. <color use="#RED"/>
  370. </fill>
  371. </font>
  372. </proto>
  373. <field>
  374. <font use="#HELV-RED"/>
  375. </field>
  376. </subform>
  377. </template>
  378. </xdp:xdp>
  379. `;
  380. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  381. const font = root.template.subform.field.font;
  382. expect(font.typeface).toEqual("helvetica");
  383. expect(font.overline).toEqual(0);
  384. expect(font.size).toEqual(31);
  385. expect(font.weight).toEqual("normal");
  386. expect(font.posture).toEqual("italic");
  387. expect(font.fill.color.value).toEqual({
  388. r: 7,
  389. g: 8,
  390. b: 9
  391. });
  392. });
  393. it("should parse a xfa document and apply a prototype with content", function () {
  394. const xml = `
  395. <?xml version="1.0"?>
  396. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  397. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  398. <subform>
  399. <proto>
  400. <text id="TEXT">default TEXT</text>
  401. </proto>
  402. <field>
  403. <value>
  404. <text use="#TEXT"></text>
  405. </value>
  406. </field>
  407. <field>
  408. <value>
  409. <text use="#TEXT">Overriding text</text>
  410. </value>
  411. </field>
  412. </subform>
  413. </template>
  414. </xdp:xdp>
  415. `;
  416. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  417. let field = root.template.subform.field[0];
  418. expect(field.value.text.$content).toEqual("default TEXT");
  419. field = root.template.subform.field[1];
  420. expect(field.value.text.$content).toEqual("Overriding text");
  421. });
  422. });
  423. describe("Search in XFA", function () {
  424. it("should search some nodes in a template object", function () {
  425. const xml = `
  426. <?xml version="1.0"?>
  427. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  428. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  429. <subform name="Receipt" id="l">
  430. <subform id="m">
  431. <field name="Description" id="a"> </field>
  432. <field name="Units" id="b"> </field>
  433. <field name="Unit_Price" id="c"> </field>
  434. <field name="Total_Price" id="d"> </field>
  435. </subform>
  436. <subform id="n">
  437. <field name="Description" id="e"> </field>
  438. <field name="Units" id="f"> </field>
  439. <field name="Unit_Price" id="g"> </field>
  440. <field name="Total_Price" id="h"> </field>
  441. </subform>
  442. <subform name="foo" id="o">
  443. <field name="Description" id="p"> </field>
  444. <field name="Units" id="q"> </field>
  445. <field name="Unit_Price" id="r"> </field>
  446. <field name="Total_Price" id="s"> </field>
  447. </subform>
  448. <field name="Sub_Total" id="i"> </field>
  449. <field name="Tax" id="j"> </field>
  450. <field name="Total_Price" id="k"> </field>
  451. </subform>
  452. </template>
  453. </xdp:xdp>
  454. `;
  455. const root = new _parser.XFAParser().parse(xml);
  456. let found = root[_xfa_object.$getChildrenByName]("subform", true);
  457. expect(found.map(x => x.id)).toEqual(["l", "m", "n", "o"]);
  458. found = root[_xfa_object.$getChildrenByName]("Total_Price", true);
  459. expect(found.map(x => x.id)).toEqual(["d", "h", "s", "k"]);
  460. found = root.template[_xfa_object.$getChildrenByName]("Receipt", false);
  461. const receipt = found[0];
  462. found = receipt[_xfa_object.$getChildrenByName]("Total_Price", false);
  463. expect(found.map(x => x.id)).toEqual(["d", "h", "k"]);
  464. expect(receipt[_xfa_object.$getChildrenByClass]("name")).toEqual("Receipt");
  465. const subforms = receipt[_xfa_object.$getChildrenByClass]("subform");
  466. expect(subforms.children.map(x => x.id)).toEqual(["m", "n", "o"]);
  467. });
  468. it("should search some nodes in a template object using SOM", function () {
  469. const xml = `
  470. <?xml version="1.0"?>
  471. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  472. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  473. <subform name="Receipt" id="l">
  474. <subform id="m">
  475. <field name="Description" id="a"> </field>
  476. <field name="Units" id="b"> </field>
  477. <field name="Unit_Price" id="c"> </field>
  478. <field name="Total_Price" id="d"> </field>
  479. </subform>
  480. <subform id="n">
  481. <field name="Description" id="e"> </field>
  482. <field name="Units" id="f"> </field>
  483. <field name="Unit_Price" id="g"> </field>
  484. <field name="Total_Price" id="h"> </field>
  485. </subform>
  486. <subform name="foo" id="o">
  487. <field name="Description" id="p"> </field>
  488. <field name="Units" id="q"> </field>
  489. <field name="Unit_Price" id="r"> </field>
  490. <field name="Total_Price" id="s"> </field>
  491. </subform>
  492. <field name="Sub_Total" id="i"> </field>
  493. <field name="Tax" id="j"> </field>
  494. <field name="Total_Price" id="k"> </field>
  495. </subform>
  496. </template>
  497. </xdp:xdp>
  498. `;
  499. const root = new _parser.XFAParser().parse(xml);
  500. expect((0, _som.searchNode)(root, null, "$template..Description.id")[0][_xfa_object.$text]()).toBe("a");
  501. expect((0, _som.searchNode)(root, null, "$template..Description.id")[0][_xfa_object.$text]()).toBe("a");
  502. expect((0, _som.searchNode)(root, null, "$template..Description[0].id")[0][_xfa_object.$text]()).toBe("a");
  503. expect((0, _som.searchNode)(root, null, "$template..Description[1].id")[0][_xfa_object.$text]()).toBe("e");
  504. expect((0, _som.searchNode)(root, null, "$template..Description[2].id")[0][_xfa_object.$text]()).toBe("p");
  505. expect((0, _som.searchNode)(root, null, "$template.Receipt.id")[0][_xfa_object.$text]()).toBe("l");
  506. expect((0, _som.searchNode)(root, null, "$template.Receipt.Description[1].id")[0][_xfa_object.$text]()).toBe("e");
  507. expect((0, _som.searchNode)(root, null, "$template.Receipt.Description[2]")).toBe(null);
  508. expect((0, _som.searchNode)(root, null, "$template.Receipt.foo.Description.id")[0][_xfa_object.$text]()).toBe("p");
  509. expect((0, _som.searchNode)(root, null, "$template.#subform.Sub_Total.id")[0][_xfa_object.$text]()).toBe("i");
  510. expect((0, _som.searchNode)(root, null, "$template.#subform.Units.id")[0][_xfa_object.$text]()).toBe("b");
  511. expect((0, _som.searchNode)(root, null, "$template.#subform.Units.parent.id")[0][_xfa_object.$text]()).toBe("m");
  512. });
  513. it("should search some nodes in a datasets object", function () {
  514. const xml = `
  515. <?xml version="1.0"?>
  516. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  517. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  518. <xfa:data>
  519. <Receipt>
  520. <Page>1</Page>
  521. <Detail PartNo="GS001">
  522. <Description>Giant Slingshot</Description>
  523. <Units>1</Units>
  524. <Unit_Price>250.00</Unit_Price>
  525. <Total_Price>250.00</Total_Price>
  526. </Detail>
  527. <Page>2</Page>
  528. <Detail PartNo="RRB-LB">
  529. <Description>Road Runner Bait, large bag</Description>
  530. <Units>5</Units>
  531. <Unit_Price>12.00</Unit_Price>
  532. <Total_Price>60.00</Total_Price>
  533. </Detail>
  534. <Sub_Total>310.00</Sub_Total>
  535. <Tax>24.80</Tax>
  536. <Total_Price>334.80</Total_Price>
  537. </Receipt>
  538. </xfa:data>
  539. </xfa:datasets>
  540. </xdp:xdp>
  541. `;
  542. const root = new _parser.XFAParser().parse(xml);
  543. const data = root.datasets.data;
  544. let found = data[_xfa_object.$getChildrenByName]("Description", true);
  545. expect(found.map(x => x[_xfa_object.$text]())).toEqual(["Giant Slingshot", "Road Runner Bait, large bag"]);
  546. found = data[_xfa_object.$getChildrenByName]("Total_Price", true);
  547. expect(found.map(x => x[_xfa_object.$text]())).toEqual(["250.00", "60.00", "334.80"]);
  548. });
  549. it("should search some nodes using SOM from a non-root node", function () {
  550. const xml = `
  551. <?xml version="1.0"?>
  552. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  553. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  554. <xfa:data>
  555. <Receipt>
  556. <Page>1</Page>
  557. <Detail PartNo="GS001">
  558. <Description>Giant Slingshot</Description>
  559. <Units>1</Units>
  560. <Unit_Price>250.00</Unit_Price>
  561. <Total_Price>250.00</Total_Price>
  562. </Detail>
  563. <Page>2</Page>
  564. <Detail PartNo="RRB-LB">
  565. <Description>Road Runner Bait, large bag</Description>
  566. <Units>5</Units>
  567. <Unit_Price>12.00</Unit_Price>
  568. <Total_Price>60.00</Total_Price>
  569. </Detail>
  570. <Sub_Total>310.00</Sub_Total>
  571. <Tax>24.80</Tax>
  572. <Total_Price>334.80</Total_Price>
  573. </Receipt>
  574. </xfa:data>
  575. </xfa:datasets>
  576. </xdp:xdp>
  577. `;
  578. const root = new _parser.XFAParser().parse(xml);
  579. const [receipt] = root.datasets.data[_xfa_object.$getChildren]("Receipt");
  580. expect((0, _som.searchNode)(root, receipt, "Detail[*].Total_Price").map(x => x[_xfa_object.$text]())).toEqual(["250.00", "60.00"]);
  581. const [units] = (0, _som.searchNode)(root, receipt, "Detail[1].Units");
  582. expect(units[_xfa_object.$text]()).toBe("5");
  583. let [found] = (0, _som.searchNode)(root, units, "Total_Price");
  584. expect(found[_xfa_object.$text]()).toBe("60.00");
  585. found = (0, _som.searchNode)(root, units, "Total_Pric");
  586. expect(found).toEqual(null);
  587. });
  588. it("should search some nodes in a datasets object using SOM", function () {
  589. const xml = `
  590. <?xml version="1.0"?>
  591. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  592. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  593. <xfa:data>
  594. <Receipt Detail="Acme">
  595. <Detail>foo</Detail>
  596. <Detail>bar</Detail>
  597. </Receipt>
  598. </xfa:data>
  599. </xfa:datasets>
  600. </xdp:xdp>
  601. `;
  602. const root = new _parser.XFAParser().parse(xml);
  603. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail")[0][_xfa_object.$text]()).toBe("Acme");
  604. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[0]")[0][_xfa_object.$text]()).toBe("Acme");
  605. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[1]")[0][_xfa_object.$text]()).toBe("foo");
  606. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[2]")[0][_xfa_object.$text]()).toBe("bar");
  607. });
  608. });
  609. describe("Bind data into form", function () {
  610. it("should make a basic binding", function () {
  611. const xml = `
  612. <?xml version="1.0"?>
  613. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  614. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  615. <subform name="A">
  616. <subform name="B">
  617. <field name="C">
  618. </field>
  619. <field name="D">
  620. </field>
  621. </subform>
  622. </subform>
  623. </template>
  624. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  625. <xfa:data>
  626. <A>
  627. <C>xyz</C>
  628. </A>
  629. </xfa:data>
  630. </xfa:datasets>
  631. </xdp:xdp>
  632. `;
  633. const root = new _parser.XFAParser().parse(xml);
  634. const form = new _bind.Binder(root).bind();
  635. expect((0, _som.searchNode)(form, form, "A.B.C.value.text")[0][_xfa_object.$dump]().$content).toBe("xyz");
  636. });
  637. it("should make another basic binding", function () {
  638. const xml = `
  639. <?xml version="1.0"?>
  640. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  641. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  642. <subform name="registration">
  643. <field name="first"> </field>
  644. <field name="last"> </field>
  645. <field name="apt"> </field>
  646. <field name="street"> </field>
  647. <field name="city"> </field>
  648. <field name="country"> </field>
  649. <field name="postalcode"/>
  650. </subform>
  651. </template>
  652. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  653. <xfa:data>
  654. <registration>
  655. <first>Jack</first>
  656. <last>Spratt</last>
  657. <apt/>
  658. <street>99 Candlestick Lane</street>
  659. <city>London</city>
  660. <country>UK</country>
  661. <postalcode>SW1</postalcode>
  662. </registration>
  663. </xfa:data>
  664. </xfa:datasets>
  665. </xdp:xdp>
  666. `;
  667. const root = new _parser.XFAParser().parse(xml);
  668. const form = new _bind.Binder(root).bind();
  669. expect((0, _som.searchNode)(form, form, "registration.first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  670. expect((0, _som.searchNode)(form, form, "registration.last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  671. expect((0, _som.searchNode)(form, form, "registration.apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  672. expect((0, _som.searchNode)(form, form, "registration.street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  673. expect((0, _som.searchNode)(form, form, "registration.city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  674. expect((0, _som.searchNode)(form, form, "registration.country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  675. expect((0, _som.searchNode)(form, form, "registration.postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  676. });
  677. it("should make basic binding with extra subform", function () {
  678. const xml = `
  679. <?xml version="1.0"?>
  680. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  681. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  682. <subform name="registration">
  683. <field name="first"> </field>
  684. <field name="last"> </field>
  685. <subform name="address">
  686. <field name="apt"> </field>
  687. <field name="street"> </field>
  688. <field name="city"> </field>
  689. <field name="country"> </field>
  690. <field name="postalcode"> </field>
  691. </subform>
  692. </subform>
  693. </template>
  694. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  695. <xfa:data>
  696. <registration>
  697. <first>Jack</first>
  698. <last>Spratt</last>
  699. <apt/>
  700. <street>99 Candlestick Lane</street>
  701. <city>London</city>
  702. <country>UK</country>
  703. <postalcode>SW1</postalcode>
  704. </registration>
  705. </xfa:data>
  706. </xfa:datasets>
  707. </xdp:xdp>
  708. `;
  709. const root = new _parser.XFAParser().parse(xml);
  710. const form = new _bind.Binder(root).bind();
  711. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  712. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  713. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  714. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  715. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  716. expect((0, _som.searchNode)(form, form, "registration..country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  717. expect((0, _som.searchNode)(form, form, "registration..postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  718. });
  719. it("should make basic binding with extra subform", function () {
  720. const xml = `
  721. <?xml version="1.0"?>
  722. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  723. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  724. <subform name="registration" mergeMode="consumeData">
  725. <subform name="address">
  726. <field name="first"/>
  727. <field name="last"/>
  728. <field name="apt"/>
  729. <field name="street"/>
  730. <field name="city"/>
  731. </subform>
  732. </subform>
  733. </template>
  734. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  735. <xfa:data>
  736. <registration>
  737. <first>Jack</first>
  738. <last>Spratt</last>
  739. <address>
  740. <apt>7</apt>
  741. <street>99 Candlestick Lane</street>
  742. <city>London</city>
  743. </address>
  744. </registration>
  745. </xfa:data>
  746. </xfa:datasets>
  747. </xdp:xdp>
  748. `;
  749. const root = new _parser.XFAParser().parse(xml);
  750. const form = new _bind.Binder(root).bind();
  751. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  752. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  753. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe("7");
  754. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  755. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  756. });
  757. it("should make basic binding with same names in different parts", function () {
  758. const xml = `
  759. <?xml version="1.0"?>
  760. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  761. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  762. <subform name="application" mergeMode="consumeData">
  763. <subform name="sponsor">
  764. <field name="lastname"> </field>
  765. <!-- sponsor's last name -->
  766. </subform>
  767. <field name="lastname"> </field>
  768. <!-- applicant's last name -->
  769. </subform>
  770. </template>
  771. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  772. <xfa:data>
  773. <application>
  774. <lastname>Abott</lastname>
  775. <sponsor>
  776. <lastname>Costello</lastname>
  777. </sponsor>
  778. </application>
  779. </xfa:data>
  780. </xfa:datasets>
  781. </xdp:xdp>
  782. `;
  783. const root = new _parser.XFAParser().parse(xml);
  784. const form = new _bind.Binder(root).bind();
  785. expect((0, _som.searchNode)(form, form, "application.sponsor.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Costello");
  786. expect((0, _som.searchNode)(form, form, "application.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Abott");
  787. });
  788. it("should make binding and create nodes in data", function () {
  789. const xml = `
  790. <?xml version="1.0"?>
  791. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  792. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  793. <subform name="root" mergeMode="matchTemplate">
  794. <subform name="A">
  795. <field name="a"/>
  796. <field name="b"/>
  797. <subform name="B">
  798. <field name="c"/>
  799. <field name="d"/>
  800. <subform name="C">
  801. <field name="e"/>
  802. <field name="f"/>
  803. </subform>
  804. </subform>
  805. </subform>
  806. </subform>
  807. </template>
  808. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  809. <xfa:data>
  810. <root>
  811. <A>
  812. <b>1</b>
  813. </A>
  814. </root>
  815. </xfa:data>
  816. </xfa:datasets>
  817. </xdp:xdp>
  818. `;
  819. const root = new _parser.XFAParser().parse(xml);
  820. const binder = new _bind.Binder(root);
  821. const form = binder.bind();
  822. const data = binder.getData();
  823. expect((0, _som.searchNode)(form, form, "root..b..text")[0][_xfa_object.$dump]().$content).toBe("1");
  824. expect((0, _som.searchNode)(data, data, "root.A.a")[0][_xfa_object.$dump]().$name).toBe("a");
  825. expect((0, _som.searchNode)(data, data, "root.A.B.c")[0][_xfa_object.$dump]().$name).toBe("c");
  826. expect((0, _som.searchNode)(data, data, "root.A.B.d")[0][_xfa_object.$dump]().$name).toBe("d");
  827. expect((0, _som.searchNode)(data, data, "root.A.B.C.e")[0][_xfa_object.$dump]().$name).toBe("e");
  828. expect((0, _som.searchNode)(data, data, "root.A.B.C.f")[0][_xfa_object.$dump]().$name).toBe("f");
  829. });
  830. it("should make binding and set properties", function () {
  831. const xml = `
  832. <?xml version="1.0"?>
  833. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  834. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  835. <subform name="Id">
  836. <field name="LastName">
  837. <setProperty ref="$data.Main.Style.NameFont" target="font.typeface"/>
  838. <setProperty ref="$data.Main.Style.NameSize" target="font.size"/>
  839. <setProperty ref="$data.Main.Help.LastName" target="assist.toolTip"/>
  840. <font></font>
  841. <assist>
  842. <toolTip>
  843. </toolTip>
  844. </assist>
  845. </field>
  846. </subform>
  847. </template>
  848. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  849. <xfa:data>
  850. <Id>
  851. <LastName>foo</LastName>
  852. </Id>
  853. <Main>
  854. <Style>
  855. <NameFont>myfont</NameFont>
  856. <NameSize>123.4pt</NameSize>
  857. </Style>
  858. <Help>
  859. <LastName>Give the name!</LastName>
  860. </Help>
  861. </Main>
  862. </xfa:data>
  863. </xfa:datasets>
  864. </xdp:xdp>
  865. `;
  866. const root = new _parser.XFAParser().parse(xml);
  867. const form = new _bind.Binder(root).bind();
  868. expect((0, _som.searchNode)(form, form, "Id.LastName..text")[0][_xfa_object.$dump]().$content).toBe("foo");
  869. expect((0, _som.searchNode)(form, form, "Id.LastName.font.typeface")[0][_xfa_object.$text]()).toBe("myfont");
  870. expect((0, _som.searchNode)(form, form, "Id.LastName.font.size")[0][_xfa_object.$text]()).toEqual(123.4);
  871. expect((0, _som.searchNode)(form, form, "Id.LastName.assist.toolTip")[0][_xfa_object.$dump]().$content).toBe("Give the name!");
  872. });
  873. it("should make binding and bind items", function () {
  874. const xml = `
  875. <?xml version="1.0"?>
  876. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  877. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  878. <subform name="main">
  879. <field name="CardName">
  880. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  881. <ui>
  882. <choiceList/>
  883. </ui>
  884. </field>
  885. </subform>
  886. </template>
  887. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  888. <xfa:data>
  889. <main>
  890. <ccs>
  891. <cc uiname="Visa" token="VISA"/>
  892. <cc uiname="Mastercard" token="MC"/>
  893. <cc uiname="American Express" token="AMEX"/>
  894. </ccs>
  895. <CardName>MC</CardName>
  896. </main>
  897. </xfa:data>
  898. </xfa:datasets>
  899. </xdp:xdp>
  900. `;
  901. const root = new _parser.XFAParser().parse(xml);
  902. const form = new _bind.Binder(root).bind();
  903. expect((0, _som.searchNode)(form, form, "subform.CardName.items[*].text[*]").map(x => x[_xfa_object.$text]())).toEqual(["Visa", "Mastercard", "American Express", "VISA", "MC", "AMEX"]);
  904. });
  905. it("should make binding with occurrences in consumeData mode", function () {
  906. const xml = `
  907. <?xml version="1.0"?>
  908. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  909. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  910. <subform name="root" mergeMode="consumeData">
  911. <subform name="section" id="section1">
  912. <occur min="0" max="-1"/>
  913. <bind match="dataRef" ref="$.section[*]"/>
  914. <field name="line-item"/>
  915. </subform>
  916. <subform name="section" id="section2">
  917. <occur min="0" max="-1"/>
  918. <bind match="dataRef" ref="$.section[*]"/>
  919. <field name="line-item"/>
  920. </subform>
  921. </subform>
  922. </template>
  923. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  924. <xfa:data>
  925. <root>
  926. <section>
  927. <line-item>item1</line-item>
  928. </section>
  929. <section>
  930. <line-item>item2</line-item>
  931. </section>
  932. </root>
  933. </xfa:data>
  934. </xfa:datasets>
  935. </xdp:xdp>
  936. `;
  937. const root = new _parser.XFAParser().parse(xml);
  938. const form = new _bind.Binder(root).bind();
  939. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1"]);
  940. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2"]);
  941. });
  942. it("should make binding with occurrences in matchTemplate mode", function () {
  943. const xml = `
  944. <?xml version="1.0"?>
  945. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  946. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  947. <subform name="root" mergeMode="matchTemplate">
  948. <subform name="section" id="section1">
  949. <occur min="0" max="-1"/>
  950. <bind match="dataRef" ref="$.section[*]"/>
  951. <field name="line-item"/>
  952. </subform>
  953. <subform name="section" id="section2">
  954. <occur min="0" max="-1"/>
  955. <bind match="dataRef" ref="$.section[*]"/>
  956. <field name="line-item"/>
  957. </subform>
  958. </subform>
  959. </template>
  960. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  961. <xfa:data>
  962. <root>
  963. <section>
  964. <line-item>item1</line-item>
  965. </section>
  966. <section>
  967. <line-item>item2</line-item>
  968. </section>
  969. </root>
  970. </xfa:data>
  971. </xfa:datasets>
  972. </xdp:xdp>
  973. `;
  974. const root = new _parser.XFAParser().parse(xml);
  975. const form = new _bind.Binder(root).bind();
  976. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1", "section2", "section2"]);
  977. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2", "item1", "item2"]);
  978. });
  979. });
  980. });