xfa_parser_spec.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. 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 parse CDATA when needed", 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. <field>
  266. <extras>
  267. <exData contentType="text/html" name="foo">
  268. <![CDATA[<body xmlns="http://www.w3.org/1999/xhtml">
  269. <span>hello</span></body>]]>
  270. </exData>
  271. </extra>
  272. </field>
  273. </subform>
  274. </template>
  275. </xdp:xdp>
  276. `;
  277. const root = new _parser.XFAParser().parse(xml);
  278. const exdata = (0, _som.searchNode)(root, root, "foo")[0];
  279. const body = exdata[_xfa_object.$dump]().$content[_xfa_object.$dump]();
  280. const expected = {
  281. $name: "body",
  282. attributes: {},
  283. children: [{
  284. $content: "hello",
  285. $name: "span",
  286. attributes: {},
  287. children: []
  288. }]
  289. };
  290. expect(body).toEqual(expected);
  291. });
  292. it("should parse a xfa document and apply some prototypes", function () {
  293. const xml = `
  294. <?xml version="1.0"?>
  295. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  296. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  297. <subform>
  298. <proto>
  299. <font id="id1" typeface="Foo" size="123pt" weight="bold" posture="italic">
  300. <fill>
  301. <color value="1,2,3"/>
  302. </fill>
  303. </font>
  304. </proto>
  305. <field>
  306. <font use="#id1"/>
  307. </field>
  308. <field>
  309. <font use="#id1" size="456pt" weight="bold" posture="normal">
  310. <fill>
  311. <color value="4,5,6"/>
  312. </fill>
  313. <extras id="id2"/>
  314. </font>
  315. </field>
  316. </subform>
  317. </template>
  318. </xdp:xdp>
  319. `;
  320. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  321. let font = root.template.subform.field[0].font;
  322. expect(font.typeface).toEqual("Foo");
  323. expect(font.overline).toEqual(0);
  324. expect(font.size).toEqual(123);
  325. expect(font.weight).toEqual("bold");
  326. expect(font.posture).toEqual("italic");
  327. expect(font.fill.color.value).toEqual({
  328. r: 1,
  329. g: 2,
  330. b: 3
  331. });
  332. expect(font.extras).toEqual(undefined);
  333. font = root.template.subform.field[1].font;
  334. expect(font.typeface).toEqual("Foo");
  335. expect(font.overline).toEqual(0);
  336. expect(font.size).toEqual(456);
  337. expect(font.weight).toEqual("bold");
  338. expect(font.posture).toEqual("normal");
  339. expect(font.fill.color.value).toEqual({
  340. r: 4,
  341. g: 5,
  342. b: 6
  343. });
  344. expect(font.extras.id).toEqual("id2");
  345. });
  346. it("should parse a xfa document and apply some prototypes through usehref", function () {
  347. const xml = `
  348. <?xml version="1.0"?>
  349. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  350. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  351. <subform>
  352. <proto>
  353. <draw name="foo">
  354. <font typeface="Foo" size="123pt" weight="bold" posture="italic">
  355. <fill>
  356. <color value="1,2,3"/>
  357. </fill>
  358. </font>
  359. </draw>
  360. </proto>
  361. <field>
  362. <font usehref=".#som($template.#subform.foo.#font)"/>
  363. </field>
  364. <field>
  365. <font usehref=".#som($template.#subform.foo.#font)" size="456pt" weight="bold" posture="normal">
  366. <fill>
  367. <color value="4,5,6"/>
  368. </fill>
  369. <extras id="id2"/>
  370. </font>
  371. </field>
  372. </subform>
  373. </template>
  374. </xdp:xdp>
  375. `;
  376. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  377. let font = root.template.subform.field[0].font;
  378. expect(font.typeface).toEqual("Foo");
  379. expect(font.overline).toEqual(0);
  380. expect(font.size).toEqual(123);
  381. expect(font.weight).toEqual("bold");
  382. expect(font.posture).toEqual("italic");
  383. expect(font.fill.color.value).toEqual({
  384. r: 1,
  385. g: 2,
  386. b: 3
  387. });
  388. expect(font.extras).toEqual(undefined);
  389. font = root.template.subform.field[1].font;
  390. expect(font.typeface).toEqual("Foo");
  391. expect(font.overline).toEqual(0);
  392. expect(font.size).toEqual(456);
  393. expect(font.weight).toEqual("bold");
  394. expect(font.posture).toEqual("normal");
  395. expect(font.fill.color.value).toEqual({
  396. r: 4,
  397. g: 5,
  398. b: 6
  399. });
  400. expect(font.extras.id).toEqual("id2");
  401. });
  402. it("should parse a xfa document with xhtml", function () {
  403. const xml = `
  404. <?xml version="1.0"?>
  405. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  406. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  407. <extras>
  408. <text>
  409. <body xmlns="http://www.w3.org/1999/xhtml">
  410. <p style="foo: bar; text-indent:0.5in; line-height:11px;bar:foo;tab-stop: left 0.5in">
  411. The first line of this paragraph is indented a half-inch.<br/>
  412. Successive lines are not indented.<br/>
  413. This is the last line of the paragraph.<br/>
  414. </p>
  415. </body>
  416. </text>
  417. </extras>
  418. </template>
  419. </xdp:xdp>
  420. `;
  421. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  422. const p = root.template.extras.text.$content[_xfa_object.$getChildren]()[0];
  423. expect(p.style).toEqual("text-indent:0.5in;line-height:11px;tab-stop:left 0.5in");
  424. 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(""));
  425. });
  426. it("should parse a xfa document and apply some prototypes with cycle", function () {
  427. const xml = `
  428. <?xml version="1.0"?>
  429. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  430. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  431. <subform>
  432. <proto>
  433. <subform id="id1">
  434. <subform use="#id1"/>
  435. </subform>
  436. </proto>
  437. </subform>
  438. <subform use="#id1"/>
  439. </template>
  440. </xdp:xdp>
  441. `;
  442. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  443. const subform = root.template.subform[1];
  444. expect(subform.id).toEqual("id1");
  445. expect(subform.subform.id).toEqual("id1");
  446. });
  447. it("should parse a xfa document and apply some nested prototypes", function () {
  448. const xml = `
  449. <?xml version="1.0"?>
  450. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  451. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  452. <subform>
  453. <proto>
  454. <color id="RED" value="7, 8, 9"/>
  455. <font id="HELV" typeface="helvetica" size="31pt" weight="normal" posture="italic"> </font>
  456. <font id="HELV-RED" use="#HELV">
  457. <fill>
  458. <color use="#RED"/>
  459. </fill>
  460. </font>
  461. </proto>
  462. <field>
  463. <font use="#HELV-RED"/>
  464. </field>
  465. </subform>
  466. </template>
  467. </xdp:xdp>
  468. `;
  469. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  470. const font = root.template.subform.field.font;
  471. expect(font.typeface).toEqual("helvetica");
  472. expect(font.overline).toEqual(0);
  473. expect(font.size).toEqual(31);
  474. expect(font.weight).toEqual("normal");
  475. expect(font.posture).toEqual("italic");
  476. expect(font.fill.color.value).toEqual({
  477. r: 7,
  478. g: 8,
  479. b: 9
  480. });
  481. });
  482. it("should parse a xfa document and apply a prototype with content", function () {
  483. const xml = `
  484. <?xml version="1.0"?>
  485. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  486. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  487. <subform>
  488. <proto>
  489. <text id="TEXT">default TEXT</text>
  490. </proto>
  491. <field>
  492. <value>
  493. <text use="#TEXT"></text>
  494. </value>
  495. </field>
  496. <field>
  497. <value>
  498. <text use="#TEXT">Overriding text</text>
  499. </value>
  500. </field>
  501. </subform>
  502. </template>
  503. </xdp:xdp>
  504. `;
  505. const root = new _parser.XFAParser().parse(xml)[_xfa_object.$dump]();
  506. let field = root.template.subform.field[0];
  507. expect(field.value.text.$content).toEqual("default TEXT");
  508. field = root.template.subform.field[1];
  509. expect(field.value.text.$content).toEqual("Overriding text");
  510. });
  511. });
  512. describe("Search in XFA", function () {
  513. it("should search some nodes in a template object", function () {
  514. const xml = `
  515. <?xml version="1.0"?>
  516. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  517. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  518. <subform name="Receipt" id="l">
  519. <subform id="m">
  520. <field name="Description" id="a"> </field>
  521. <field name="Units" id="b"> </field>
  522. <field name="Unit_Price" id="c"> </field>
  523. <field name="Total_Price" id="d"> </field>
  524. </subform>
  525. <subform id="n">
  526. <field name="Description" id="e"> </field>
  527. <field name="Units" id="f"> </field>
  528. <field name="Unit_Price" id="g"> </field>
  529. <field name="Total_Price" id="h"> </field>
  530. </subform>
  531. <subform name="foo" id="o">
  532. <field name="Description" id="p"> </field>
  533. <field name="Units" id="q"> </field>
  534. <field name="Unit_Price" id="r"> </field>
  535. <field name="Total_Price" id="s"> </field>
  536. </subform>
  537. <field name="Sub_Total" id="i"> </field>
  538. <field name="Tax" id="j"> </field>
  539. <field name="Total_Price" id="k"> </field>
  540. </subform>
  541. </template>
  542. </xdp:xdp>
  543. `;
  544. const root = new _parser.XFAParser().parse(xml);
  545. let found = root[_xfa_object.$getChildrenByName]("subform", true);
  546. expect(found.map(x => x.id)).toEqual(["l", "m", "n", "o"]);
  547. found = root[_xfa_object.$getChildrenByName]("Total_Price", true);
  548. expect(found.map(x => x.id)).toEqual(["d", "h", "s", "k"]);
  549. found = root.template[_xfa_object.$getChildrenByName]("Receipt", false);
  550. const receipt = found[0];
  551. found = receipt[_xfa_object.$getChildrenByName]("Total_Price", false);
  552. expect(found.map(x => x.id)).toEqual(["d", "h", "k"]);
  553. expect(receipt[_xfa_object.$getChildrenByClass]("name")).toEqual("Receipt");
  554. const subforms = receipt[_xfa_object.$getChildrenByClass]("subform");
  555. expect(subforms.children.map(x => x.id)).toEqual(["m", "n", "o"]);
  556. });
  557. it("should search some nodes in a template object using SOM", function () {
  558. const xml = `
  559. <?xml version="1.0"?>
  560. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  561. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  562. <subform name="Receipt" id="l">
  563. <subform id="m">
  564. <field name="Description" id="a"> </field>
  565. <field name="Units" id="b"> </field>
  566. <field name="Unit_Price" id="c"> </field>
  567. <field name="Total_Price" id="d"> </field>
  568. </subform>
  569. <subform id="n">
  570. <field name="Description" id="e"> </field>
  571. <field name="Units" id="f"> </field>
  572. <field name="Unit_Price" id="g"> </field>
  573. <field name="Total_Price" id="h"> </field>
  574. </subform>
  575. <subform name="foo" id="o">
  576. <field name="Description" id="p"> </field>
  577. <field name="Units" id="q"> </field>
  578. <field name="Unit_Price" id="r"> </field>
  579. <field name="Total_Price" id="s"> </field>
  580. </subform>
  581. <field name="Sub_Total" id="i"> </field>
  582. <field name="Tax" id="j"> </field>
  583. <field name="Total_Price" id="k"> </field>
  584. </subform>
  585. </template>
  586. </xdp:xdp>
  587. `;
  588. const root = new _parser.XFAParser().parse(xml);
  589. expect((0, _som.searchNode)(root, null, "$template..Description.id")[0][_xfa_object.$text]()).toBe("a");
  590. expect((0, _som.searchNode)(root, null, "$template..Description.id")[0][_xfa_object.$text]()).toBe("a");
  591. expect((0, _som.searchNode)(root, null, "$template..Description[0].id")[0][_xfa_object.$text]()).toBe("a");
  592. expect((0, _som.searchNode)(root, null, "$template..Description[1].id")[0][_xfa_object.$text]()).toBe("e");
  593. expect((0, _som.searchNode)(root, null, "$template..Description[2].id")[0][_xfa_object.$text]()).toBe("p");
  594. expect((0, _som.searchNode)(root, null, "$template.Receipt.id")[0][_xfa_object.$text]()).toBe("l");
  595. expect((0, _som.searchNode)(root, null, "$template.Receipt.Description[1].id")[0][_xfa_object.$text]()).toBe("e");
  596. expect((0, _som.searchNode)(root, null, "$template.Receipt.Description[2]")).toBe(null);
  597. expect((0, _som.searchNode)(root, null, "$template.Receipt.foo.Description.id")[0][_xfa_object.$text]()).toBe("p");
  598. expect((0, _som.searchNode)(root, null, "$template.#subform.Sub_Total.id")[0][_xfa_object.$text]()).toBe("i");
  599. expect((0, _som.searchNode)(root, null, "$template.#subform.Units.id")[0][_xfa_object.$text]()).toBe("b");
  600. expect((0, _som.searchNode)(root, null, "$template.#subform.Units.parent.id")[0][_xfa_object.$text]()).toBe("m");
  601. });
  602. it("should search some nodes in a datasets object", function () {
  603. const xml = `
  604. <?xml version="1.0"?>
  605. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  606. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  607. <xfa:data>
  608. <Receipt>
  609. <Page>1</Page>
  610. <Detail PartNo="GS001">
  611. <Description>Giant Slingshot</Description>
  612. <Units>1</Units>
  613. <Unit_Price>250.00</Unit_Price>
  614. <Total_Price>250.00</Total_Price>
  615. </Detail>
  616. <Page>2</Page>
  617. <Detail PartNo="RRB-LB">
  618. <Description>Road Runner Bait, large bag</Description>
  619. <Units>5</Units>
  620. <Unit_Price>12.00</Unit_Price>
  621. <Total_Price>60.00</Total_Price>
  622. </Detail>
  623. <Sub_Total>310.00</Sub_Total>
  624. <Tax>24.80</Tax>
  625. <Total_Price>334.80</Total_Price>
  626. </Receipt>
  627. </xfa:data>
  628. </xfa:datasets>
  629. </xdp:xdp>
  630. `;
  631. const root = new _parser.XFAParser().parse(xml);
  632. const data = root.datasets.data;
  633. let found = data[_xfa_object.$getChildrenByName]("Description", true);
  634. expect(found.map(x => x[_xfa_object.$text]())).toEqual(["Giant Slingshot", "Road Runner Bait, large bag"]);
  635. found = data[_xfa_object.$getChildrenByName]("Total_Price", true);
  636. expect(found.map(x => x[_xfa_object.$text]())).toEqual(["250.00", "60.00", "334.80"]);
  637. });
  638. it("should search some nodes using SOM from a non-root node", function () {
  639. const xml = `
  640. <?xml version="1.0"?>
  641. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  642. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  643. <xfa:data>
  644. <Receipt>
  645. <Page>1</Page>
  646. <Detail PartNo="GS001">
  647. <Description>Giant Slingshot</Description>
  648. <Units>1</Units>
  649. <Unit_Price>250.00</Unit_Price>
  650. <Total_Price>250.00</Total_Price>
  651. </Detail>
  652. <Page>2</Page>
  653. <Detail PartNo="RRB-LB">
  654. <Description>Road Runner Bait, large bag</Description>
  655. <Units>5</Units>
  656. <Unit_Price>12.00</Unit_Price>
  657. <Total_Price>60.00</Total_Price>
  658. </Detail>
  659. <Sub_Total>310.00</Sub_Total>
  660. <Tax>24.80</Tax>
  661. <Total_Price>334.80</Total_Price>
  662. </Receipt>
  663. </xfa:data>
  664. </xfa:datasets>
  665. </xdp:xdp>
  666. `;
  667. const root = new _parser.XFAParser().parse(xml);
  668. const [receipt] = root.datasets.data[_xfa_object.$getChildren]("Receipt");
  669. expect((0, _som.searchNode)(root, receipt, "Detail[*].Total_Price").map(x => x[_xfa_object.$text]())).toEqual(["250.00", "60.00"]);
  670. const [units] = (0, _som.searchNode)(root, receipt, "Detail[1].Units");
  671. expect(units[_xfa_object.$text]()).toBe("5");
  672. let [found] = (0, _som.searchNode)(root, units, "Total_Price");
  673. expect(found[_xfa_object.$text]()).toBe("60.00");
  674. found = (0, _som.searchNode)(root, units, "Total_Pric");
  675. expect(found).toEqual(null);
  676. });
  677. it("should search some nodes in a datasets object using SOM", function () {
  678. const xml = `
  679. <?xml version="1.0"?>
  680. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  681. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  682. <xfa:data>
  683. <Receipt Detail="Acme">
  684. <Detail>foo</Detail>
  685. <Detail>bar</Detail>
  686. </Receipt>
  687. </xfa:data>
  688. </xfa:datasets>
  689. </xdp:xdp>
  690. `;
  691. const root = new _parser.XFAParser().parse(xml);
  692. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail")[0][_xfa_object.$text]()).toBe("Acme");
  693. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[0]")[0][_xfa_object.$text]()).toBe("Acme");
  694. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[1]")[0][_xfa_object.$text]()).toBe("foo");
  695. expect((0, _som.searchNode)(root, null, "$data.Receipt.Detail[2]")[0][_xfa_object.$text]()).toBe("bar");
  696. });
  697. });
  698. describe("Bind data into form", function () {
  699. it("should make a basic binding", function () {
  700. const xml = `
  701. <?xml version="1.0"?>
  702. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  703. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  704. <subform name="A">
  705. <subform name="B">
  706. <field name="C">
  707. </field>
  708. <field name="D">
  709. </field>
  710. </subform>
  711. </subform>
  712. </template>
  713. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  714. <xfa:data>
  715. <A>
  716. <C>xyz</C>
  717. </A>
  718. </xfa:data>
  719. </xfa:datasets>
  720. </xdp:xdp>
  721. `;
  722. const root = new _parser.XFAParser().parse(xml);
  723. const form = new _bind.Binder(root).bind();
  724. expect((0, _som.searchNode)(form, form, "A.B.C.value.text")[0][_xfa_object.$dump]().$content).toBe("xyz");
  725. });
  726. it("should make a basic binding and create a non-existing node", function () {
  727. const xml = `
  728. <?xml version="1.0"?>
  729. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  730. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  731. <subform name="A" mergeMode="matchTemplate">
  732. <subform name="B">
  733. <field name="C">
  734. </field>
  735. <field name="D">
  736. <value>
  737. <text>foobar</text>
  738. </value>
  739. </field>
  740. </subform>
  741. </subform>
  742. </template>
  743. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  744. <xfa:data>
  745. <A>
  746. </A>
  747. </xfa:data>
  748. </xfa:datasets>
  749. </xdp:xdp>
  750. `;
  751. const root = new _parser.XFAParser().parse(xml);
  752. const binder = new _bind.Binder(root);
  753. const form = binder.bind();
  754. const data = binder.getData();
  755. expect((0, _som.searchNode)(form, form, "A.B.D.value.text")[0][_xfa_object.$dump]().$content).toBe("foobar");
  756. const expected = {
  757. $name: "A",
  758. attributes: {},
  759. children: [{
  760. $name: "B",
  761. attributes: {},
  762. children: [{
  763. $name: "C",
  764. attributes: {},
  765. children: []
  766. }, {
  767. $name: "D",
  768. attributes: {},
  769. children: []
  770. }]
  771. }]
  772. };
  773. expect((0, _som.searchNode)(data, data, "A")[0][_xfa_object.$dump]()).toEqual(expected);
  774. });
  775. it("should make a basic binding and create a non-existing node with namespaceId equal to -1", function () {
  776. const xml = `
  777. <?xml version="1.0"?>
  778. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  779. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  780. <subform name="A">
  781. <subform name="B">
  782. <field name="C">
  783. </field>
  784. <field name="D">
  785. <value>
  786. <text>foobar</text>
  787. </value>
  788. </field>
  789. </subform>
  790. </subform>
  791. </template>
  792. </xdp:xdp>
  793. `;
  794. const root = new _parser.XFAParser().parse(xml);
  795. const binder = new _bind.Binder(root);
  796. const form = binder.bind();
  797. const data = binder.getData();
  798. expect((0, _som.searchNode)(form, form, "A.B.D.value.text")[0][_xfa_object.$dump]().$content).toBe("foobar");
  799. const expected = {
  800. $name: "A",
  801. $ns: -1,
  802. attributes: {},
  803. children: [{
  804. $name: "B",
  805. $ns: -1,
  806. attributes: {},
  807. children: [{
  808. $name: "C",
  809. $ns: -1,
  810. attributes: {},
  811. children: []
  812. }, {
  813. $name: "D",
  814. $ns: -1,
  815. attributes: {},
  816. children: []
  817. }]
  818. }]
  819. };
  820. expect((0, _som.searchNode)(data, data, "A")[0][_xfa_object.$dump](true)).toEqual(expected);
  821. });
  822. it("should make another basic binding", function () {
  823. const xml = `
  824. <?xml version="1.0"?>
  825. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  826. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  827. <subform name="registration">
  828. <field name="first"> </field>
  829. <field name="last"> </field>
  830. <field name="apt"> </field>
  831. <field name="street"> </field>
  832. <field name="city"> </field>
  833. <field name="country"> </field>
  834. <field name="postalcode"/>
  835. </subform>
  836. </template>
  837. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  838. <xfa:data>
  839. <registration>
  840. <first>Jack</first>
  841. <last>Spratt</last>
  842. <apt/>
  843. <street>99 Candlestick Lane</street>
  844. <city>London</city>
  845. <country>UK</country>
  846. <postalcode>SW1</postalcode>
  847. </registration>
  848. </xfa:data>
  849. </xfa:datasets>
  850. </xdp:xdp>
  851. `;
  852. const root = new _parser.XFAParser().parse(xml);
  853. const form = new _bind.Binder(root).bind();
  854. expect((0, _som.searchNode)(form, form, "registration.first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  855. expect((0, _som.searchNode)(form, form, "registration.last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  856. expect((0, _som.searchNode)(form, form, "registration.apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  857. expect((0, _som.searchNode)(form, form, "registration.street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  858. expect((0, _som.searchNode)(form, form, "registration.city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  859. expect((0, _som.searchNode)(form, form, "registration.country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  860. expect((0, _som.searchNode)(form, form, "registration.postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  861. });
  862. it("should make basic binding with extra subform", function () {
  863. const xml = `
  864. <?xml version="1.0"?>
  865. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  866. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  867. <subform name="registration">
  868. <field name="first"> </field>
  869. <field name="last"> </field>
  870. <subform name="address">
  871. <field name="apt"> </field>
  872. <field name="street"> </field>
  873. <field name="city"> </field>
  874. <field name="country"> </field>
  875. <field name="postalcode"> </field>
  876. </subform>
  877. </subform>
  878. </template>
  879. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  880. <xfa:data>
  881. <registration>
  882. <first>Jack</first>
  883. <last>Spratt</last>
  884. <apt/>
  885. <street>99 Candlestick Lane</street>
  886. <city>London</city>
  887. <country>UK</country>
  888. <postalcode>SW1</postalcode>
  889. </registration>
  890. </xfa:data>
  891. </xfa:datasets>
  892. </xdp:xdp>
  893. `;
  894. const root = new _parser.XFAParser().parse(xml);
  895. const form = new _bind.Binder(root).bind();
  896. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  897. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  898. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  899. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  900. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  901. expect((0, _som.searchNode)(form, form, "registration..country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  902. expect((0, _som.searchNode)(form, form, "registration..postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  903. });
  904. it("should make basic binding with extra subform", function () {
  905. const xml = `
  906. <?xml version="1.0"?>
  907. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  908. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  909. <subform name="registration" mergeMode="consumeData">
  910. <subform name="address">
  911. <field name="first"/>
  912. <field name="last"/>
  913. <field name="apt"/>
  914. <field name="street"/>
  915. <field name="city"/>
  916. </subform>
  917. </subform>
  918. </template>
  919. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  920. <xfa:data>
  921. <registration>
  922. <first>Jack</first>
  923. <last>Spratt</last>
  924. <address>
  925. <apt>7</apt>
  926. <street>99 Candlestick Lane</street>
  927. <city>London</city>
  928. </address>
  929. </registration>
  930. </xfa:data>
  931. </xfa:datasets>
  932. </xdp:xdp>
  933. `;
  934. const root = new _parser.XFAParser().parse(xml);
  935. const form = new _bind.Binder(root).bind();
  936. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  937. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  938. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe("7");
  939. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  940. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  941. });
  942. it("should make basic binding with same names in different parts", 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="application" mergeMode="consumeData">
  948. <subform name="sponsor">
  949. <field name="lastname"> </field>
  950. <!-- sponsor's last name -->
  951. </subform>
  952. <field name="lastname"> </field>
  953. <!-- applicant's last name -->
  954. </subform>
  955. </template>
  956. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  957. <xfa:data>
  958. <application>
  959. <lastname>Abott</lastname>
  960. <sponsor>
  961. <lastname>Costello</lastname>
  962. </sponsor>
  963. </application>
  964. </xfa:data>
  965. </xfa:datasets>
  966. </xdp:xdp>
  967. `;
  968. const root = new _parser.XFAParser().parse(xml);
  969. const form = new _bind.Binder(root).bind();
  970. expect((0, _som.searchNode)(form, form, "application.sponsor.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Costello");
  971. expect((0, _som.searchNode)(form, form, "application.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Abott");
  972. });
  973. it("should make binding and create nodes in data", function () {
  974. const xml = `
  975. <?xml version="1.0"?>
  976. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  977. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  978. <subform name="root" mergeMode="matchTemplate">
  979. <subform name="A">
  980. <field name="a"/>
  981. <field name="b"/>
  982. <subform name="B">
  983. <field name="c"/>
  984. <field name="d"/>
  985. <subform name="C">
  986. <field name="e"/>
  987. <field name="f"/>
  988. </subform>
  989. </subform>
  990. </subform>
  991. </subform>
  992. </template>
  993. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  994. <xfa:data>
  995. <root>
  996. <A>
  997. <b>1</b>
  998. </A>
  999. </root>
  1000. </xfa:data>
  1001. </xfa:datasets>
  1002. </xdp:xdp>
  1003. `;
  1004. const root = new _parser.XFAParser().parse(xml);
  1005. const binder = new _bind.Binder(root);
  1006. const form = binder.bind();
  1007. const data = binder.getData();
  1008. expect((0, _som.searchNode)(form, form, "root..b..text")[0][_xfa_object.$dump]().$content).toBe("1");
  1009. expect((0, _som.searchNode)(data, data, "root.A.a")[0][_xfa_object.$dump]().$name).toBe("a");
  1010. expect((0, _som.searchNode)(data, data, "root.A.B.c")[0][_xfa_object.$dump]().$name).toBe("c");
  1011. expect((0, _som.searchNode)(data, data, "root.A.B.d")[0][_xfa_object.$dump]().$name).toBe("d");
  1012. expect((0, _som.searchNode)(data, data, "root.A.B.C.e")[0][_xfa_object.$dump]().$name).toBe("e");
  1013. expect((0, _som.searchNode)(data, data, "root.A.B.C.f")[0][_xfa_object.$dump]().$name).toBe("f");
  1014. });
  1015. it("should make binding and set properties", function () {
  1016. const xml = `
  1017. <?xml version="1.0"?>
  1018. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1019. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1020. <subform name="Id">
  1021. <field name="LastName">
  1022. <setProperty ref="$data.Main.Style.NameFont" target="font.typeface"/>
  1023. <setProperty ref="$data.Main.Style.NameSize" target="font.size"/>
  1024. <setProperty ref="$data.Main.Help.LastName" target="assist.toolTip"/>
  1025. <font></font>
  1026. <assist>
  1027. <toolTip>
  1028. </toolTip>
  1029. </assist>
  1030. </field>
  1031. </subform>
  1032. </template>
  1033. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1034. <xfa:data>
  1035. <Id>
  1036. <LastName>foo</LastName>
  1037. </Id>
  1038. <Main>
  1039. <Style>
  1040. <NameFont>myfont</NameFont>
  1041. <NameSize>123.4pt</NameSize>
  1042. </Style>
  1043. <Help>
  1044. <LastName>Give the name!</LastName>
  1045. </Help>
  1046. </Main>
  1047. </xfa:data>
  1048. </xfa:datasets>
  1049. </xdp:xdp>
  1050. `;
  1051. const root = new _parser.XFAParser().parse(xml);
  1052. const form = new _bind.Binder(root).bind();
  1053. expect((0, _som.searchNode)(form, form, "Id.LastName..text")[0][_xfa_object.$dump]().$content).toBe("foo");
  1054. expect((0, _som.searchNode)(form, form, "Id.LastName.font.typeface")[0][_xfa_object.$text]()).toBe("myfont");
  1055. expect((0, _som.searchNode)(form, form, "Id.LastName.font.size")[0][_xfa_object.$text]()).toEqual(123.4);
  1056. expect((0, _som.searchNode)(form, form, "Id.LastName.assist.toolTip")[0][_xfa_object.$dump]().$content).toBe("Give the name!");
  1057. });
  1058. it("should make binding and bind items", function () {
  1059. const xml = `
  1060. <?xml version="1.0"?>
  1061. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1062. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1063. <subform name="main">
  1064. <field name="CardName">
  1065. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  1066. <ui>
  1067. <choiceList/>
  1068. </ui>
  1069. </field>
  1070. </subform>
  1071. </template>
  1072. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1073. <xfa:data>
  1074. <main>
  1075. <ccs>
  1076. <cc uiname="Visa" token="VISA"/>
  1077. <cc uiname="Mastercard" token="MC"/>
  1078. <cc uiname="American Express" token="AMEX"/>
  1079. </ccs>
  1080. <CardName>MC</CardName>
  1081. </main>
  1082. </xfa:data>
  1083. </xfa:datasets>
  1084. </xdp:xdp>
  1085. `;
  1086. const root = new _parser.XFAParser().parse(xml);
  1087. const form = new _bind.Binder(root).bind();
  1088. expect((0, _som.searchNode)(form, form, "subform.CardName.items[*].text[*]").map(x => x[_xfa_object.$text]())).toEqual(["Visa", "Mastercard", "American Express", "VISA", "MC", "AMEX"]);
  1089. });
  1090. it("should make binding and bind items with a ref", function () {
  1091. const xml = `
  1092. <?xml version="1.0"?>
  1093. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1094. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1095. <subform name="main">
  1096. <field name="CardName">
  1097. <bind match="dataRef" ref="$data.main.value"/>
  1098. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  1099. <ui>
  1100. <choiceList/>
  1101. </ui>
  1102. </field>
  1103. </subform>
  1104. </template>
  1105. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1106. <xfa:data>
  1107. <main>
  1108. <value>VISA</value>
  1109. <ccs>
  1110. <cc uiname="Visa" token="VISA"/>
  1111. <cc uiname="Mastercard" token="MC"/>
  1112. <cc uiname="American Express" token="AMEX"/>
  1113. </ccs>
  1114. <CardName>MC</CardName>
  1115. </main>
  1116. </xfa:data>
  1117. </xfa:datasets>
  1118. </xdp:xdp>
  1119. `;
  1120. const root = new _parser.XFAParser().parse(xml);
  1121. const form = new _bind.Binder(root).bind();
  1122. expect((0, _som.searchNode)(form, form, "subform.CardName.value.text").map(x => x[_xfa_object.$text]())).toEqual(["VISA"]);
  1123. expect((0, _som.searchNode)(form, form, "subform.CardName.items[*].text[*]").map(x => x[_xfa_object.$text]())).toEqual(["Visa", "Mastercard", "American Express", "VISA", "MC", "AMEX"]);
  1124. });
  1125. it("should make binding with occurrences in consumeData mode", function () {
  1126. const xml = `
  1127. <?xml version="1.0"?>
  1128. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1129. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1130. <subform name="root" mergeMode="consumeData">
  1131. <subform name="section" id="section1">
  1132. <occur min="0" max="-1"/>
  1133. <bind match="dataRef" ref="$.section[*]"/>
  1134. <field name="line-item"/>
  1135. </subform>
  1136. <subform name="section" id="section2">
  1137. <occur min="0" max="-1"/>
  1138. <bind match="dataRef" ref="$.section[*]"/>
  1139. <field name="line-item"/>
  1140. </subform>
  1141. </subform>
  1142. </template>
  1143. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1144. <xfa:data>
  1145. <root>
  1146. <section>
  1147. <line-item>item1</line-item>
  1148. </section>
  1149. <section>
  1150. <line-item>item2</line-item>
  1151. </section>
  1152. </root>
  1153. </xfa:data>
  1154. </xfa:datasets>
  1155. </xdp:xdp>
  1156. `;
  1157. const root = new _parser.XFAParser().parse(xml);
  1158. const form = new _bind.Binder(root).bind();
  1159. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1"]);
  1160. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2"]);
  1161. });
  1162. it("should make binding with occurrences in matchTemplate mode", function () {
  1163. const xml = `
  1164. <?xml version="1.0"?>
  1165. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1166. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1167. <subform name="root" mergeMode="matchTemplate">
  1168. <subform name="section" id="section1">
  1169. <occur min="0" max="-1"/>
  1170. <bind match="dataRef" ref="$.section[*]"/>
  1171. <field name="line-item"/>
  1172. </subform>
  1173. <subform name="section" id="section2">
  1174. <occur min="0" max="-1"/>
  1175. <bind match="dataRef" ref="$.section[*]"/>
  1176. <field name="line-item"/>
  1177. </subform>
  1178. </subform>
  1179. </template>
  1180. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1181. <xfa:data>
  1182. <root>
  1183. <section>
  1184. <line-item>item1</line-item>
  1185. </section>
  1186. <section>
  1187. <line-item>item2</line-item>
  1188. </section>
  1189. </root>
  1190. </xfa:data>
  1191. </xfa:datasets>
  1192. </xdp:xdp>
  1193. `;
  1194. const root = new _parser.XFAParser().parse(xml);
  1195. const form = new _bind.Binder(root).bind();
  1196. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1", "section2", "section2"]);
  1197. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2", "item1", "item2"]);
  1198. });
  1199. it("should make binding and create nodes in data with some bind tag", function () {
  1200. const xml = `
  1201. <?xml version="1.0"?>
  1202. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1203. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1204. <subform name="root" mergeMode="matchTemplate">
  1205. <subform name="A">
  1206. <occur max="-1"/>
  1207. <bind ref="$.root.foo[*]" match="dataRef"/>
  1208. </subform>
  1209. <subform name="B">
  1210. <occur max="2"/>
  1211. <bind ref="$.root.bar[2]" match="dataRef"/>
  1212. </subform>
  1213. </subform>
  1214. </template>
  1215. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1216. <xfa:data>
  1217. <root>
  1218. </root>
  1219. </xfa:data>
  1220. </xfa:datasets>
  1221. </xdp:xdp>
  1222. `;
  1223. const root = new _parser.XFAParser().parse(xml);
  1224. const binder = new _bind.Binder(root);
  1225. binder.bind();
  1226. const data = binder.getData();
  1227. const expected = {
  1228. $name: "root",
  1229. children: [{
  1230. $name: "root",
  1231. children: [{
  1232. $name: "foo",
  1233. children: [],
  1234. attributes: {}
  1235. }, {
  1236. $name: "bar",
  1237. children: [],
  1238. attributes: {}
  1239. }, {
  1240. $name: "bar",
  1241. children: [],
  1242. attributes: {}
  1243. }, {
  1244. $name: "bar",
  1245. children: [],
  1246. attributes: {}
  1247. }],
  1248. attributes: {}
  1249. }],
  1250. attributes: {}
  1251. };
  1252. expect((0, _som.searchNode)(data, data, "root")[0][_xfa_object.$dump]()).toEqual(expected);
  1253. });
  1254. it("should make a binding with a bindItems", function () {
  1255. const xml = `
  1256. <?xml version="1.0"?>
  1257. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1258. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1259. <subform name="A" mergeMode="matchTemplate">
  1260. <subform name="B">
  1261. <field name="C">
  1262. <ui>
  1263. <choicelist/>
  1264. </ui>
  1265. <bindItems ref="xfa.datasets.foo.bar[*]" labelRef="$" valueRef="oof"/>
  1266. </field>
  1267. </subform>
  1268. </subform>
  1269. </template>
  1270. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1271. <foo>
  1272. <bar oof="a">1</bar>
  1273. <bar oof="b">2</bar>
  1274. <bar oof="c">3</bar>
  1275. <bar oof="d">4</bar>
  1276. <bar oof="e">5</bar>
  1277. </foo>
  1278. <xfa:data>
  1279. <A><B></B></A>
  1280. </xfa:data>
  1281. </xfa:datasets>
  1282. </xdp:xdp>
  1283. `;
  1284. const root = new _parser.XFAParser().parse(xml);
  1285. const form = new _bind.Binder(root).bind();
  1286. expect((0, _som.searchNode)(form, form, "A.B.C.items[0].text[*]").map(x => x[_xfa_object.$dump]().$content)).toEqual(["1", "2", "3", "4", "5"]);
  1287. expect((0, _som.searchNode)(form, form, "A.B.C.items[1].text[*]").map(x => x[_xfa_object.$dump]().$content)).toEqual(["a", "b", "c", "d", "e"]);
  1288. });
  1289. });
  1290. it("should make a binding with a element in an area", function () {
  1291. const xml = `
  1292. <?xml version="1.0"?>
  1293. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1294. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1295. <subform name="A" mergeMode="matchTemplate">
  1296. <area>
  1297. <field name="B"/>
  1298. </area>
  1299. </subform>
  1300. </template>
  1301. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1302. <xfa:data>
  1303. <A><B>foobar</B></A>
  1304. </xfa:data>
  1305. </xfa:datasets>
  1306. </xdp:xdp>
  1307. `;
  1308. const root = new _parser.XFAParser().parse(xml);
  1309. const form = new _bind.Binder(root).bind();
  1310. expect((0, _som.searchNode)(form, form, "A..B..text")[0][_xfa_object.$dump]().$content).toBe("foobar");
  1311. });
  1312. });