xfa_parser_spec.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _xfa_object = require("../../core/xfa/xfa_object.js");
  24. var _bind = require("../../core/xfa/bind.js");
  25. var _som = require("../../core/xfa/som.js");
  26. var _parser = require("../../core/xfa/parser.js");
  27. describe("XFAParser", function () {
  28. describe("Parse XFA", function () {
  29. it("should parse a xfa document and create an object to represent it", function () {
  30. const xml = `
  31. <?xml version="1.0"?>
  32. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" uuid="1234" invalid="foo">
  33. <config xmlns="http://www.xfa.org/schema/xci/3.1/">
  34. <present>
  35. <pdf name="hello">
  36. <adobeExtensionLevel>
  37. 7
  38. </adobeExtensionLevel>
  39. </pdf>
  40. <invalid><a>foobar</a></invalid>
  41. </present>
  42. <acrobat>
  43. <submitUrl>http://a.b.c</submitUrl>
  44. <acrobat7>
  45. <dynamicRender>
  46. forbidden
  47. </dynamicRender>
  48. </acrobat7>
  49. <autoSave>enabled</autoSave>
  50. <submitUrl>
  51. http://d.e.f
  52. </submitUrl>
  53. <submitUrl>http://g.h.i</submitUrl>
  54. <validate>foobar</validate>
  55. </acrobat>
  56. </config>
  57. <template baseProfile="full" xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  58. <extras>
  59. <float>1.23</float>
  60. <boolean>1</boolean>
  61. <integer>314</integer>
  62. <float>2.71</float>
  63. </extras>
  64. <subform>
  65. <proto>
  66. <area x="hello" y="-3.14in" relevant="-foo +bar" />
  67. <color value="111, 222, 123" />
  68. <color value="111, abc, 123" />
  69. <medium imagingBBox="1,2in,3.4cm,5.67px" />
  70. <medium imagingBBox="1,2in,-3cm,4px" />
  71. </proto>
  72. </subform>
  73. </template>
  74. </xdp:xdp>
  75. `;
  76. const attributes = {
  77. id: "",
  78. name: "",
  79. use: "",
  80. usehref: ""
  81. };
  82. const mediumAttributes = {
  83. id: "",
  84. long: 0,
  85. orientation: "portrait",
  86. short: 0,
  87. stock: "",
  88. trayIn: "auto",
  89. trayOut: "auto",
  90. use: "",
  91. usehref: ""
  92. };
  93. const colorAttributes = {
  94. cSpace: "SRGB",
  95. id: "",
  96. use: "",
  97. usehref: ""
  98. };
  99. const root = new _parser.XFAParser().parse(xml);
  100. const expected = {
  101. uuid: "1234",
  102. timeStamp: "",
  103. template: {
  104. baseProfile: "full",
  105. extras: { ...attributes,
  106. float: [{ ...attributes,
  107. $content: 1.23
  108. }, { ...attributes,
  109. $content: 2.71
  110. }],
  111. boolean: { ...attributes,
  112. $content: 1
  113. },
  114. integer: { ...attributes,
  115. $content: 314
  116. }
  117. },
  118. subform: {
  119. access: "open",
  120. allowMacro: 0,
  121. anchorType: "topLeft",
  122. colSpan: 1,
  123. columnWidths: [0],
  124. h: "",
  125. hAlign: "left",
  126. id: "",
  127. layout: "position",
  128. locale: "",
  129. maxH: 0,
  130. maxW: 0,
  131. mergeMode: "consumeData",
  132. minH: 0,
  133. minW: 0,
  134. name: "",
  135. presence: "visible",
  136. relevant: [],
  137. restoreState: "manual",
  138. scope: "name",
  139. use: "",
  140. usehref: "",
  141. w: "",
  142. x: 0,
  143. y: 0,
  144. proto: {
  145. area: { ...attributes,
  146. colSpan: 1,
  147. x: 0,
  148. y: -226.08,
  149. relevant: [{
  150. excluded: true,
  151. viewname: "foo"
  152. }, {
  153. excluded: false,
  154. viewname: "bar"
  155. }]
  156. },
  157. color: [{ ...colorAttributes,
  158. value: {
  159. r: 111,
  160. g: 222,
  161. b: 123
  162. }
  163. }, { ...colorAttributes,
  164. value: {
  165. r: 111,
  166. g: 0,
  167. b: 123
  168. }
  169. }],
  170. medium: [{ ...mediumAttributes,
  171. imagingBBox: {
  172. x: 1,
  173. y: 144,
  174. width: 96.3779527559055,
  175. height: 5.67
  176. }
  177. }, { ...mediumAttributes,
  178. imagingBBox: {
  179. x: -1,
  180. y: -1,
  181. width: -1,
  182. height: -1
  183. }
  184. }]
  185. }
  186. }
  187. },
  188. config: {
  189. acrobat: {
  190. acrobat7: {
  191. dynamicRender: {
  192. $content: "forbidden"
  193. }
  194. },
  195. autoSave: {
  196. $content: "enabled"
  197. },
  198. validate: {
  199. $content: "preSubmit"
  200. },
  201. submitUrl: [{
  202. $content: "http://a.b.c"
  203. }, {
  204. $content: "http://d.e.f"
  205. }, {
  206. $content: "http://g.h.i"
  207. }]
  208. },
  209. present: {
  210. pdf: {
  211. name: "hello",
  212. adobeExtensionLevel: {
  213. $content: 7
  214. }
  215. }
  216. }
  217. }
  218. };
  219. expect(root[_xfa_object.$dump]()).toEqual(expected);
  220. });
  221. it("should parse a xfa document and check namespaces", function () {
  222. const xml = `
  223. <?xml version="1.0"?>
  224. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  225. <config xmlns:foo="http:/www.foo.com" xmlns="http://www.xfa.org/schema/xci/3.1/">
  226. <present xmlns="http://www.mozilla.org">
  227. <pdf name="hello">
  228. <adobeExtensionLevel>
  229. 7
  230. </adobeExtensionLevel>
  231. </pdf>
  232. </present>
  233. <acrobat>
  234. <foo:submitUrl>http://a.b.c</foo:submitUrl>
  235. <submitUrl>http://c.b.a</submitUrl>
  236. </acrobat>
  237. </config>
  238. <template baseProfile="full" xmlns="http://www.allizom.org">
  239. <extras>
  240. <float>1.23</float>
  241. </extras>
  242. </template>
  243. </xdp:xdp>
  244. `;
  245. const root = new _parser.XFAParser().parse(xml);
  246. const expected = {
  247. uuid: "",
  248. timeStamp: "",
  249. config: {
  250. acrobat: {
  251. submitUrl: {
  252. $content: "http://c.b.a"
  253. }
  254. }
  255. }
  256. };
  257. expect(root[_xfa_object.$dump]()).toEqual(expected);
  258. });
  259. it("should parse a xfa document and 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 \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 another basic binding", 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="registration">
  781. <field name="first"> </field>
  782. <field name="last"> </field>
  783. <field name="apt"> </field>
  784. <field name="street"> </field>
  785. <field name="city"> </field>
  786. <field name="country"> </field>
  787. <field name="postalcode"/>
  788. </subform>
  789. </template>
  790. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  791. <xfa:data>
  792. <registration>
  793. <first>Jack</first>
  794. <last>Spratt</last>
  795. <apt/>
  796. <street>99 Candlestick Lane</street>
  797. <city>London</city>
  798. <country>UK</country>
  799. <postalcode>SW1</postalcode>
  800. </registration>
  801. </xfa:data>
  802. </xfa:datasets>
  803. </xdp:xdp>
  804. `;
  805. const root = new _parser.XFAParser().parse(xml);
  806. const form = new _bind.Binder(root).bind();
  807. expect((0, _som.searchNode)(form, form, "registration.first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  808. expect((0, _som.searchNode)(form, form, "registration.last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  809. expect((0, _som.searchNode)(form, form, "registration.apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  810. expect((0, _som.searchNode)(form, form, "registration.street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  811. expect((0, _som.searchNode)(form, form, "registration.city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  812. expect((0, _som.searchNode)(form, form, "registration.country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  813. expect((0, _som.searchNode)(form, form, "registration.postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  814. });
  815. it("should make basic binding with extra subform", function () {
  816. const xml = `
  817. <?xml version="1.0"?>
  818. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  819. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  820. <subform name="registration">
  821. <field name="first"> </field>
  822. <field name="last"> </field>
  823. <subform name="address">
  824. <field name="apt"> </field>
  825. <field name="street"> </field>
  826. <field name="city"> </field>
  827. <field name="country"> </field>
  828. <field name="postalcode"> </field>
  829. </subform>
  830. </subform>
  831. </template>
  832. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  833. <xfa:data>
  834. <registration>
  835. <first>Jack</first>
  836. <last>Spratt</last>
  837. <apt/>
  838. <street>99 Candlestick Lane</street>
  839. <city>London</city>
  840. <country>UK</country>
  841. <postalcode>SW1</postalcode>
  842. </registration>
  843. </xfa:data>
  844. </xfa:datasets>
  845. </xdp:xdp>
  846. `;
  847. const root = new _parser.XFAParser().parse(xml);
  848. const form = new _bind.Binder(root).bind();
  849. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  850. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  851. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe(undefined);
  852. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  853. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  854. expect((0, _som.searchNode)(form, form, "registration..country..text")[0][_xfa_object.$dump]().$content).toBe("UK");
  855. expect((0, _som.searchNode)(form, form, "registration..postalcode..text")[0][_xfa_object.$dump]().$content).toBe("SW1");
  856. });
  857. it("should make basic binding with extra subform", function () {
  858. const xml = `
  859. <?xml version="1.0"?>
  860. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  861. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  862. <subform name="registration" mergeMode="consumeData">
  863. <subform name="address">
  864. <field name="first"/>
  865. <field name="last"/>
  866. <field name="apt"/>
  867. <field name="street"/>
  868. <field name="city"/>
  869. </subform>
  870. </subform>
  871. </template>
  872. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  873. <xfa:data>
  874. <registration>
  875. <first>Jack</first>
  876. <last>Spratt</last>
  877. <address>
  878. <apt>7</apt>
  879. <street>99 Candlestick Lane</street>
  880. <city>London</city>
  881. </address>
  882. </registration>
  883. </xfa:data>
  884. </xfa:datasets>
  885. </xdp:xdp>
  886. `;
  887. const root = new _parser.XFAParser().parse(xml);
  888. const form = new _bind.Binder(root).bind();
  889. expect((0, _som.searchNode)(form, form, "registration..first..text")[0][_xfa_object.$dump]().$content).toBe("Jack");
  890. expect((0, _som.searchNode)(form, form, "registration..last..text")[0][_xfa_object.$dump]().$content).toBe("Spratt");
  891. expect((0, _som.searchNode)(form, form, "registration..apt..text")[0][_xfa_object.$dump]().$content).toBe("7");
  892. expect((0, _som.searchNode)(form, form, "registration..street..text")[0][_xfa_object.$dump]().$content).toBe("99 Candlestick Lane");
  893. expect((0, _som.searchNode)(form, form, "registration..city..text")[0][_xfa_object.$dump]().$content).toBe("London");
  894. });
  895. it("should make basic binding with same names in different parts", function () {
  896. const xml = `
  897. <?xml version="1.0"?>
  898. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  899. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  900. <subform name="application" mergeMode="consumeData">
  901. <subform name="sponsor">
  902. <field name="lastname"> </field>
  903. <!-- sponsor's last name -->
  904. </subform>
  905. <field name="lastname"> </field>
  906. <!-- applicant's last name -->
  907. </subform>
  908. </template>
  909. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  910. <xfa:data>
  911. <application>
  912. <lastname>Abott</lastname>
  913. <sponsor>
  914. <lastname>Costello</lastname>
  915. </sponsor>
  916. </application>
  917. </xfa:data>
  918. </xfa:datasets>
  919. </xdp:xdp>
  920. `;
  921. const root = new _parser.XFAParser().parse(xml);
  922. const form = new _bind.Binder(root).bind();
  923. expect((0, _som.searchNode)(form, form, "application.sponsor.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Costello");
  924. expect((0, _som.searchNode)(form, form, "application.lastname..text")[0][_xfa_object.$dump]().$content).toBe("Abott");
  925. });
  926. it("should make binding and create nodes in data", function () {
  927. const xml = `
  928. <?xml version="1.0"?>
  929. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  930. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  931. <subform name="root" mergeMode="matchTemplate">
  932. <subform name="A">
  933. <field name="a"/>
  934. <field name="b"/>
  935. <subform name="B">
  936. <field name="c"/>
  937. <field name="d"/>
  938. <subform name="C">
  939. <field name="e"/>
  940. <field name="f"/>
  941. </subform>
  942. </subform>
  943. </subform>
  944. </subform>
  945. </template>
  946. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  947. <xfa:data>
  948. <root>
  949. <A>
  950. <b>1</b>
  951. </A>
  952. </root>
  953. </xfa:data>
  954. </xfa:datasets>
  955. </xdp:xdp>
  956. `;
  957. const root = new _parser.XFAParser().parse(xml);
  958. const binder = new _bind.Binder(root);
  959. const form = binder.bind();
  960. const data = binder.getData();
  961. expect((0, _som.searchNode)(form, form, "root..b..text")[0][_xfa_object.$dump]().$content).toBe("1");
  962. expect((0, _som.searchNode)(data, data, "root.A.a")[0][_xfa_object.$dump]().$name).toBe("a");
  963. expect((0, _som.searchNode)(data, data, "root.A.B.c")[0][_xfa_object.$dump]().$name).toBe("c");
  964. expect((0, _som.searchNode)(data, data, "root.A.B.d")[0][_xfa_object.$dump]().$name).toBe("d");
  965. expect((0, _som.searchNode)(data, data, "root.A.B.C.e")[0][_xfa_object.$dump]().$name).toBe("e");
  966. expect((0, _som.searchNode)(data, data, "root.A.B.C.f")[0][_xfa_object.$dump]().$name).toBe("f");
  967. });
  968. it("should make binding and set properties", function () {
  969. const xml = `
  970. <?xml version="1.0"?>
  971. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  972. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  973. <subform name="Id">
  974. <field name="LastName">
  975. <setProperty ref="$data.Main.Style.NameFont" target="font.typeface"/>
  976. <setProperty ref="$data.Main.Style.NameSize" target="font.size"/>
  977. <setProperty ref="$data.Main.Help.LastName" target="assist.toolTip"/>
  978. <font></font>
  979. <assist>
  980. <toolTip>
  981. </toolTip>
  982. </assist>
  983. </field>
  984. </subform>
  985. </template>
  986. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  987. <xfa:data>
  988. <Id>
  989. <LastName>foo</LastName>
  990. </Id>
  991. <Main>
  992. <Style>
  993. <NameFont>myfont</NameFont>
  994. <NameSize>123.4pt</NameSize>
  995. </Style>
  996. <Help>
  997. <LastName>Give the name!</LastName>
  998. </Help>
  999. </Main>
  1000. </xfa:data>
  1001. </xfa:datasets>
  1002. </xdp:xdp>
  1003. `;
  1004. const root = new _parser.XFAParser().parse(xml);
  1005. const form = new _bind.Binder(root).bind();
  1006. expect((0, _som.searchNode)(form, form, "Id.LastName..text")[0][_xfa_object.$dump]().$content).toBe("foo");
  1007. expect((0, _som.searchNode)(form, form, "Id.LastName.font.typeface")[0][_xfa_object.$text]()).toBe("myfont");
  1008. expect((0, _som.searchNode)(form, form, "Id.LastName.font.size")[0][_xfa_object.$text]()).toEqual(123.4);
  1009. expect((0, _som.searchNode)(form, form, "Id.LastName.assist.toolTip")[0][_xfa_object.$dump]().$content).toBe("Give the name!");
  1010. });
  1011. it("should make binding and bind items", function () {
  1012. const xml = `
  1013. <?xml version="1.0"?>
  1014. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1015. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1016. <subform name="main">
  1017. <field name="CardName">
  1018. <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>
  1019. <ui>
  1020. <choiceList/>
  1021. </ui>
  1022. </field>
  1023. </subform>
  1024. </template>
  1025. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1026. <xfa:data>
  1027. <main>
  1028. <ccs>
  1029. <cc uiname="Visa" token="VISA"/>
  1030. <cc uiname="Mastercard" token="MC"/>
  1031. <cc uiname="American Express" token="AMEX"/>
  1032. </ccs>
  1033. <CardName>MC</CardName>
  1034. </main>
  1035. </xfa:data>
  1036. </xfa:datasets>
  1037. </xdp:xdp>
  1038. `;
  1039. const root = new _parser.XFAParser().parse(xml);
  1040. const form = new _bind.Binder(root).bind();
  1041. expect((0, _som.searchNode)(form, form, "subform.CardName.items[*].text[*]").map(x => x[_xfa_object.$text]())).toEqual(["Visa", "Mastercard", "American Express", "VISA", "MC", "AMEX"]);
  1042. });
  1043. it("should make binding with occurrences in consumeData mode", function () {
  1044. const xml = `
  1045. <?xml version="1.0"?>
  1046. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1047. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1048. <subform name="root" mergeMode="consumeData">
  1049. <subform name="section" id="section1">
  1050. <occur min="0" max="-1"/>
  1051. <bind match="dataRef" ref="$.section[*]"/>
  1052. <field name="line-item"/>
  1053. </subform>
  1054. <subform name="section" id="section2">
  1055. <occur min="0" max="-1"/>
  1056. <bind match="dataRef" ref="$.section[*]"/>
  1057. <field name="line-item"/>
  1058. </subform>
  1059. </subform>
  1060. </template>
  1061. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1062. <xfa:data>
  1063. <root>
  1064. <section>
  1065. <line-item>item1</line-item>
  1066. </section>
  1067. <section>
  1068. <line-item>item2</line-item>
  1069. </section>
  1070. </root>
  1071. </xfa:data>
  1072. </xfa:datasets>
  1073. </xdp:xdp>
  1074. `;
  1075. const root = new _parser.XFAParser().parse(xml);
  1076. const form = new _bind.Binder(root).bind();
  1077. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1"]);
  1078. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2"]);
  1079. });
  1080. it("should make binding with occurrences in matchTemplate mode", function () {
  1081. const xml = `
  1082. <?xml version="1.0"?>
  1083. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1084. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1085. <subform name="root" mergeMode="matchTemplate">
  1086. <subform name="section" id="section1">
  1087. <occur min="0" max="-1"/>
  1088. <bind match="dataRef" ref="$.section[*]"/>
  1089. <field name="line-item"/>
  1090. </subform>
  1091. <subform name="section" id="section2">
  1092. <occur min="0" max="-1"/>
  1093. <bind match="dataRef" ref="$.section[*]"/>
  1094. <field name="line-item"/>
  1095. </subform>
  1096. </subform>
  1097. </template>
  1098. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1099. <xfa:data>
  1100. <root>
  1101. <section>
  1102. <line-item>item1</line-item>
  1103. </section>
  1104. <section>
  1105. <line-item>item2</line-item>
  1106. </section>
  1107. </root>
  1108. </xfa:data>
  1109. </xfa:datasets>
  1110. </xdp:xdp>
  1111. `;
  1112. const root = new _parser.XFAParser().parse(xml);
  1113. const form = new _bind.Binder(root).bind();
  1114. expect((0, _som.searchNode)(form, form, "root.section[*].id").map(x => x[_xfa_object.$text]())).toEqual(["section1", "section1", "section2", "section2"]);
  1115. expect((0, _som.searchNode)(form, form, "root.section[*].line-item..text").map(x => x[_xfa_object.$text]())).toEqual(["item1", "item2", "item1", "item2"]);
  1116. });
  1117. it("should make binding and create nodes in data with some bind tag", function () {
  1118. const xml = `
  1119. <?xml version="1.0"?>
  1120. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1121. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1122. <subform name="root" mergeMode="matchTemplate">
  1123. <subform name="A">
  1124. <occur max="-1"/>
  1125. <bind ref="$.root.foo[*]" match="dataRef"/>
  1126. </subform>
  1127. <subform name="B">
  1128. <occur max="2"/>
  1129. <bind ref="$.root.bar[2]" match="dataRef"/>
  1130. </subform>
  1131. </subform>
  1132. </template>
  1133. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1134. <xfa:data>
  1135. <root>
  1136. </root>
  1137. </xfa:data>
  1138. </xfa:datasets>
  1139. </xdp:xdp>
  1140. `;
  1141. const root = new _parser.XFAParser().parse(xml);
  1142. const binder = new _bind.Binder(root);
  1143. binder.bind();
  1144. const data = binder.getData();
  1145. const expected = {
  1146. $name: "root",
  1147. children: [{
  1148. $name: "root",
  1149. children: [{
  1150. $name: "foo",
  1151. children: [],
  1152. attributes: {}
  1153. }, {
  1154. $name: "bar",
  1155. children: [],
  1156. attributes: {}
  1157. }, {
  1158. $name: "bar",
  1159. children: [],
  1160. attributes: {}
  1161. }, {
  1162. $name: "bar",
  1163. children: [],
  1164. attributes: {}
  1165. }],
  1166. attributes: {}
  1167. }],
  1168. attributes: {}
  1169. };
  1170. expect((0, _som.searchNode)(data, data, "root")[0][_xfa_object.$dump]()).toEqual(expected);
  1171. });
  1172. it("should make a binding with a bindItems", function () {
  1173. const xml = `
  1174. <?xml version="1.0"?>
  1175. <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  1176. <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
  1177. <subform name="A" mergeMode="matchTemplate">
  1178. <subform name="B">
  1179. <field name="C">
  1180. <ui>
  1181. <choicelist/>
  1182. </ui>
  1183. <bindItems ref="xfa.datasets.foo.bar[*]" labelRef="$" valueRef="oof"/>
  1184. </field>
  1185. </subform>
  1186. </subform>
  1187. </template>
  1188. <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
  1189. <foo>
  1190. <bar oof="a">1</bar>
  1191. <bar oof="b">2</bar>
  1192. <bar oof="c">3</bar>
  1193. <bar oof="d">4</bar>
  1194. <bar oof="e">5</bar>
  1195. </foo>
  1196. <xfa:data>
  1197. <A><B></B></A>
  1198. </xfa:data>
  1199. </xfa:datasets>
  1200. </xdp:xdp>
  1201. `;
  1202. const root = new _parser.XFAParser().parse(xml);
  1203. const form = new _bind.Binder(root).bind();
  1204. 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"]);
  1205. 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"]);
  1206. });
  1207. });
  1208. });