XFA -- Add support for SOM expressions (#12983)
- specifications: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.364.2157&rep=rep1&type=pdf#page=87; - add a parser for SOM expressions; - add search functions to resolve those expressions; - search functions will be used to bind data into template.
This commit is contained in:
parent
fafe039849
commit
45329af926
232
src/core/xfa/som.js
Normal file
232
src/core/xfa/som.js
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
/* Copyright 2021 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 {
|
||||||
|
$getChildrenByClass,
|
||||||
|
$getChildrenByName,
|
||||||
|
$getParent,
|
||||||
|
XFAObject,
|
||||||
|
XFAObjectArray,
|
||||||
|
} from "./xfa_object.js";
|
||||||
|
import { warn } from "../../shared/util.js";
|
||||||
|
|
||||||
|
const namePattern = /^[^.[]+/;
|
||||||
|
const indexPattern = /^[^\]]+/;
|
||||||
|
const operators = {
|
||||||
|
dot: 0,
|
||||||
|
dotDot: 1,
|
||||||
|
dotHash: 2,
|
||||||
|
dotBracket: 3,
|
||||||
|
dotParen: 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
const shortcuts = new Map([
|
||||||
|
["$data", root => root.datasets.data],
|
||||||
|
["$template", root => root.template],
|
||||||
|
["$connectionSet", root => root.connectionSet],
|
||||||
|
["$form", root => root.form],
|
||||||
|
["$layout", root => root.layout],
|
||||||
|
["$host", root => root.host],
|
||||||
|
["$dataWindow", root => root.dataWindow],
|
||||||
|
["$event", root => root.event],
|
||||||
|
["!", root => root.datasets],
|
||||||
|
["$xfa", root => root],
|
||||||
|
["xfa", root => root],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const somCache = new WeakMap();
|
||||||
|
|
||||||
|
function parseIndex(index) {
|
||||||
|
index = index.trim();
|
||||||
|
if (index === "*") {
|
||||||
|
return Infinity;
|
||||||
|
}
|
||||||
|
return parseInt(index, 10) || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseExpression(expr, dotDotAllowed) {
|
||||||
|
let match = expr.match(namePattern);
|
||||||
|
if (!match) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let [name] = match;
|
||||||
|
const parsed = [
|
||||||
|
{
|
||||||
|
name,
|
||||||
|
cacheName: "." + name,
|
||||||
|
index: 0,
|
||||||
|
js: null,
|
||||||
|
formCalc: null,
|
||||||
|
operator: operators.dot,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let pos = name.length;
|
||||||
|
|
||||||
|
while (pos < expr.length) {
|
||||||
|
const spos = pos;
|
||||||
|
const char = expr.charAt(pos++);
|
||||||
|
if (char === "[") {
|
||||||
|
match = expr.slice(pos).match(indexPattern);
|
||||||
|
if (!match) {
|
||||||
|
warn("XFA - Invalid index in SOM expression");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
parsed[parsed.length - 1].index = parseIndex(match[0]);
|
||||||
|
pos += match[0].length + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let operator;
|
||||||
|
switch (expr.charAt(pos)) {
|
||||||
|
case ".":
|
||||||
|
if (!dotDotAllowed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
pos++;
|
||||||
|
operator = operators.dotDot;
|
||||||
|
break;
|
||||||
|
case "#":
|
||||||
|
pos++;
|
||||||
|
operator = operators.dotHash;
|
||||||
|
break;
|
||||||
|
case "[":
|
||||||
|
// TODO: FormCalc expression so need to use the parser
|
||||||
|
operator = operators.dotBracket;
|
||||||
|
break;
|
||||||
|
case "(":
|
||||||
|
// TODO:
|
||||||
|
// Javascript expression: should be a boolean operation with a path
|
||||||
|
// so maybe we can have our own parser for that stuff or
|
||||||
|
// maybe use the formcalc one.
|
||||||
|
operator = operators.dotParen;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
operator = operators.dot;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
match = expr.slice(pos).match(namePattern);
|
||||||
|
if (!match) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
[name] = match;
|
||||||
|
pos += name.length;
|
||||||
|
parsed.push({
|
||||||
|
name,
|
||||||
|
cacheName: expr.slice(spos, pos),
|
||||||
|
operator,
|
||||||
|
index: 0,
|
||||||
|
js: null,
|
||||||
|
formCalc: null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
function searchNode(root, container, expr, dotDotAllowed = true) {
|
||||||
|
const parsed = parseExpression(expr, dotDotAllowed);
|
||||||
|
if (!parsed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const fn = shortcuts.get(parsed[0].name);
|
||||||
|
let i = 0;
|
||||||
|
let isQualified;
|
||||||
|
if (fn) {
|
||||||
|
isQualified = true;
|
||||||
|
root = [fn(root)];
|
||||||
|
i = 1;
|
||||||
|
} else {
|
||||||
|
isQualified = container === null;
|
||||||
|
root = [container || root];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let ii = parsed.length; i < ii; i++) {
|
||||||
|
const { name, cacheName, operator, index } = parsed[i];
|
||||||
|
const nodes = [];
|
||||||
|
for (const node of root) {
|
||||||
|
if (!(node instanceof XFAObject)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cached = somCache.get(node);
|
||||||
|
if (!cached) {
|
||||||
|
cached = new Map();
|
||||||
|
somCache.set(node, cached);
|
||||||
|
}
|
||||||
|
|
||||||
|
let children = cached.get(cacheName);
|
||||||
|
if (!children) {
|
||||||
|
switch (operator) {
|
||||||
|
case operators.dot:
|
||||||
|
children = node[$getChildrenByName](name, false);
|
||||||
|
break;
|
||||||
|
case operators.dotDot:
|
||||||
|
children = node[$getChildrenByName](name, true);
|
||||||
|
break;
|
||||||
|
case operators.dotHash:
|
||||||
|
children = node[$getChildrenByClass](name);
|
||||||
|
if (children instanceof XFAObjectArray) {
|
||||||
|
children = children.children;
|
||||||
|
} else {
|
||||||
|
children = [children];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cached.set(cacheName, children);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (children.length > 0) {
|
||||||
|
nodes.push(children);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nodes.length === 0 && !isQualified && i === 0) {
|
||||||
|
// We've an unqualified expression and we didn't find anything
|
||||||
|
// so look at container and siblings of container and so on.
|
||||||
|
// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.364.2157&rep=rep1&type=pdf#page=114
|
||||||
|
const parent = container[$getParent]();
|
||||||
|
container = parent;
|
||||||
|
if (!container) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
i = -1;
|
||||||
|
root = [container];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFinite(index)) {
|
||||||
|
root = nodes.filter(node => index < node.length).map(node => node[index]);
|
||||||
|
} else {
|
||||||
|
root = nodes.reduce((acc, node) => acc.concat(node), []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root.length === 1) {
|
||||||
|
return root[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { searchNode };
|
@ -24,7 +24,13 @@ const $cleanup = Symbol();
|
|||||||
const $content = Symbol("content");
|
const $content = Symbol("content");
|
||||||
const $dump = Symbol();
|
const $dump = Symbol();
|
||||||
const $finalize = Symbol();
|
const $finalize = Symbol();
|
||||||
|
const $isDataValue = Symbol();
|
||||||
|
const $getAttributeIt = Symbol();
|
||||||
|
const $getChildrenByClass = Symbol();
|
||||||
|
const $getChildrenByName = Symbol();
|
||||||
|
const $getChildrenByNameIt = Symbol();
|
||||||
const $getChildren = Symbol();
|
const $getChildren = Symbol();
|
||||||
|
const $getParent = Symbol();
|
||||||
const $isTransparent = Symbol();
|
const $isTransparent = Symbol();
|
||||||
const $lastAttribute = Symbol();
|
const $lastAttribute = Symbol();
|
||||||
const $namespaceId = Symbol("namespaceId");
|
const $namespaceId = Symbol("namespaceId");
|
||||||
@ -139,12 +145,16 @@ class XFAObject {
|
|||||||
return shadow(this, _attributeNames, proto._attributes);
|
return shadow(this, _attributeNames, proto._attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[$getParent]() {
|
||||||
|
return this[_parent];
|
||||||
|
}
|
||||||
|
|
||||||
[$getChildren](name = null) {
|
[$getChildren](name = null) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return this[_children];
|
return this[_children];
|
||||||
}
|
}
|
||||||
|
|
||||||
return this[_children].filter(c => c[$nodeName] === name);
|
return this[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
[$dump]() {
|
[$dump]() {
|
||||||
@ -363,6 +373,47 @@ class XFAObject {
|
|||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[$getChildren](name = null) {
|
||||||
|
if (!name) {
|
||||||
|
return this[_children];
|
||||||
|
}
|
||||||
|
|
||||||
|
return this[_children].filter(c => c[$nodeName] === name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[$getChildrenByClass](name) {
|
||||||
|
return this[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
[$getChildrenByName](name, allTransparent, first = true) {
|
||||||
|
return Array.from(this[$getChildrenByNameIt](name, allTransparent, first));
|
||||||
|
}
|
||||||
|
|
||||||
|
*[$getChildrenByNameIt](name, allTransparent, first = true) {
|
||||||
|
if (name === "parent") {
|
||||||
|
yield this[_parent];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const child of this[_children]) {
|
||||||
|
if (child[$nodeName] === name) {
|
||||||
|
yield child;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (child.name === name) {
|
||||||
|
yield child;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allTransparent || child[$isTransparent]()) {
|
||||||
|
yield* child[$getChildrenByNameIt](name, allTransparent, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first && this[_attributeNames].has(name)) {
|
||||||
|
yield new XFAAttribute(this, name, this[name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class XFAObjectArray {
|
class XFAObjectArray {
|
||||||
@ -398,10 +449,34 @@ class XFAObjectArray {
|
|||||||
clone[_children] = this[_children].map(c => c[_clone]());
|
clone[_children] = this[_children].map(c => c[_clone]());
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get children() {
|
||||||
|
return this[_children];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class XFAAttribute {
|
||||||
|
constructor(node, name, value) {
|
||||||
|
this[_parent] = node;
|
||||||
|
this[$nodeName] = name;
|
||||||
|
this[$content] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$getParent]() {
|
||||||
|
return this[_parent];
|
||||||
|
}
|
||||||
|
|
||||||
|
[$isDataValue]() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
[$text]() {
|
||||||
|
return this[$content];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class XmlObject extends XFAObject {
|
class XmlObject extends XFAObject {
|
||||||
constructor(nsId, name, attributes = Object.create(null)) {
|
constructor(nsId, name, attributes = null) {
|
||||||
super(nsId, name);
|
super(nsId, name);
|
||||||
this[$content] = "";
|
this[$content] = "";
|
||||||
if (name !== "#text") {
|
if (name !== "#text") {
|
||||||
@ -412,6 +487,7 @@ class XmlObject extends XFAObject {
|
|||||||
[$onChild](child) {
|
[$onChild](child) {
|
||||||
if (this[$content]) {
|
if (this[$content]) {
|
||||||
const node = new XmlObject(this[$namespaceId], "#text");
|
const node = new XmlObject(this[$namespaceId], "#text");
|
||||||
|
node[_parent] = this;
|
||||||
node[$content] = this[$content];
|
node[$content] = this[$content];
|
||||||
this[$content] = "";
|
this[$content] = "";
|
||||||
this[_children].push(node);
|
this[_children].push(node);
|
||||||
@ -428,6 +504,7 @@ class XmlObject extends XFAObject {
|
|||||||
[$finalize]() {
|
[$finalize]() {
|
||||||
if (this[$content] && this[_children].length > 0) {
|
if (this[$content] && this[_children].length > 0) {
|
||||||
const node = new XmlObject(this[$namespaceId], "#text");
|
const node = new XmlObject(this[$namespaceId], "#text");
|
||||||
|
node[_parent] = this;
|
||||||
node[$content] = this[$content];
|
node[$content] = this[$content];
|
||||||
this[_children].push(node);
|
this[_children].push(node);
|
||||||
delete this[$content];
|
delete this[$content];
|
||||||
@ -440,6 +517,54 @@ class XmlObject extends XFAObject {
|
|||||||
}
|
}
|
||||||
return this[_children].map(c => c[$text]()).join("");
|
return this[_children].map(c => c[$text]()).join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[$getChildren](name = null) {
|
||||||
|
if (!name) {
|
||||||
|
return this[_children];
|
||||||
|
}
|
||||||
|
|
||||||
|
return this[_children].filter(c => c[$nodeName] === name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[$getChildrenByClass](name) {
|
||||||
|
const value = this[_attributes][name];
|
||||||
|
if (value !== undefined) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return this[$getChildren](name);
|
||||||
|
}
|
||||||
|
|
||||||
|
*[$getChildrenByNameIt](name, allTransparent) {
|
||||||
|
const value = this[_attributes][name];
|
||||||
|
if (value !== undefined) {
|
||||||
|
yield new XFAAttribute(this, name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const child of this[_children]) {
|
||||||
|
if (child[$nodeName] === name) {
|
||||||
|
yield child;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allTransparent) {
|
||||||
|
yield* child[$getChildrenByNameIt](name, allTransparent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*[$getAttributeIt](name) {
|
||||||
|
const value = this[_attributes][name];
|
||||||
|
if (value !== undefined) {
|
||||||
|
yield new XFAAttribute(this, name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const child of this[_children]) {
|
||||||
|
yield* child[$getAttributeIt](name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[$isDataValue]() {
|
||||||
|
return this[_children].length === 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ContentObject extends XFAObject {
|
class ContentObject extends XFAObject {
|
||||||
@ -521,7 +646,13 @@ export {
|
|||||||
$content,
|
$content,
|
||||||
$dump,
|
$dump,
|
||||||
$finalize,
|
$finalize,
|
||||||
|
$getAttributeIt,
|
||||||
$getChildren,
|
$getChildren,
|
||||||
|
$getChildrenByClass,
|
||||||
|
$getChildrenByName,
|
||||||
|
$getChildrenByNameIt,
|
||||||
|
$getParent,
|
||||||
|
$isDataValue,
|
||||||
$isTransparent,
|
$isTransparent,
|
||||||
$namespaceId,
|
$namespaceId,
|
||||||
$nodeName,
|
$nodeName,
|
||||||
@ -538,6 +669,7 @@ export {
|
|||||||
Option10,
|
Option10,
|
||||||
OptionObject,
|
OptionObject,
|
||||||
StringObject,
|
StringObject,
|
||||||
|
XFAAttribute,
|
||||||
XFAObject,
|
XFAObject,
|
||||||
XFAObjectArray,
|
XFAObjectArray,
|
||||||
XmlObject,
|
XmlObject,
|
||||||
|
@ -13,7 +13,14 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { $dump, $getChildren, $text } from "../../src/core/xfa/xfa_object.js";
|
import {
|
||||||
|
$dump,
|
||||||
|
$getChildren,
|
||||||
|
$getChildrenByClass,
|
||||||
|
$getChildrenByName,
|
||||||
|
$text,
|
||||||
|
} from "../../src/core/xfa/xfa_object.js";
|
||||||
|
import { searchNode } from "../../src/core/xfa/som.js";
|
||||||
import { XFAParser } from "../../src/core/xfa/parser.js";
|
import { XFAParser } from "../../src/core/xfa/parser.js";
|
||||||
|
|
||||||
describe("XFAParser", function () {
|
describe("XFAParser", function () {
|
||||||
@ -416,4 +423,240 @@ describe("XFAParser", function () {
|
|||||||
expect(field.value.text.$content).toEqual("Overriding text");
|
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")[$text]()).toBe(
|
||||||
|
"a"
|
||||||
|
);
|
||||||
|
expect(searchNode(root, null, "$template..Description.id")[$text]()).toBe(
|
||||||
|
"a"
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template..Description[0].id")[$text]()
|
||||||
|
).toBe("a");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template..Description[1].id")[$text]()
|
||||||
|
).toBe("e");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template..Description[2].id")[$text]()
|
||||||
|
).toBe("p");
|
||||||
|
expect(searchNode(root, null, "$template.Receipt.id")[$text]()).toBe("l");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template.Receipt.Description[1].id")[$text]()
|
||||||
|
).toBe("e");
|
||||||
|
expect(searchNode(root, null, "$template.Receipt.Description[2]")).toBe(
|
||||||
|
null
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template.Receipt.foo.Description.id")[$text]()
|
||||||
|
).toBe("p");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template.#subform.Sub_Total.id")[$text]()
|
||||||
|
).toBe("i");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template.#subform.Units.id")[$text]()
|
||||||
|
).toBe("b");
|
||||||
|
expect(
|
||||||
|
searchNode(root, null, "$template.#subform.Units.parent.id")[$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")[$text]()).toBe(
|
||||||
|
"Acme"
|
||||||
|
);
|
||||||
|
expect(searchNode(root, null, "$data.Receipt.Detail[0]")[$text]()).toBe(
|
||||||
|
"Acme"
|
||||||
|
);
|
||||||
|
expect(searchNode(root, null, "$data.Receipt.Detail[1]")[$text]()).toBe(
|
||||||
|
"foo"
|
||||||
|
);
|
||||||
|
expect(searchNode(root, null, "$data.Receipt.Detail[2]")[$text]()).toBe(
|
||||||
|
"bar"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user