xfa_parser_spec.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. $dump,
  17. $getChildren,
  18. $getChildrenByClass,
  19. $getChildrenByName,
  20. $text,
  21. } from "../../src/core/xfa/xfa_object.js";
  22. import { Binder } from "../../src/core/xfa/bind.js";
  23. import { searchNode } from "../../src/core/xfa/som.js";
  24. import { XFAParser } from "../../src/core/xfa/parser.js";
  25. describe("XFAParser", function () {
  26. describe("Parse XFA", function () {
  27. it("should parse a xfa document and create an object to represent it", function () {
  28. const xml = `
  29. <?xml version="1.0"?>
  30. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" uuid="1234" invalid="foo">
  31. <config xmlns="http://www.xfa.org/schema/xci/3.1/">
  32. <present>
  33. <pdf name="hello">
  34. <adobeExtensionLevel>
  35. 7
  36. </adobeExtensionLevel>
  37. </pdf>
  38. <invalid><a>foobar</a></invalid>
  39. </present>
  40. <acrobat>
  41. <submitUrl>http://a.b.c</submitUrl>
  42. <acrobat7>
  43. <dynamicRender>
  44. forbidden
  45. </dynamicRender>
  46. </acrobat7>
  47. <autoSave>enabled</autoSave>
  48. <submitUrl>
  49. http://d.e.f
  50. </submitUrl>
  51. <submitUrl>http://g.h.i</submitUrl>
  52. <validate>foobar</validate>
  53. </acrobat>
  54. </config>
  55. <template baseProfile="full" xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  56. <extras>
  57. <float>1.23</float>
  58. <boolean>1</boolean>
  59. <integer>314</integer>
  60. <float>2.71</float>
  61. </extras>
  62. <subform>
  63. <proto>
  64. <area x="hello" y="-3.14in" relevant="-foo +bar" />
  65. <color value="111, 222, 123" />
  66. <color value="111, abc, 123" />
  67. <medium imagingBBox="1,2in,3.4cm,5.67px" />
  68. <medium imagingBBox="1,2in,-3cm,4px" />
  69. </proto>
  70. </subform>
  71. </template>
  72. </xdp:xdp>
  73. `;
  74. const attributes = {
  75. id: "",
  76. name: "",
  77. use: "",
  78. usehref: "",
  79. };
  80. const mediumAttributes = {
  81. id: "",
  82. long: 0,
  83. orientation: "portrait",
  84. short: 0,
  85. stock: "",
  86. trayIn: "auto",
  87. trayOut: "auto",
  88. use: "",
  89. usehref: "",
  90. };
  91. const colorAttributes = {
  92. cSpace: "SRGB",
  93. id: "",
  94. use: "",
  95. usehref: "",
  96. };
  97. const root = new XFAParser().parse(xml);
  98. const expected = {
  99. uuid: "1234",
  100. timeStamp: "",
  101. template: {
  102. baseProfile: "full",
  103. extras: {
  104. ...attributes,
  105. float: [
  106. { ...attributes, $content: 1.23 },
  107. { ...attributes, $content: 2.71 },
  108. ],
  109. boolean: { ...attributes, $content: 1 },
  110. integer: { ...attributes, $content: 314 },
  111. },
  112. subform: {
  113. access: "open",
  114. allowMacro: 0,
  115. anchorType: "topLeft",
  116. colSpan: 1,
  117. columnWidths: [0],
  118. h: "",
  119. hAlign: "left",
  120. id: "",
  121. layout: "position",
  122. locale: "",
  123. maxH: 0,
  124. maxW: 0,
  125. mergeMode: "consumeData",
  126. minH: 0,
  127. minW: 0,
  128. name: "",
  129. presence: "visible",
  130. relevant: [],
  131. restoreState: "manual",
  132. scope: "name",
  133. use: "",
  134. usehref: "",
  135. w: "",
  136. x: 0,
  137. y: 0,
  138. proto: {
  139. area: {
  140. ...attributes,
  141. colSpan: 1,
  142. x: 0,
  143. y: -226.08,
  144. relevant: [
  145. { excluded: true, viewname: "foo" },
  146. { excluded: false, viewname: "bar" },
  147. ],
  148. },
  149. color: [
  150. {
  151. ...colorAttributes,
  152. value: { r: 111, g: 222, b: 123 },
  153. },
  154. {
  155. ...colorAttributes,
  156. value: { r: 111, g: 0, b: 123 },
  157. },
  158. ],
  159. medium: [
  160. {
  161. ...mediumAttributes,
  162. imagingBBox: {
  163. x: 1,
  164. y: 144,
  165. width: 96.3779527559055,
  166. height: 5.67,
  167. },
  168. },
  169. {
  170. ...mediumAttributes,
  171. imagingBBox: {
  172. x: -1,
  173. y: -1,
  174. width: -1,
  175. height: -1,
  176. },
  177. },
  178. ],
  179. },
  180. },
  181. },
  182. config: {
  183. acrobat: {
  184. acrobat7: {
  185. dynamicRender: {
  186. $content: "forbidden",
  187. },
  188. },
  189. autoSave: {
  190. $content: "enabled",
  191. },
  192. validate: {
  193. $content: "preSubmit",
  194. },
  195. submitUrl: [
  196. {
  197. $content: "http://a.b.c",
  198. },
  199. {
  200. $content: "http://d.e.f",
  201. },
  202. {
  203. $content: "http://g.h.i",
  204. },
  205. ],
  206. },
  207. present: {
  208. pdf: {
  209. name: "hello",
  210. adobeExtensionLevel: {
  211. $content: 7,
  212. },
  213. },
  214. },
  215. },
  216. };
  217. expect(root[$dump]()).toEqual(expected);
  218. });
  219. it("should parse a xfa document and check namespaces", function () {
  220. const xml = `
  221. <?xml version="1.0"?>
  222. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  223. <config xmlns:foo="http:/www.foo.com" xmlns="http://www.xfa.org/schema/xci/3.1/">
  224. <present xmlns="http://www.mozilla.org">
  225. <pdf name="hello">
  226. <adobeExtensionLevel>
  227. 7
  228. </adobeExtensionLevel>
  229. </pdf>
  230. </present>
  231. <acrobat>
  232. <foo:submitUrl>http://a.b.c</foo:submitUrl>
  233. <submitUrl>http://c.b.a</submitUrl>
  234. </acrobat>
  235. </config>
  236. <template baseProfile="full" xmlns="http://www.allizom.org">
  237. <extras>
  238. <float>1.23</float>
  239. </extras>
  240. </template>
  241. </xdp:xdp>
  242. `;
  243. const root = new XFAParser().parse(xml);
  244. const expected = {
  245. uuid: "",
  246. timeStamp: "",
  247. config: {
  248. acrobat: {
  249. submitUrl: { $content: "http://c.b.a" },
  250. },
  251. },
  252. };
  253. expect(root[$dump]()).toEqual(expected);
  254. });
  255. it("should parse a xfa document and parse CDATA when needed", function () {
  256. const xml = `
  257. <?xml version="1.0"?>
  258. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  259. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  260. <subform>
  261. <field>
  262. <extras>
  263. <exData contentType="text/html" name="foo">
  264. <![CDATA[<body xmlns="http://www.w3.org/1999/xhtml">
  265. <span>hello</span></body>]]>
  266. </exData>
  267. </extra>
  268. </field>
  269. </subform>
  270. </template>
  271. </xdp:xdp>
  272. `;
  273. const root = new XFAParser().parse(xml);
  274. const exdata = searchNode(root, root, "foo")[0];
  275. const body = exdata[$dump]().$content[$dump]();
  276. const expected = {
  277. $name: "body",
  278. attributes: {},
  279. children: [
  280. { $content: "hello", $name: "span", attributes: {}, children: [] },
  281. ],
  282. };
  283. expect(body).toEqual(expected);
  284. });
  285. it("should parse a xfa document and apply some prototypes", function () {
  286. const xml = `
  287. <?xml version="1.0"?>
  288. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  289. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  290. <subform>
  291. <proto>
  292. <font id="id1" typeface="Foo" size="123pt" weight="bold" posture="italic">
  293. <fill>
  294. <color value="1,2,3"/>
  295. </fill>
  296. </font>
  297. </proto>
  298. <field>
  299. <font use="#id1"/>
  300. </field>
  301. <field>
  302. <font use="#id1" size="456pt" weight="bold" posture="normal">
  303. <fill>
  304. <color value="4,5,6"/>
  305. </fill>
  306. <extras id="id2"/>
  307. </font>
  308. </field>
  309. </subform>
  310. </template>
  311. </xdp:xdp>
  312. `;
  313. const root = new XFAParser().parse(xml)[$dump]();
  314. let font = root.template.subform.field[0].font;
  315. expect(font.typeface).toEqual("Foo");
  316. expect(font.overline).toEqual(0);
  317. expect(font.size).toEqual(123);
  318. expect(font.weight).toEqual("bold");
  319. expect(font.posture).toEqual("italic");
  320. expect(font.fill.color.value).toEqual({ r: 1, g: 2, b: 3 });
  321. expect(font.extras).toEqual(undefined);
  322. font = root.template.subform.field[1].font;
  323. expect(font.typeface).toEqual("Foo");
  324. expect(font.overline).toEqual(0);
  325. expect(font.size).toEqual(456);
  326. expect(font.weight).toEqual("bold");
  327. expect(font.posture).toEqual("normal");
  328. expect(font.fill.color.value).toEqual({ r: 4, g: 5, b: 6 });
  329. expect(font.extras.id).toEqual("id2");
  330. });
  331. it("should parse a xfa document and apply some prototypes through usehref", function () {
  332. const xml = `
  333. <?xml version="1.0"?>
  334. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  335. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  336. <subform>
  337. <proto>
  338. <draw name="foo">
  339. <font typeface="Foo" size="123pt" weight="bold" posture="italic">
  340. <fill>
  341. <color value="1,2,3"/>
  342. </fill>
  343. </font>
  344. </draw>
  345. </proto>
  346. <field>
  347. <font usehref=".#som($template.#subform.foo.#font)"/>
  348. </field>
  349. <field>
  350. <font usehref=".#som($template.#subform.foo.#font)" size="456pt" weight="bold" posture="normal">
  351. <fill>
  352. <color value="4,5,6"/>
  353. </fill>
  354. <extras id="id2"/>
  355. </font>
  356. </field>
  357. </subform>
  358. </template>
  359. </xdp:xdp>
  360. `;
  361. const root = new XFAParser().parse(xml)[$dump]();
  362. let font = root.template.subform.field[0].font;
  363. expect(font.typeface).toEqual("Foo");
  364. expect(font.overline).toEqual(0);
  365. expect(font.size).toEqual(123);
  366. expect(font.weight).toEqual("bold");
  367. expect(font.posture).toEqual("italic");
  368. expect(font.fill.color.value).toEqual({ r: 1, g: 2, b: 3 });
  369. expect(font.extras).toEqual(undefined);
  370. font = root.template.subform.field[1].font;
  371. expect(font.typeface).toEqual("Foo");
  372. expect(font.overline).toEqual(0);
  373. expect(font.size).toEqual(456);
  374. expect(font.weight).toEqual("bold");
  375. expect(font.posture).toEqual("normal");
  376. expect(font.fill.color.value).toEqual({ r: 4, g: 5, b: 6 });
  377. expect(font.extras.id).toEqual("id2");
  378. });
  379. it("should parse a xfa document with xhtml", function () {
  380. const xml = `
  381. <?xml version="1.0"?>
  382. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  383. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  384. <extras>
  385. <text>
  386. <body xmlns="http://www.w3.org/1999/xhtml">
  387. <p style="foo: bar; text-indent:0.5in; line-height:11px;bar:foo;tab-stop: left 0.5in">
  388. The first line of this paragraph is indented a half-inch.<br/>
  389. Successive lines are not indented.<br/>
  390. This is the last line of the paragraph.<br/>
  391. </p>
  392. </body>
  393. </text>
  394. </extras>
  395. </template>
  396. </xdp:xdp>
  397. `;
  398. const root = new XFAParser().parse(xml)[$dump]();
  399. const p = root.template.extras.text.$content[$getChildren]()[0];
  400. expect(p.style).toEqual(
  401. "text-indent:0.5in;line-height:11px;tab-stop:left 0.5in"
  402. );
  403. expect(p[$text]()).toEqual(
  404. [
  405. " The first line of this paragraph is indented a half-inch.\n",
  406. " Successive lines are not indented.\n",
  407. " This is the last line of the paragraph.\n ",
  408. ].join("")
  409. );
  410. });
  411. it("should parse a xfa document and apply some prototypes with cycle", function () {
  412. const xml = `
  413. <?xml version="1.0"?>
  414. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  415. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  416. <subform>
  417. <proto>
  418. <subform id="id1">
  419. <subform use="#id1"/>
  420. </subform>
  421. </proto>
  422. </subform>
  423. <subform use="#id1"/>
  424. </template>
  425. </xdp:xdp>
  426. `;
  427. const root = new XFAParser().parse(xml)[$dump]();
  428. const subform = root.template.subform[1];
  429. expect(subform.id).toEqual("id1");
  430. expect(subform.subform.id).toEqual("id1");
  431. });
  432. it("should parse a xfa document and apply some nested prototypes", function () {
  433. const xml = `
  434. <?xml version="1.0"?>
  435. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  436. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  437. <subform>
  438. <proto>
  439. <color id="RED" value="7, 8, 9"/>
  440. <font id="HELV" typeface="helvetica" size="31pt" weight="normal" posture="italic"> </font>
  441. <font id="HELV-RED" use="#HELV">
  442. <fill>
  443. <color use="#RED"/>
  444. </fill>
  445. </font>
  446. </proto>
  447. <field>
  448. <font use="#HELV-RED"/>
  449. </field>
  450. </subform>
  451. </template>
  452. </xdp:xdp>
  453. `;
  454. const root = new XFAParser().parse(xml)[$dump]();
  455. const font = root.template.subform.field.font;
  456. expect(font.typeface).toEqual("helvetica");
  457. expect(font.overline).toEqual(0);
  458. expect(font.size).toEqual(31);
  459. expect(font.weight).toEqual("normal");
  460. expect(font.posture).toEqual("italic");
  461. expect(font.fill.color.value).toEqual({ r: 7, g: 8, b: 9 });
  462. });
  463. it("should parse a xfa document and apply a prototype with content", function () {
  464. const xml = `
  465. <?xml version="1.0"?>
  466. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  467. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  468. <subform>
  469. <proto>
  470. <text id="TEXT">default TEXT</text>
  471. </proto>
  472. <field>
  473. <value>
  474. <text use="#TEXT"></text>
  475. </value>
  476. </field>
  477. <field>
  478. <value>
  479. <text use="#TEXT">Overriding text</text>
  480. </value>
  481. </field>
  482. </subform>
  483. </template>
  484. </xdp:xdp>
  485. `;
  486. const root = new XFAParser().parse(xml)[$dump]();
  487. let field = root.template.subform.field[0];
  488. expect(field.value.text.$content).toEqual("default TEXT");
  489. field = root.template.subform.field[1];
  490. expect(field.value.text.$content).toEqual("Overriding text");
  491. });
  492. });
  493. describe("Search in XFA", function () {
  494. it("should search some nodes in a template object", function () {
  495. const xml = `
  496. <?xml version="1.0"?>
  497. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  498. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  499. <subform name="Receipt" id="l">
  500. <subform id="m">
  501. <field name="Description" id="a"> </field>
  502. <field name="Units" id="b"> </field>
  503. <field name="Unit_Price" id="c"> </field>
  504. <field name="Total_Price" id="d"> </field>
  505. </subform>
  506. <subform id="n">
  507. <field name="Description" id="e"> </field>
  508. <field name="Units" id="f"> </field>
  509. <field name="Unit_Price" id="g"> </field>
  510. <field name="Total_Price" id="h"> </field>
  511. </subform>
  512. <subform name="foo" id="o">
  513. <field name="Description" id="p"> </field>
  514. <field name="Units" id="q"> </field>
  515. <field name="Unit_Price" id="r"> </field>
  516. <field name="Total_Price" id="s"> </field>
  517. </subform>
  518. <field name="Sub_Total" id="i"> </field>
  519. <field name="Tax" id="j"> </field>
  520. <field name="Total_Price" id="k"> </field>
  521. </subform>
  522. </template>
  523. </xdp:xdp>
  524. `;
  525. const root = new XFAParser().parse(xml);
  526. let found = root[$getChildrenByName]("subform", true);
  527. expect(found.map(x => x.id)).toEqual(["l", "m", "n", "o"]);
  528. found = root[$getChildrenByName]("Total_Price", true);
  529. expect(found.map(x => x.id)).toEqual(["d", "h", "s", "k"]);
  530. found = root.template[$getChildrenByName]("Receipt", false);
  531. const receipt = found[0];
  532. found = receipt[$getChildrenByName]("Total_Price", false);
  533. expect(found.map(x => x.id)).toEqual(["d", "h", "k"]);
  534. expect(receipt[$getChildrenByClass]("name")).toEqual("Receipt");
  535. const subforms = receipt[$getChildrenByClass]("subform");
  536. expect(subforms.children.map(x => x.id)).toEqual(["m", "n", "o"]);
  537. });
  538. it("should search some nodes in a template object using SOM", function () {
  539. const xml = `
  540. <?xml version="1.0"?>
  541. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  542. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  543. <subform name="Receipt" id="l">
  544. <subform id="m">
  545. <field name="Description" id="a"> </field>
  546. <field name="Units" id="b"> </field>
  547. <field name="Unit_Price" id="c"> </field>
  548. <field name="Total_Price" id="d"> </field>
  549. </subform>
  550. <subform id="n">
  551. <field name="Description" id="e"> </field>
  552. <field name="Units" id="f"> </field>
  553. <field name="Unit_Price" id="g"> </field>
  554. <field name="Total_Price" id="h"> </field>
  555. </subform>
  556. <subform name="foo" id="o">
  557. <field name="Description" id="p"> </field>
  558. <field name="Units" id="q"> </field>
  559. <field name="Unit_Price" id="r"> </field>
  560. <field name="Total_Price" id="s"> </field>
  561. </subform>
  562. <field name="Sub_Total" id="i"> </field>
  563. <field name="Tax" id="j"> </field>
  564. <field name="Total_Price" id="k"> </field>
  565. </subform>
  566. </template>
  567. </xdp:xdp>
  568. `;
  569. const root = new XFAParser().parse(xml);
  570. expect(
  571. searchNode(root, null, "$template..Description.id")[0][$text]()
  572. ).toBe("a");
  573. expect(
  574. searchNode(root, null, "$template..Description.id")[0][$text]()
  575. ).toBe("a");
  576. expect(
  577. searchNode(root, null, "$template..Description[0].id")[0][$text]()
  578. ).toBe("a");
  579. expect(
  580. searchNode(root, null, "$template..Description[1].id")[0][$text]()
  581. ).toBe("e");
  582. expect(
  583. searchNode(root, null, "$template..Description[2].id")[0][$text]()
  584. ).toBe("p");
  585. expect(searchNode(root, null, "$template.Receipt.id")[0][$text]()).toBe(
  586. "l"
  587. );
  588. expect(
  589. searchNode(root, null, "$template.Receipt.Description[1].id")[0][
  590. $text
  591. ]()
  592. ).toBe("e");
  593. expect(searchNode(root, null, "$template.Receipt.Description[2]")).toBe(
  594. null
  595. );
  596. expect(
  597. searchNode(root, null, "$template.Receipt.foo.Description.id")[0][
  598. $text
  599. ]()
  600. ).toBe("p");
  601. expect(
  602. searchNode(root, null, "$template.#subform.Sub_Total.id")[0][$text]()
  603. ).toBe("i");
  604. expect(
  605. searchNode(root, null, "$template.#subform.Units.id")[0][$text]()
  606. ).toBe("b");
  607. expect(
  608. searchNode(root, null, "$template.#subform.Units.parent.id")[0][$text]()
  609. ).toBe("m");
  610. });
  611. it("should search some nodes in a datasets object", function () {
  612. const xml = `
  613. <?xml version="1.0"?>
  614. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  615. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  616. <xfa:data>
  617. <Receipt>
  618. <Page>1</Page>
  619. <Detail PartNo="GS001">
  620. <Description>Giant Slingshot</Description>
  621. <Units>1</Units>
  622. <Unit_Price>250.00</Unit_Price>
  623. <Total_Price>250.00</Total_Price>
  624. </Detail>
  625. <Page>2</Page>
  626. <Detail PartNo="RRB-LB">
  627. <Description>Road Runner Bait, large bag</Description>
  628. <Units>5</Units>
  629. <Unit_Price>12.00</Unit_Price>
  630. <Total_Price>60.00</Total_Price>
  631. </Detail>
  632. <Sub_Total>310.00</Sub_Total>
  633. <Tax>24.80</Tax>
  634. <Total_Price>334.80</Total_Price>
  635. </Receipt>
  636. </xfa:data>
  637. </xfa:datasets>
  638. </xdp:xdp>
  639. `;
  640. const root = new XFAParser().parse(xml);
  641. const data = root.datasets.data;
  642. let found = data[$getChildrenByName]("Description", true);
  643. expect(found.map(x => x[$text]())).toEqual([
  644. "Giant Slingshot",
  645. "Road Runner Bait, large bag",
  646. ]);
  647. found = data[$getChildrenByName]("Total_Price", true);
  648. expect(found.map(x => x[$text]())).toEqual(["250.00", "60.00", "334.80"]);
  649. });
  650. it("should search some nodes using SOM from a non-root node", function () {
  651. const xml = `
  652. <?xml version="1.0"?>
  653. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  654. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  655. <xfa:data>
  656. <Receipt>
  657. <Page>1</Page>
  658. <Detail PartNo="GS001">
  659. <Description>Giant Slingshot</Description>
  660. <Units>1</Units>
  661. <Unit_Price>250.00</Unit_Price>
  662. <Total_Price>250.00</Total_Price>
  663. </Detail>
  664. <Page>2</Page>
  665. <Detail PartNo="RRB-LB">
  666. <Description>Road Runner Bait, large bag</Description>
  667. <Units>5</Units>
  668. <Unit_Price>12.00</Unit_Price>
  669. <Total_Price>60.00</Total_Price>
  670. </Detail>
  671. <Sub_Total>310.00</Sub_Total>
  672. <Tax>24.80</Tax>
  673. <Total_Price>334.80</Total_Price>
  674. </Receipt>
  675. </xfa:data>
  676. </xfa:datasets>
  677. </xdp:xdp>
  678. `;
  679. const root = new XFAParser().parse(xml);
  680. const [receipt] = root.datasets.data[$getChildren]("Receipt");
  681. expect(
  682. searchNode(root, receipt, "Detail[*].Total_Price").map(x => x[$text]())
  683. ).toEqual(["250.00", "60.00"]);
  684. const [units] = searchNode(root, receipt, "Detail[1].Units");
  685. expect(units[$text]()).toBe("5");
  686. let [found] = searchNode(root, units, "Total_Price");
  687. expect(found[$text]()).toBe("60.00");
  688. found = searchNode(root, units, "Total_Pric");
  689. expect(found).toEqual(null);
  690. });
  691. it("should search some nodes in a datasets object using SOM", function () {
  692. const xml = `
  693. <?xml version="1.0"?>
  694. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  695. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  696. <xfa:data>
  697. <Receipt Detail="Acme">
  698. <Detail>foo</Detail>
  699. <Detail>bar</Detail>
  700. </Receipt>
  701. </xfa:data>
  702. </xfa:datasets>
  703. </xdp:xdp>
  704. `;
  705. const root = new XFAParser().parse(xml);
  706. expect(searchNode(root, null, "$data.Receipt.Detail")[0][$text]()).toBe(
  707. "Acme"
  708. );
  709. expect(
  710. searchNode(root, null, "$data.Receipt.Detail[0]")[0][$text]()
  711. ).toBe("Acme");
  712. expect(
  713. searchNode(root, null, "$data.Receipt.Detail[1]")[0][$text]()
  714. ).toBe("foo");
  715. expect(
  716. searchNode(root, null, "$data.Receipt.Detail[2]")[0][$text]()
  717. ).toBe("bar");
  718. });
  719. });
  720. describe("Bind data into form", function () {
  721. it("should make a basic binding", function () {
  722. const xml = `
  723. <?xml version="1.0"?>
  724. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  725. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  726. <subform name="A">
  727. <subform name="B">
  728. <field name="C">
  729. </field>
  730. <field name="D">
  731. </field>
  732. </subform>
  733. </subform>
  734. </template>
  735. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  736. <xfa:data>
  737. <A>
  738. <C>xyz</C>
  739. </A>
  740. </xfa:data>
  741. </xfa:datasets>
  742. </xdp:xdp>
  743. `;
  744. const root = new XFAParser().parse(xml);
  745. const form = new Binder(root).bind();
  746. expect(
  747. searchNode(form, form, "A.B.C.value.text")[0][$dump]().$content
  748. ).toBe("xyz");
  749. });
  750. it("should make a basic binding and create a non-existing node", function () {
  751. const xml = `
  752. <?xml version="1.0"?>
  753. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  754. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  755. <subform name="A" mergeMode="matchTemplate">
  756. <subform name="B">
  757. <field name="C">
  758. </field>
  759. <field name="D">
  760. <value>
  761. <text>foobar</text>
  762. </value>
  763. </field>
  764. </subform>
  765. </subform>
  766. </template>
  767. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  768. <xfa:data>
  769. <A>
  770. </A>
  771. </xfa:data>
  772. </xfa:datasets>
  773. </xdp:xdp>
  774. `;
  775. const root = new XFAParser().parse(xml);
  776. const binder = new Binder(root);
  777. const form = binder.bind();
  778. const data = binder.getData();
  779. expect(
  780. searchNode(form, form, "A.B.D.value.text")[0][$dump]().$content
  781. ).toBe("foobar");
  782. const expected = {
  783. $name: "A",
  784. attributes: {},
  785. children: [
  786. {
  787. $name: "B",
  788. attributes: {},
  789. children: [
  790. {
  791. $name: "C",
  792. attributes: {},
  793. children: [],
  794. },
  795. {
  796. $name: "D",
  797. attributes: {},
  798. children: [],
  799. },
  800. ],
  801. },
  802. ],
  803. };
  804. expect(searchNode(data, data, "A")[0][$dump]()).toEqual(expected);
  805. });
  806. it("should make a basic binding and create a non-existing node with namespaceId equal to -1", function () {
  807. const xml = `
  808. <?xml version="1.0"?>
  809. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  810. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  811. <subform name="A">
  812. <subform name="B">
  813. <field name="C">
  814. </field>
  815. <field name="D">
  816. <value>
  817. <text>foobar</text>
  818. </value>
  819. </field>
  820. </subform>
  821. </subform>
  822. </template>
  823. </xdp:xdp>
  824. `;
  825. const root = new XFAParser().parse(xml);
  826. const binder = new Binder(root);
  827. const form = binder.bind();
  828. const data = binder.getData();
  829. expect(
  830. searchNode(form, form, "A.B.D.value.text")[0][$dump]().$content
  831. ).toBe("foobar");
  832. // Created nodes mustn't belong to xfa:datasets namespace.
  833. const expected = {
  834. $name: "A",
  835. $ns: -1,
  836. attributes: {},
  837. children: [
  838. {
  839. $name: "B",
  840. $ns: -1,
  841. attributes: {},
  842. children: [
  843. {
  844. $name: "C",
  845. $ns: -1,
  846. attributes: {},
  847. children: [],
  848. },
  849. {
  850. $name: "D",
  851. $ns: -1,
  852. attributes: {},
  853. children: [],
  854. },
  855. ],
  856. },
  857. ],
  858. };
  859. expect(searchNode(data, data, "A")[0][$dump](/* hasNS */ true)).toEqual(
  860. expected
  861. );
  862. });
  863. it("should make another basic binding", function () {
  864. const xml = `
  865. <?xml version="1.0"?>
  866. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  867. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  868. <subform name="registration">
  869. <field name="first"> </field>
  870. <field name="last"> </field>
  871. <field name="apt"> </field>
  872. <field name="street"> </field>
  873. <field name="city"> </field>
  874. <field name="country"> </field>
  875. <field name="postalcode"/>
  876. </subform>
  877. </template>
  878. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  879. <xfa:data>
  880. <registration>
  881. <first>Jack</first>
  882. <last>Spratt</last>
  883. <apt/>
  884. <street>99 Candlestick Lane</street>
  885. <city>London</city>
  886. <country>UK</country>
  887. <postalcode>SW1</postalcode>
  888. </registration>
  889. </xfa:data>
  890. </xfa:datasets>
  891. </xdp:xdp>
  892. `;
  893. const root = new XFAParser().parse(xml);
  894. const form = new Binder(root).bind();
  895. expect(
  896. searchNode(form, form, "registration.first..text")[0][$dump]().$content
  897. ).toBe("Jack");
  898. expect(
  899. searchNode(form, form, "registration.last..text")[0][$dump]().$content
  900. ).toBe("Spratt");
  901. expect(
  902. searchNode(form, form, "registration.apt..text")[0][$dump]().$content
  903. ).toBe(undefined);
  904. expect(
  905. searchNode(form, form, "registration.street..text")[0][$dump]().$content
  906. ).toBe("99 Candlestick Lane");
  907. expect(
  908. searchNode(form, form, "registration.city..text")[0][$dump]().$content
  909. ).toBe("London");
  910. expect(
  911. searchNode(form, form, "registration.country..text")[0][$dump]()
  912. .$content
  913. ).toBe("UK");
  914. expect(
  915. searchNode(form, form, "registration.postalcode..text")[0][$dump]()
  916. .$content
  917. ).toBe("SW1");
  918. });
  919. it("should make basic binding with extra subform", function () {
  920. const xml = `
  921. <?xml version="1.0"?>
  922. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  923. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  924. <subform name="registration">
  925. <field name="first"> </field>
  926. <field name="last"> </field>
  927. <subform name="address">
  928. <field name="apt"> </field>
  929. <field name="street"> </field>
  930. <field name="city"> </field>
  931. <field name="country"> </field>
  932. <field name="postalcode"> </field>
  933. </subform>
  934. </subform>
  935. </template>
  936. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  937. <xfa:data>
  938. <registration>
  939. <first>Jack</first>
  940. <last>Spratt</last>
  941. <apt/>
  942. <street>99 Candlestick Lane</street>
  943. <city>London</city>
  944. <country>UK</country>
  945. <postalcode>SW1</postalcode>
  946. </registration>
  947. </xfa:data>
  948. </xfa:datasets>
  949. </xdp:xdp>
  950. `;
  951. const root = new XFAParser().parse(xml);
  952. const form = new Binder(root).bind();
  953. expect(
  954. searchNode(form, form, "registration..first..text")[0][$dump]().$content
  955. ).toBe("Jack");
  956. expect(
  957. searchNode(form, form, "registration..last..text")[0][$dump]().$content
  958. ).toBe("Spratt");
  959. expect(
  960. searchNode(form, form, "registration..apt..text")[0][$dump]().$content
  961. ).toBe(undefined);
  962. expect(
  963. searchNode(form, form, "registration..street..text")[0][$dump]()
  964. .$content
  965. ).toBe("99 Candlestick Lane");
  966. expect(
  967. searchNode(form, form, "registration..city..text")[0][$dump]().$content
  968. ).toBe("London");
  969. expect(
  970. searchNode(form, form, "registration..country..text")[0][$dump]()
  971. .$content
  972. ).toBe("UK");
  973. expect(
  974. searchNode(form, form, "registration..postalcode..text")[0][$dump]()
  975. .$content
  976. ).toBe("SW1");
  977. });
  978. it("should make basic binding with extra subform", function () {
  979. const xml = `
  980. <?xml version="1.0"?>
  981. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  982. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  983. <subform name="registration" mergeMode="consumeData">
  984. <subform name="address">
  985. <field name="first"/>
  986. <field name="last"/>
  987. <field name="apt"/>
  988. <field name="street"/>
  989. <field name="city"/>
  990. </subform>
  991. </subform>
  992. </template>
  993. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  994. <xfa:data>
  995. <registration>
  996. <first>Jack</first>
  997. <last>Spratt</last>
  998. <address>
  999. <apt>7</apt>
  1000. <street>99 Candlestick Lane</street>
  1001. <city>London</city>
  1002. </address>
  1003. </registration>
  1004. </xfa:data>
  1005. </xfa:datasets>
  1006. </xdp:xdp>
  1007. `;
  1008. const root = new XFAParser().parse(xml);
  1009. const form = new Binder(root).bind();
  1010. expect(
  1011. searchNode(form, form, "registration..first..text")[0][$dump]().$content
  1012. ).toBe("Jack");
  1013. expect(
  1014. searchNode(form, form, "registration..last..text")[0][$dump]().$content
  1015. ).toBe("Spratt");
  1016. expect(
  1017. searchNode(form, form, "registration..apt..text")[0][$dump]().$content
  1018. ).toBe("7");
  1019. expect(
  1020. searchNode(form, form, "registration..street..text")[0][$dump]()
  1021. .$content
  1022. ).toBe("99 Candlestick Lane");
  1023. expect(
  1024. searchNode(form, form, "registration..city..text")[0][$dump]().$content
  1025. ).toBe("London");
  1026. });
  1027. it("should make basic binding with same names in different parts", function () {
  1028. const xml = `
  1029. <?xml version="1.0"?>
  1030. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1031. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1032. <subform name="application" mergeMode="consumeData">
  1033. <subform name="sponsor">
  1034. <field name="lastname"> </field>
  1035. <!-- sponsor's last name -->
  1036. </subform>
  1037. <field name="lastname"> </field>
  1038. <!-- applicant's last name -->
  1039. </subform>
  1040. </template>
  1041. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1042. <xfa:data>
  1043. <application>
  1044. <lastname>Abott</lastname>
  1045. <sponsor>
  1046. <lastname>Costello</lastname>
  1047. </sponsor>
  1048. </application>
  1049. </xfa:data>
  1050. </xfa:datasets>
  1051. </xdp:xdp>
  1052. `;
  1053. const root = new XFAParser().parse(xml);
  1054. const form = new Binder(root).bind();
  1055. expect(
  1056. searchNode(form, form, "application.sponsor.lastname..text")[0][$dump]()
  1057. .$content
  1058. ).toBe("Costello");
  1059. expect(
  1060. searchNode(form, form, "application.lastname..text")[0][$dump]()
  1061. .$content
  1062. ).toBe("Abott");
  1063. });
  1064. it("should make binding and create nodes in data", function () {
  1065. const xml = `
  1066. <?xml version="1.0"?>
  1067. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1068. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1069. <subform name="root" mergeMode="matchTemplate">
  1070. <subform name="A">
  1071. <field name="a"/>
  1072. <field name="b"/>
  1073. <subform name="B">
  1074. <field name="c"/>
  1075. <field name="d"/>
  1076. <subform name="C">
  1077. <field name="e"/>
  1078. <field name="f"/>
  1079. </subform>
  1080. </subform>
  1081. </subform>
  1082. </subform>
  1083. </template>
  1084. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1085. <xfa:data>
  1086. <root>
  1087. <A>
  1088. <b>1</b>
  1089. </A>
  1090. </root>
  1091. </xfa:data>
  1092. </xfa:datasets>
  1093. </xdp:xdp>
  1094. `;
  1095. const root = new XFAParser().parse(xml);
  1096. const binder = new Binder(root);
  1097. const form = binder.bind();
  1098. const data = binder.getData();
  1099. expect(searchNode(form, form, "root..b..text")[0][$dump]().$content).toBe(
  1100. "1"
  1101. );
  1102. expect(searchNode(data, data, "root.A.a")[0][$dump]().$name).toBe("a");
  1103. expect(searchNode(data, data, "root.A.B.c")[0][$dump]().$name).toBe("c");
  1104. expect(searchNode(data, data, "root.A.B.d")[0][$dump]().$name).toBe("d");
  1105. expect(searchNode(data, data, "root.A.B.C.e")[0][$dump]().$name).toBe(
  1106. "e"
  1107. );
  1108. expect(searchNode(data, data, "root.A.B.C.f")[0][$dump]().$name).toBe(
  1109. "f"
  1110. );
  1111. });
  1112. it("should make binding and set properties", function () {
  1113. const xml = `
  1114. <?xml version="1.0"?>
  1115. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1116. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1117. <subform name="Id">
  1118. <field name="LastName">
  1119. <setProperty ref="$data.Main.Style.NameFont" target="font.typeface"/>
  1120. <setProperty ref="$data.Main.Style.NameSize" target="font.size"/>
  1121. <setProperty ref="$data.Main.Help.LastName" target="assist.toolTip"/>
  1122. <font></font>
  1123. <assist>
  1124. <toolTip>
  1125. </toolTip>
  1126. </assist>
  1127. </field>
  1128. </subform>
  1129. </template>
  1130. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1131. <xfa:data>
  1132. <Id>
  1133. <LastName>foo</LastName>
  1134. </Id>
  1135. <Main>
  1136. <Style>
  1137. <NameFont>myfont</NameFont>
  1138. <NameSize>123.4pt</NameSize>
  1139. </Style>
  1140. <Help>
  1141. <LastName>Give the name!</LastName>
  1142. </Help>
  1143. </Main>
  1144. </xfa:data>
  1145. </xfa:datasets>
  1146. </xdp:xdp>
  1147. `;
  1148. const root = new XFAParser().parse(xml);
  1149. const form = new Binder(root).bind();
  1150. expect(
  1151. searchNode(form, form, "Id.LastName..text")[0][$dump]().$content
  1152. ).toBe("foo");
  1153. expect(
  1154. searchNode(form, form, "Id.LastName.font.typeface")[0][$text]()
  1155. ).toBe("myfont");
  1156. expect(
  1157. searchNode(form, form, "Id.LastName.font.size")[0][$text]()
  1158. ).toEqual(123.4);
  1159. expect(
  1160. searchNode(form, form, "Id.LastName.assist.toolTip")[0][$dump]()
  1161. .$content
  1162. ).toBe("Give the name!");
  1163. });
  1164. it("should make binding and bind items", function () {
  1165. const xml = `
  1166. <?xml version="1.0"?>
  1167. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1168. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1169. <subform name="main">
  1170. <field name="CardName">
  1171. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  1172. <ui>
  1173. <choiceList/>
  1174. </ui>
  1175. </field>
  1176. </subform>
  1177. </template>
  1178. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1179. <xfa:data>
  1180. <main>
  1181. <ccs>
  1182. <cc uiname="Visa" token="VISA"/>
  1183. <cc uiname="Mastercard" token="MC"/>
  1184. <cc uiname="American Express" token="AMEX"/>
  1185. </ccs>
  1186. <CardName>MC</CardName>
  1187. </main>
  1188. </xfa:data>
  1189. </xfa:datasets>
  1190. </xdp:xdp>
  1191. `;
  1192. const root = new XFAParser().parse(xml);
  1193. const form = new Binder(root).bind();
  1194. expect(
  1195. searchNode(form, form, "subform.CardName.items[*].text[*]").map(x =>
  1196. x[$text]()
  1197. )
  1198. ).toEqual([
  1199. "Visa",
  1200. "Mastercard",
  1201. "American Express",
  1202. "VISA",
  1203. "MC",
  1204. "AMEX",
  1205. ]);
  1206. });
  1207. it("should make binding and bind items with a ref", function () {
  1208. const xml = `
  1209. <?xml version="1.0"?>
  1210. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1211. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1212. <subform name="main">
  1213. <field name="CardName">
  1214. <bind match="dataRef" ref="$data.main.value"/>
  1215. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  1216. <ui>
  1217. <choiceList/>
  1218. </ui>
  1219. </field>
  1220. </subform>
  1221. </template>
  1222. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1223. <xfa:data>
  1224. <main>
  1225. <value>VISA</value>
  1226. <ccs>
  1227. <cc uiname="Visa" token="VISA"/>
  1228. <cc uiname="Mastercard" token="MC"/>
  1229. <cc uiname="American Express" token="AMEX"/>
  1230. </ccs>
  1231. <CardName>MC</CardName>
  1232. </main>
  1233. </xfa:data>
  1234. </xfa:datasets>
  1235. </xdp:xdp>
  1236. `;
  1237. const root = new XFAParser().parse(xml);
  1238. const form = new Binder(root).bind();
  1239. expect(
  1240. searchNode(form, form, "subform.CardName.value.text").map(x =>
  1241. x[$text]()
  1242. )
  1243. ).toEqual(["VISA"]);
  1244. expect(
  1245. searchNode(form, form, "subform.CardName.items[*].text[*]").map(x =>
  1246. x[$text]()
  1247. )
  1248. ).toEqual([
  1249. "Visa",
  1250. "Mastercard",
  1251. "American Express",
  1252. "VISA",
  1253. "MC",
  1254. "AMEX",
  1255. ]);
  1256. });
  1257. it("should make binding with occurrences in consumeData mode", function () {
  1258. const xml = `
  1259. <?xml version="1.0"?>
  1260. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1261. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1262. <subform name="root" mergeMode="consumeData">
  1263. <subform name="section" id="section1">
  1264. <occur min="0" max="-1"/>
  1265. <bind match="dataRef" ref="$.section[*]"/>
  1266. <field name="line-item"/>
  1267. </subform>
  1268. <subform name="section" id="section2">
  1269. <occur min="0" max="-1"/>
  1270. <bind match="dataRef" ref="$.section[*]"/>
  1271. <field name="line-item"/>
  1272. </subform>
  1273. </subform>
  1274. </template>
  1275. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1276. <xfa:data>
  1277. <root>
  1278. <section>
  1279. <line-item>item1</line-item>
  1280. </section>
  1281. <section>
  1282. <line-item>item2</line-item>
  1283. </section>
  1284. </root>
  1285. </xfa:data>
  1286. </xfa:datasets>
  1287. </xdp:xdp>
  1288. `;
  1289. const root = new XFAParser().parse(xml);
  1290. const form = new Binder(root).bind();
  1291. expect(
  1292. searchNode(form, form, "root.section[*].id").map(x => x[$text]())
  1293. ).toEqual(["section1", "section1"]);
  1294. expect(
  1295. searchNode(form, form, "root.section[*].line-item..text").map(x =>
  1296. x[$text]()
  1297. )
  1298. ).toEqual(["item1", "item2"]);
  1299. });
  1300. it("should make binding with occurrences in matchTemplate mode", function () {
  1301. const xml = `
  1302. <?xml version="1.0"?>
  1303. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1304. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1305. <subform name="root" mergeMode="matchTemplate">
  1306. <subform name="section" id="section1">
  1307. <occur min="0" max="-1"/>
  1308. <bind match="dataRef" ref="$.section[*]"/>
  1309. <field name="line-item"/>
  1310. </subform>
  1311. <subform name="section" id="section2">
  1312. <occur min="0" max="-1"/>
  1313. <bind match="dataRef" ref="$.section[*]"/>
  1314. <field name="line-item"/>
  1315. </subform>
  1316. </subform>
  1317. </template>
  1318. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1319. <xfa:data>
  1320. <root>
  1321. <section>
  1322. <line-item>item1</line-item>
  1323. </section>
  1324. <section>
  1325. <line-item>item2</line-item>
  1326. </section>
  1327. </root>
  1328. </xfa:data>
  1329. </xfa:datasets>
  1330. </xdp:xdp>
  1331. `;
  1332. const root = new XFAParser().parse(xml);
  1333. const form = new Binder(root).bind();
  1334. expect(
  1335. searchNode(form, form, "root.section[*].id").map(x => x[$text]())
  1336. ).toEqual(["section1", "section1", "section2", "section2"]);
  1337. expect(
  1338. searchNode(form, form, "root.section[*].line-item..text").map(x =>
  1339. x[$text]()
  1340. )
  1341. ).toEqual(["item1", "item2", "item1", "item2"]);
  1342. });
  1343. it("should make binding and create nodes in data with some bind tag", function () {
  1344. const xml = `
  1345. <?xml version="1.0"?>
  1346. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1347. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1348. <subform name="root" mergeMode="matchTemplate">
  1349. <subform name="A">
  1350. <occur max="-1"/>
  1351. <bind ref="$.root.foo[*]" match="dataRef"/>
  1352. </subform>
  1353. <subform name="B">
  1354. <occur max="2"/>
  1355. <bind ref="$.root.bar[2]" match="dataRef"/>
  1356. </subform>
  1357. </subform>
  1358. </template>
  1359. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1360. <xfa:data>
  1361. <root>
  1362. </root>
  1363. </xfa:data>
  1364. </xfa:datasets>
  1365. </xdp:xdp>
  1366. `;
  1367. const root = new XFAParser().parse(xml);
  1368. const binder = new Binder(root);
  1369. binder.bind();
  1370. const data = binder.getData();
  1371. const expected = {
  1372. $name: "root",
  1373. children: [
  1374. {
  1375. $name: "root",
  1376. children: [
  1377. {
  1378. $name: "foo",
  1379. children: [],
  1380. attributes: {},
  1381. },
  1382. {
  1383. $name: "bar",
  1384. children: [],
  1385. attributes: {},
  1386. },
  1387. {
  1388. $name: "bar",
  1389. children: [],
  1390. attributes: {},
  1391. },
  1392. {
  1393. $name: "bar",
  1394. children: [],
  1395. attributes: {},
  1396. },
  1397. ],
  1398. attributes: {},
  1399. },
  1400. ],
  1401. attributes: {},
  1402. };
  1403. expect(searchNode(data, data, "root")[0][$dump]()).toEqual(expected);
  1404. });
  1405. it("should make a binding with a bindItems", function () {
  1406. const xml = `
  1407. <?xml version="1.0"?>
  1408. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1409. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1410. <subform name="A" mergeMode="matchTemplate">
  1411. <subform name="B">
  1412. <field name="C">
  1413. <ui>
  1414. <choicelist/>
  1415. </ui>
  1416. <bindItems ref="xfa.datasets.foo.bar[*]" labelRef="$" valueRef="oof"/>
  1417. </field>
  1418. </subform>
  1419. </subform>
  1420. </template>
  1421. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1422. <foo>
  1423. <bar oof="a">1</bar>
  1424. <bar oof="b">2</bar>
  1425. <bar oof="c">3</bar>
  1426. <bar oof="d">4</bar>
  1427. <bar oof="e">5</bar>
  1428. </foo>
  1429. <xfa:data>
  1430. <A><B></B></A>
  1431. </xfa:data>
  1432. </xfa:datasets>
  1433. </xdp:xdp>
  1434. `;
  1435. const root = new XFAParser().parse(xml);
  1436. const form = new Binder(root).bind();
  1437. expect(
  1438. searchNode(form, form, "A.B.C.items[0].text[*]").map(
  1439. x => x[$dump]().$content
  1440. )
  1441. ).toEqual(["1", "2", "3", "4", "5"]);
  1442. expect(
  1443. searchNode(form, form, "A.B.C.items[1].text[*]").map(
  1444. x => x[$dump]().$content
  1445. )
  1446. ).toEqual(["a", "b", "c", "d", "e"]);
  1447. });
  1448. });
  1449. it("should make a binding with a element in an area", function () {
  1450. const xml = `
  1451. <?xml version="1.0"?>
  1452. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1453. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1454. <subform name="A" mergeMode="matchTemplate">
  1455. <area>
  1456. <field name="B"/>
  1457. </area>
  1458. </subform>
  1459. </template>
  1460. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1461. <xfa:data>
  1462. <A><B>foobar</B></A>
  1463. </xfa:data>
  1464. </xfa:datasets>
  1465. </xdp:xdp>
  1466. `;
  1467. const root = new XFAParser().parse(xml);
  1468. const form = new Binder(root).bind();
  1469. expect(searchNode(form, form, "A..B..text")[0][$dump]().$content).toBe(
  1470. "foobar"
  1471. );
  1472. });
  1473. });