| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541 | /* Copyright 2020 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import {  $dump,  $getChildren,  $getChildrenByClass,  $getChildrenByName,  $text,} from "../../src/core/xfa/xfa_object.js";import { Binder } from "../../src/core/xfa/bind.js";import { searchNode } from "../../src/core/xfa/som.js";import { XFAParser } from "../../src/core/xfa/parser.js";describe("XFAParser", function () {  describe("Parse XFA", function () {    it("should parse a xfa document and create an object to represent it", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" uuid="1234" invalid="foo">  <config xmlns="http://www.xfa.org/schema/xci/3.1/">    <present>      <pdf name="hello">        <adobeExtensionLevel>          7        </adobeExtensionLevel>      </pdf>      <invalid><a>foobar</a></invalid>    </present>    <acrobat>      <submitUrl>http://a.b.c</submitUrl>      <acrobat7>        <dynamicRender>          forbidden        </dynamicRender>      </acrobat7>      <autoSave>enabled</autoSave>      <submitUrl>                 http://d.e.f      </submitUrl>      <submitUrl>http://g.h.i</submitUrl>      <validate>foobar</validate>    </acrobat>  </config>  <template baseProfile="full" xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <extras>      <float>1.23</float>      <boolean>1</boolean>      <integer>314</integer>      <float>2.71</float>    </extras>    <subform>      <proto>        <area x="hello" y="-3.14in" relevant="-foo +bar" />        <color value="111, 222, 123" />        <color value="111, abc, 123" />        <medium imagingBBox="1,2in,3.4cm,5.67px" />        <medium imagingBBox="1,2in,-3cm,4px" />      </proto>    </subform>  </template></xdp:xdp>      `;      const attributes = {        id: "",        name: "",        use: "",        usehref: "",      };      const mediumAttributes = {        id: "",        long: 0,        orientation: "portrait",        short: 0,        stock: "",        trayIn: "auto",        trayOut: "auto",        use: "",        usehref: "",      };      const colorAttributes = {        cSpace: "SRGB",        id: "",        use: "",        usehref: "",      };      const root = new XFAParser().parse(xml);      const expected = {        uuid: "1234",        timeStamp: "",        template: {          baseProfile: "full",          extras: {            ...attributes,            float: [              { ...attributes, $content: 1.23 },              { ...attributes, $content: 2.71 },            ],            boolean: { ...attributes, $content: 1 },            integer: { ...attributes, $content: 314 },          },          subform: {            access: "open",            allowMacro: 0,            anchorType: "topLeft",            colSpan: 1,            columnWidths: [0],            h: "",            hAlign: "left",            id: "",            layout: "position",            locale: "",            maxH: 0,            maxW: 0,            mergeMode: "consumeData",            minH: 0,            minW: 0,            name: "",            presence: "visible",            relevant: [],            restoreState: "manual",            scope: "name",            use: "",            usehref: "",            w: "",            x: 0,            y: 0,            proto: {              area: {                ...attributes,                colSpan: 1,                x: 0,                y: -226.08,                relevant: [                  { excluded: true, viewname: "foo" },                  { excluded: false, viewname: "bar" },                ],              },              color: [                {                  ...colorAttributes,                  value: { r: 111, g: 222, b: 123 },                },                {                  ...colorAttributes,                  value: { r: 111, g: 0, b: 123 },                },              ],              medium: [                {                  ...mediumAttributes,                  imagingBBox: {                    x: 1,                    y: 144,                    width: 96.3779527559055,                    height: 5.67,                  },                },                {                  ...mediumAttributes,                  imagingBBox: {                    x: -1,                    y: -1,                    width: -1,                    height: -1,                  },                },              ],            },          },        },        config: {          acrobat: {            acrobat7: {              dynamicRender: {                $content: "forbidden",              },            },            autoSave: {              $content: "enabled",            },            validate: {              $content: "preSubmit",            },            submitUrl: [              {                $content: "http://a.b.c",              },              {                $content: "http://d.e.f",              },              {                $content: "http://g.h.i",              },            ],          },          present: {            pdf: {              name: "hello",              adobeExtensionLevel: {                $content: 7,              },            },          },        },      };      expect(root[$dump]()).toEqual(expected);    });    it("should parse a xfa document and check namespaces", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <config xmlns:foo="http:/www.foo.com" xmlns="http://www.xfa.org/schema/xci/3.1/">    <present xmlns="http://www.mozilla.org">      <pdf name="hello">        <adobeExtensionLevel>          7        </adobeExtensionLevel>      </pdf>    </present>    <acrobat>      <foo:submitUrl>http://a.b.c</foo:submitUrl>      <submitUrl>http://c.b.a</submitUrl>    </acrobat>  </config>  <template baseProfile="full" xmlns="http://www.allizom.org">    <extras>      <float>1.23</float>    </extras>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const expected = {        uuid: "",        timeStamp: "",        config: {          acrobat: {            submitUrl: { $content: "http://c.b.a" },          },        },      };      expect(root[$dump]()).toEqual(expected);    });    it("should parse a xfa document and parse CDATA when needed", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <field>        <extras>          <exData contentType="text/html" name="foo">            <![CDATA[<body xmlns="http://www.w3.org/1999/xhtml">              <span>hello</span></body>]]>          </exData>        </extra>      </field>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const exdata = searchNode(root, root, "foo")[0];      const body = exdata[$dump]().$content[$dump]();      const expected = {        $name: "body",        attributes: {},        children: [          { $content: "hello", $name: "span", attributes: {}, children: [] },        ],      };      expect(body).toEqual(expected);    });    it("should parse a xfa document and apply some prototypes", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <proto>        <font id="id1" typeface="Foo" size="123pt" weight="bold" posture="italic">          <fill>            <color value="1,2,3"/>          </fill>        </font>      </proto>      <field>        <font use="#id1"/>      </field>      <field>        <font use="#id1" size="456pt" weight="bold" posture="normal">          <fill>            <color value="4,5,6"/>          </fill>          <extras id="id2"/>        </font>      </field>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      let font = root.template.subform.field[0].font;      expect(font.typeface).toEqual("Foo");      expect(font.overline).toEqual(0);      expect(font.size).toEqual(123);      expect(font.weight).toEqual("bold");      expect(font.posture).toEqual("italic");      expect(font.fill.color.value).toEqual({ r: 1, g: 2, b: 3 });      expect(font.extras).toEqual(undefined);      font = root.template.subform.field[1].font;      expect(font.typeface).toEqual("Foo");      expect(font.overline).toEqual(0);      expect(font.size).toEqual(456);      expect(font.weight).toEqual("bold");      expect(font.posture).toEqual("normal");      expect(font.fill.color.value).toEqual({ r: 4, g: 5, b: 6 });      expect(font.extras.id).toEqual("id2");    });    it("should parse a xfa document and apply some prototypes through usehref", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <proto>        <draw name="foo">          <font typeface="Foo" size="123pt" weight="bold" posture="italic">            <fill>              <color value="1,2,3"/>            </fill>          </font>        </draw>      </proto>      <field>        <font usehref=".#som($template.#subform.foo.#font)"/>      </field>      <field>        <font usehref=".#som($template.#subform.foo.#font)" size="456pt" weight="bold" posture="normal">          <fill>            <color value="4,5,6"/>          </fill>          <extras id="id2"/>        </font>      </field>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      let font = root.template.subform.field[0].font;      expect(font.typeface).toEqual("Foo");      expect(font.overline).toEqual(0);      expect(font.size).toEqual(123);      expect(font.weight).toEqual("bold");      expect(font.posture).toEqual("italic");      expect(font.fill.color.value).toEqual({ r: 1, g: 2, b: 3 });      expect(font.extras).toEqual(undefined);      font = root.template.subform.field[1].font;      expect(font.typeface).toEqual("Foo");      expect(font.overline).toEqual(0);      expect(font.size).toEqual(456);      expect(font.weight).toEqual("bold");      expect(font.posture).toEqual("normal");      expect(font.fill.color.value).toEqual({ r: 4, g: 5, b: 6 });      expect(font.extras.id).toEqual("id2");    });    it("should parse a xfa document with xhtml", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <extras>      <text>        <body xmlns="http://www.w3.org/1999/xhtml">          <p style="foo: bar; text-indent:0.5in; line-height:11px;bar:foo;tab-stop: left 0.5in">            The first line of this paragraph is indented a half-inch.<br/>            Successive lines are not indented.<br/>            This is the last line of the paragraph.<br/>          </p>        </body>      </text>    </extras>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      const p = root.template.extras.text.$content[$getChildren]()[0];      expect(p.style).toEqual(        "text-indent:0.5in;line-height:11px;tab-stop:left 0.5in"      );      expect(p[$text]()).toEqual(        [          " The first line of this paragraph is indented a half-inch.\n",          " Successive lines are not indented.\n",          " This is the last line of the paragraph.\n ",        ].join("")      );    });    it("should parse a xfa document and apply some prototypes with cycle", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <proto>        <subform id="id1">          <subform use="#id1"/>        </subform>      </proto>    </subform>    <subform use="#id1"/>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      const subform = root.template.subform[1];      expect(subform.id).toEqual("id1");      expect(subform.subform.id).toEqual("id1");    });    it("should parse a xfa document and apply some nested prototypes", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <proto>        <color id="RED" value="7, 8, 9"/>        <font id="HELV" typeface="helvetica" size="31pt" weight="normal" posture="italic"> </font>        <font id="HELV-RED" use="#HELV">          <fill>            <color use="#RED"/>          </fill>        </font>      </proto>      <field>        <font use="#HELV-RED"/>      </field>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      const font = root.template.subform.field.font;      expect(font.typeface).toEqual("helvetica");      expect(font.overline).toEqual(0);      expect(font.size).toEqual(31);      expect(font.weight).toEqual("normal");      expect(font.posture).toEqual("italic");      expect(font.fill.color.value).toEqual({ r: 7, g: 8, b: 9 });    });    it("should parse a xfa document and apply a prototype with content", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform>      <proto>        <text id="TEXT">default TEXT</text>      </proto>      <field>        <value>          <text use="#TEXT"></text>        </value>      </field>      <field>        <value>          <text use="#TEXT">Overriding text</text>        </value>      </field>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml)[$dump]();      let field = root.template.subform.field[0];      expect(field.value.text.$content).toEqual("default TEXT");      field = root.template.subform.field[1];      expect(field.value.text.$content).toEqual("Overriding text");    });  });  describe("Search in XFA", function () {    it("should search some nodes in a template object", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">    <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">      <subform name="Receipt" id="l">        <subform id="m">          <field name="Description" id="a">  </field>          <field name="Units" id="b">  </field>          <field name="Unit_Price" id="c">  </field>          <field name="Total_Price" id="d">  </field>        </subform>        <subform id="n">          <field name="Description" id="e">  </field>          <field name="Units" id="f">  </field>          <field name="Unit_Price" id="g">  </field>          <field name="Total_Price" id="h">  </field>        </subform>        <subform name="foo" id="o">          <field name="Description" id="p">  </field>          <field name="Units" id="q">  </field>          <field name="Unit_Price" id="r">  </field>          <field name="Total_Price" id="s">  </field>        </subform>        <field name="Sub_Total" id="i">  </field>        <field name="Tax" id="j">  </field>        <field name="Total_Price" id="k">  </field>      </subform>    </template></xdp:xdp>        `;      const root = new XFAParser().parse(xml);      let found = root[$getChildrenByName]("subform", true);      expect(found.map(x => x.id)).toEqual(["l", "m", "n", "o"]);      found = root[$getChildrenByName]("Total_Price", true);      expect(found.map(x => x.id)).toEqual(["d", "h", "s", "k"]);      found = root.template[$getChildrenByName]("Receipt", false);      const receipt = found[0];      found = receipt[$getChildrenByName]("Total_Price", false);      expect(found.map(x => x.id)).toEqual(["d", "h", "k"]);      expect(receipt[$getChildrenByClass]("name")).toEqual("Receipt");      const subforms = receipt[$getChildrenByClass]("subform");      expect(subforms.children.map(x => x.id)).toEqual(["m", "n", "o"]);    });    it("should search some nodes in a template object using SOM", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">    <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">      <subform name="Receipt" id="l">        <subform id="m">          <field name="Description" id="a">  </field>          <field name="Units" id="b">  </field>          <field name="Unit_Price" id="c">  </field>          <field name="Total_Price" id="d">  </field>        </subform>        <subform id="n">          <field name="Description" id="e">  </field>          <field name="Units" id="f">  </field>          <field name="Unit_Price" id="g">  </field>          <field name="Total_Price" id="h">  </field>        </subform>        <subform name="foo" id="o">          <field name="Description" id="p">  </field>          <field name="Units" id="q">  </field>          <field name="Unit_Price" id="r">  </field>          <field name="Total_Price" id="s">  </field>        </subform>        <field name="Sub_Total" id="i">  </field>        <field name="Tax" id="j">  </field>        <field name="Total_Price" id="k">  </field>      </subform>    </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      expect(        searchNode(root, null, "$template..Description.id")[0][$text]()      ).toBe("a");      expect(        searchNode(root, null, "$template..Description.id")[0][$text]()      ).toBe("a");      expect(        searchNode(root, null, "$template..Description[0].id")[0][$text]()      ).toBe("a");      expect(        searchNode(root, null, "$template..Description[1].id")[0][$text]()      ).toBe("e");      expect(        searchNode(root, null, "$template..Description[2].id")[0][$text]()      ).toBe("p");      expect(searchNode(root, null, "$template.Receipt.id")[0][$text]()).toBe(        "l"      );      expect(        searchNode(root, null, "$template.Receipt.Description[1].id")[0][          $text        ]()      ).toBe("e");      expect(searchNode(root, null, "$template.Receipt.Description[2]")).toBe(        null      );      expect(        searchNode(root, null, "$template.Receipt.foo.Description.id")[0][          $text        ]()      ).toBe("p");      expect(        searchNode(root, null, "$template.#subform.Sub_Total.id")[0][$text]()      ).toBe("i");      expect(        searchNode(root, null, "$template.#subform.Units.id")[0][$text]()      ).toBe("b");      expect(        searchNode(root, null, "$template.#subform.Units.parent.id")[0][$text]()      ).toBe("m");    });    it("should search some nodes in a datasets object", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <Receipt>        <Page>1</Page>        <Detail PartNo="GS001">          <Description>Giant Slingshot</Description>          <Units>1</Units>          <Unit_Price>250.00</Unit_Price>          <Total_Price>250.00</Total_Price>        </Detail>        <Page>2</Page>        <Detail PartNo="RRB-LB">          <Description>Road Runner Bait, large bag</Description>          <Units>5</Units>          <Unit_Price>12.00</Unit_Price>          <Total_Price>60.00</Total_Price>        </Detail>        <Sub_Total>310.00</Sub_Total>        <Tax>24.80</Tax>        <Total_Price>334.80</Total_Price>      </Receipt>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const data = root.datasets.data;      let found = data[$getChildrenByName]("Description", true);      expect(found.map(x => x[$text]())).toEqual([        "Giant Slingshot",        "Road Runner Bait, large bag",      ]);      found = data[$getChildrenByName]("Total_Price", true);      expect(found.map(x => x[$text]())).toEqual(["250.00", "60.00", "334.80"]);    });    it("should search some nodes using SOM from a non-root node", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <Receipt>        <Page>1</Page>        <Detail PartNo="GS001">          <Description>Giant Slingshot</Description>          <Units>1</Units>          <Unit_Price>250.00</Unit_Price>          <Total_Price>250.00</Total_Price>        </Detail>        <Page>2</Page>        <Detail PartNo="RRB-LB">          <Description>Road Runner Bait, large bag</Description>          <Units>5</Units>          <Unit_Price>12.00</Unit_Price>          <Total_Price>60.00</Total_Price>        </Detail>        <Sub_Total>310.00</Sub_Total>        <Tax>24.80</Tax>        <Total_Price>334.80</Total_Price>      </Receipt>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const [receipt] = root.datasets.data[$getChildren]("Receipt");      expect(        searchNode(root, receipt, "Detail[*].Total_Price").map(x => x[$text]())      ).toEqual(["250.00", "60.00"]);      const [units] = searchNode(root, receipt, "Detail[1].Units");      expect(units[$text]()).toBe("5");      let [found] = searchNode(root, units, "Total_Price");      expect(found[$text]()).toBe("60.00");      found = searchNode(root, units, "Total_Pric");      expect(found).toEqual(null);    });    it("should search some nodes in a datasets object using SOM", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <Receipt Detail="Acme">        <Detail>foo</Detail>        <Detail>bar</Detail>     </Receipt>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      expect(searchNode(root, null, "$data.Receipt.Detail")[0][$text]()).toBe(        "Acme"      );      expect(        searchNode(root, null, "$data.Receipt.Detail[0]")[0][$text]()      ).toBe("Acme");      expect(        searchNode(root, null, "$data.Receipt.Detail[1]")[0][$text]()      ).toBe("foo");      expect(        searchNode(root, null, "$data.Receipt.Detail[2]")[0][$text]()      ).toBe("bar");    });  });  describe("Bind data into form", function () {    it("should make a basic binding", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="A">      <subform name="B">        <field name="C">        </field>        <field name="D">        </field>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <A>        <C>xyz</C>      </A>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "A.B.C.value.text")[0][$dump]().$content      ).toBe("xyz");    });    it("should make a basic binding and create a non-existing node", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="A" mergeMode="matchTemplate">      <subform name="B">        <field name="C">        </field>        <field name="D">          <value>            <text>foobar</text>          </value>        </field>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <A>      </A>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const binder = new Binder(root);      const form = binder.bind();      const data = binder.getData();      expect(        searchNode(form, form, "A.B.D.value.text")[0][$dump]().$content      ).toBe("foobar");      const expected = {        $name: "A",        attributes: {},        children: [          {            $name: "B",            attributes: {},            children: [              {                $name: "C",                attributes: {},                children: [],              },              {                $name: "D",                attributes: {},                children: [],              },            ],          },        ],      };      expect(searchNode(data, data, "A")[0][$dump]()).toEqual(expected);    });    it("should make a basic binding and create a non-existing node with namespaceId equal to -1", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="A">      <subform name="B">        <field name="C">        </field>        <field name="D">          <value>            <text>foobar</text>          </value>        </field>      </subform>    </subform>  </template></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const binder = new Binder(root);      const form = binder.bind();      const data = binder.getData();      expect(        searchNode(form, form, "A.B.D.value.text")[0][$dump]().$content      ).toBe("foobar");      // Created nodes mustn't belong to xfa:datasets namespace.      const expected = {        $name: "A",        $ns: -1,        attributes: {},        children: [          {            $name: "B",            $ns: -1,            attributes: {},            children: [              {                $name: "C",                $ns: -1,                attributes: {},                children: [],              },              {                $name: "D",                $ns: -1,                attributes: {},                children: [],              },            ],          },        ],      };      expect(searchNode(data, data, "A")[0][$dump](/* hasNS */ true)).toEqual(        expected      );    });    it("should make another basic binding", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="registration">      <field name="first"> </field>      <field name="last">  </field>      <field name="apt">  </field>      <field name="street">  </field>      <field name="city">  </field>      <field name="country">  </field>      <field name="postalcode"/>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <registration>        <first>Jack</first>        <last>Spratt</last>        <apt/>        <street>99 Candlestick Lane</street>        <city>London</city>        <country>UK</country>        <postalcode>SW1</postalcode>      </registration>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "registration.first..text")[0][$dump]().$content      ).toBe("Jack");      expect(        searchNode(form, form, "registration.last..text")[0][$dump]().$content      ).toBe("Spratt");      expect(        searchNode(form, form, "registration.apt..text")[0][$dump]().$content      ).toBe(undefined);      expect(        searchNode(form, form, "registration.street..text")[0][$dump]().$content      ).toBe("99 Candlestick Lane");      expect(        searchNode(form, form, "registration.city..text")[0][$dump]().$content      ).toBe("London");      expect(        searchNode(form, form, "registration.country..text")[0][$dump]()          .$content      ).toBe("UK");      expect(        searchNode(form, form, "registration.postalcode..text")[0][$dump]()          .$content      ).toBe("SW1");    });    it("should make basic binding with extra subform", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="registration">      <field name="first"> </field>      <field name="last">  </field>      <subform name="address">        <field name="apt">  </field>        <field name="street">  </field>        <field name="city">  </field>        <field name="country">  </field>        <field name="postalcode">  </field>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <registration>        <first>Jack</first>        <last>Spratt</last>        <apt/>        <street>99 Candlestick Lane</street>        <city>London</city>        <country>UK</country>        <postalcode>SW1</postalcode>      </registration>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "registration..first..text")[0][$dump]().$content      ).toBe("Jack");      expect(        searchNode(form, form, "registration..last..text")[0][$dump]().$content      ).toBe("Spratt");      expect(        searchNode(form, form, "registration..apt..text")[0][$dump]().$content      ).toBe(undefined);      expect(        searchNode(form, form, "registration..street..text")[0][$dump]()          .$content      ).toBe("99 Candlestick Lane");      expect(        searchNode(form, form, "registration..city..text")[0][$dump]().$content      ).toBe("London");      expect(        searchNode(form, form, "registration..country..text")[0][$dump]()          .$content      ).toBe("UK");      expect(        searchNode(form, form, "registration..postalcode..text")[0][$dump]()          .$content      ).toBe("SW1");    });    it("should make basic binding with extra subform", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="registration" mergeMode="consumeData">      <subform name="address">        <field name="first"/>        <field name="last"/>        <field name="apt"/>        <field name="street"/>        <field name="city"/>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <registration>        <first>Jack</first>        <last>Spratt</last>        <address>          <apt>7</apt>          <street>99 Candlestick Lane</street>          <city>London</city>        </address>      </registration>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "registration..first..text")[0][$dump]().$content      ).toBe("Jack");      expect(        searchNode(form, form, "registration..last..text")[0][$dump]().$content      ).toBe("Spratt");      expect(        searchNode(form, form, "registration..apt..text")[0][$dump]().$content      ).toBe("7");      expect(        searchNode(form, form, "registration..street..text")[0][$dump]()          .$content      ).toBe("99 Candlestick Lane");      expect(        searchNode(form, form, "registration..city..text")[0][$dump]().$content      ).toBe("London");    });    it("should make basic binding with same names in different parts", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="application" mergeMode="consumeData">      <subform name="sponsor">        <field name="lastname">  </field>        <!-- sponsor's last name -->      </subform>      <field name="lastname">  </field>      <!-- applicant's last name -->    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <application>        <lastname>Abott</lastname>        <sponsor>          <lastname>Costello</lastname>        </sponsor>      </application>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "application.sponsor.lastname..text")[0][$dump]()          .$content      ).toBe("Costello");      expect(        searchNode(form, form, "application.lastname..text")[0][$dump]()          .$content      ).toBe("Abott");    });    it("should make binding and create nodes in data", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="root" mergeMode="matchTemplate">      <subform name="A">        <field name="a"/>        <field name="b"/>        <subform name="B">          <field name="c"/>          <field name="d"/>          <subform name="C">            <field name="e"/>            <field name="f"/>          </subform>        </subform>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <root>        <A>          <b>1</b>        </A>      </root>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const binder = new Binder(root);      const form = binder.bind();      const data = binder.getData();      expect(searchNode(form, form, "root..b..text")[0][$dump]().$content).toBe(        "1"      );      expect(searchNode(data, data, "root.A.a")[0][$dump]().$name).toBe("a");      expect(searchNode(data, data, "root.A.B.c")[0][$dump]().$name).toBe("c");      expect(searchNode(data, data, "root.A.B.d")[0][$dump]().$name).toBe("d");      expect(searchNode(data, data, "root.A.B.C.e")[0][$dump]().$name).toBe(        "e"      );      expect(searchNode(data, data, "root.A.B.C.f")[0][$dump]().$name).toBe(        "f"      );    });    it("should make binding and set properties", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="Id">      <field name="LastName">        <setProperty ref="$data.Main.Style.NameFont" target="font.typeface"/>        <setProperty ref="$data.Main.Style.NameSize" target="font.size"/>        <setProperty ref="$data.Main.Help.LastName" target="assist.toolTip"/>        <font></font>        <assist>          <toolTip>          </toolTip>        </assist>      </field>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <Id>        <LastName>foo</LastName>      </Id>      <Main>        <Style>          <NameFont>myfont</NameFont>          <NameSize>123.4pt</NameSize>        </Style>        <Help>          <LastName>Give the name!</LastName>        </Help>      </Main>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "Id.LastName..text")[0][$dump]().$content      ).toBe("foo");      expect(        searchNode(form, form, "Id.LastName.font.typeface")[0][$text]()      ).toBe("myfont");      expect(        searchNode(form, form, "Id.LastName.font.size")[0][$text]()      ).toEqual(123.4);      expect(        searchNode(form, form, "Id.LastName.assist.toolTip")[0][$dump]()          .$content      ).toBe("Give the name!");    });    it("should make binding and bind items", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="main">      <field name="CardName">        <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>        <ui>          <choiceList/>        </ui>      </field>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <main>        <ccs>          <cc uiname="Visa" token="VISA"/>          <cc uiname="Mastercard" token="MC"/>          <cc uiname="American Express" token="AMEX"/>        </ccs>        <CardName>MC</CardName>      </main>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "subform.CardName.items[*].text[*]").map(x =>          x[$text]()        )      ).toEqual([        "Visa",        "Mastercard",        "American Express",        "VISA",        "MC",        "AMEX",      ]);    });    it("should make binding and bind items with a ref", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="main">      <field name="CardName">        <bind match="dataRef" ref="$data.main.value"/>        <bindItems ref="$data.main.ccs.cc[*]" labelRef="uiname" valueRef="token"/>        <ui>          <choiceList/>        </ui>      </field>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <main>        <value>VISA</value>        <ccs>          <cc uiname="Visa" token="VISA"/>          <cc uiname="Mastercard" token="MC"/>          <cc uiname="American Express" token="AMEX"/>        </ccs>        <CardName>MC</CardName>      </main>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "subform.CardName.value.text").map(x =>          x[$text]()        )      ).toEqual(["VISA"]);      expect(        searchNode(form, form, "subform.CardName.items[*].text[*]").map(x =>          x[$text]()        )      ).toEqual([        "Visa",        "Mastercard",        "American Express",        "VISA",        "MC",        "AMEX",      ]);    });    it("should make binding with occurrences in consumeData mode", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="root" mergeMode="consumeData">      <subform name="section" id="section1">        <occur min="0" max="-1"/>        <bind match="dataRef" ref="$.section[*]"/>        <field name="line-item"/>      </subform>      <subform name="section" id="section2">        <occur min="0" max="-1"/>        <bind match="dataRef" ref="$.section[*]"/>        <field name="line-item"/>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <root>        <section>          <line-item>item1</line-item>        </section>        <section>          <line-item>item2</line-item>        </section>      </root>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "root.section[*].id").map(x => x[$text]())      ).toEqual(["section1", "section1"]);      expect(        searchNode(form, form, "root.section[*].line-item..text").map(x =>          x[$text]()        )      ).toEqual(["item1", "item2"]);    });    it("should make binding with occurrences in matchTemplate mode", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="root" mergeMode="matchTemplate">      <subform name="section" id="section1">        <occur min="0" max="-1"/>        <bind match="dataRef" ref="$.section[*]"/>        <field name="line-item"/>      </subform>      <subform name="section" id="section2">        <occur min="0" max="-1"/>        <bind match="dataRef" ref="$.section[*]"/>        <field name="line-item"/>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <root>        <section>          <line-item>item1</line-item>        </section>        <section>          <line-item>item2</line-item>        </section>      </root>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "root.section[*].id").map(x => x[$text]())      ).toEqual(["section1", "section1", "section2", "section2"]);      expect(        searchNode(form, form, "root.section[*].line-item..text").map(x =>          x[$text]()        )      ).toEqual(["item1", "item2", "item1", "item2"]);    });    it("should make binding and create nodes in data with some bind tag", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="root" mergeMode="matchTemplate">      <subform name="A">        <occur max="-1"/>        <bind ref="$.root.foo[*]" match="dataRef"/>      </subform>      <subform name="B">        <occur max="2"/>        <bind ref="$.root.bar[2]" match="dataRef"/>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <root>      </root>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const binder = new Binder(root);      binder.bind();      const data = binder.getData();      const expected = {        $name: "root",        children: [          {            $name: "root",            children: [              {                $name: "foo",                children: [],                attributes: {},              },              {                $name: "bar",                children: [],                attributes: {},              },              {                $name: "bar",                children: [],                attributes: {},              },              {                $name: "bar",                children: [],                attributes: {},              },            ],            attributes: {},          },        ],        attributes: {},      };      expect(searchNode(data, data, "root")[0][$dump]()).toEqual(expected);    });    it("should make a binding with a bindItems", function () {      const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="A" mergeMode="matchTemplate">      <subform name="B">        <field name="C">          <ui>            <choicelist/>          </ui>          <bindItems ref="xfa.datasets.foo.bar[*]" labelRef="$" valueRef="oof"/>        </field>      </subform>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <foo>      <bar oof="a">1</bar>      <bar oof="b">2</bar>      <bar oof="c">3</bar>      <bar oof="d">4</bar>      <bar oof="e">5</bar>    </foo>    <xfa:data>      <A><B></B></A>    </xfa:data>  </xfa:datasets></xdp:xdp>      `;      const root = new XFAParser().parse(xml);      const form = new Binder(root).bind();      expect(        searchNode(form, form, "A.B.C.items[0].text[*]").map(          x => x[$dump]().$content        )      ).toEqual(["1", "2", "3", "4", "5"]);      expect(        searchNode(form, form, "A.B.C.items[1].text[*]").map(          x => x[$dump]().$content        )      ).toEqual(["a", "b", "c", "d", "e"]);    });  });  it("should make a binding with a element in an area", function () {    const xml = `<?xml version="1.0"?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">  <template xmlns="http://www.xfa.org/schema/xfa-template/3.3">    <subform name="A" mergeMode="matchTemplate">      <area>        <field name="B"/>      </area>    </subform>  </template>  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">    <xfa:data>      <A><B>foobar</B></A>    </xfa:data>  </xfa:datasets></xdp:xdp>    `;    const root = new XFAParser().parse(xml);    const form = new Binder(root).bind();    expect(searchNode(form, form, "A..B..text")[0][$dump]().$content).toBe(      "foobar"    );  });});
 |