123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /**
- * @licstart The following is the entire license notice for the
- * Javascript code in this page
- *
- * 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.
- *
- * @licend The above is the entire license notice for the
- * Javascript code in this page
- */
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.createDataNode = createDataNode;
- exports.searchNode = searchNode;
- var _xfa_object = require("./xfa_object.js");
- var _util = require("../../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, current) => root.datasets.data], ["$template", (root, current) => root.template], ["$connectionSet", (root, current) => root.connectionSet], ["$form", (root, current) => root.form], ["$layout", (root, current) => root.layout], ["$host", (root, current) => root.host], ["$dataWindow", (root, current) => root.dataWindow], ["$event", (root, current) => root.event], ["!", (root, current) => root.datasets], ["$xfa", (root, current) => root], ["xfa", (root, current) => root], ["$", (root, current) => current]]);
- const somCache = new WeakMap();
- function parseIndex(index) {
- index = index.trim();
- if (index === "*") {
- return Infinity;
- }
- return parseInt(index, 10) || 0;
- }
- function parseExpression(expr, dotDotAllowed, noExpr = true) {
- 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) {
- (0, _util.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 "[":
- if (noExpr) {
- (0, _util.warn)("XFA - SOM expression contains a FormCalc subexpression which is not supported for now.");
- return null;
- }
- operator = operators.dotBracket;
- break;
- case "(":
- if (noExpr) {
- (0, _util.warn)("XFA - SOM expression contains a JavaScript subexpression which is not supported for now.");
- return null;
- }
- 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, useCache = 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, container)];
- 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 _xfa_object.XFAObject)) {
- continue;
- }
- let children, cached;
- if (useCache) {
- cached = somCache.get(node);
- if (!cached) {
- cached = new Map();
- somCache.set(node, cached);
- }
- children = cached.get(cacheName);
- }
- if (!children) {
- switch (operator) {
- case operators.dot:
- children = node[_xfa_object.$getChildrenByName](name, false);
- break;
- case operators.dotDot:
- children = node[_xfa_object.$getChildrenByName](name, true);
- break;
- case operators.dotHash:
- children = node[_xfa_object.$getChildrenByClass](name);
- if (children instanceof _xfa_object.XFAObjectArray) {
- children = children.children;
- } else {
- children = [children];
- }
- break;
- default:
- break;
- }
- if (useCache) {
- cached.set(cacheName, children);
- }
- }
- if (children.length > 0) {
- nodes.push(children);
- }
- }
- if (nodes.length === 0 && !isQualified && i === 0) {
- const parent = container[_xfa_object.$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;
- }
- return root;
- }
- function createNodes(root, path) {
- let node = null;
- for (const {
- name,
- index
- } of path) {
- for (let i = 0, ii = !isFinite(index) ? 0 : index; i <= ii; i++) {
- node = new _xfa_object.XmlObject(root[_xfa_object.$namespaceId], name);
- root[_xfa_object.$appendChild](node);
- }
- root = node;
- }
- return node;
- }
- function createDataNode(root, container, expr) {
- const parsed = parseExpression(expr);
- if (!parsed) {
- return null;
- }
- if (parsed.some(x => x.operator === operators.dotDot)) {
- return null;
- }
- const fn = shortcuts.get(parsed[0].name);
- let i = 0;
- if (fn) {
- root = fn(root, container);
- i = 1;
- } else {
- root = container || root;
- }
- for (let ii = parsed.length; i < ii; i++) {
- const {
- name,
- operator,
- index
- } = parsed[i];
- if (!isFinite(index)) {
- parsed[i].index = 0;
- return createNodes(root, parsed.slice(i));
- }
- let children;
- switch (operator) {
- case operators.dot:
- children = root[_xfa_object.$getChildrenByName](name, false);
- break;
- case operators.dotDot:
- children = root[_xfa_object.$getChildrenByName](name, true);
- break;
- case operators.dotHash:
- children = root[_xfa_object.$getChildrenByClass](name);
- if (children instanceof _xfa_object.XFAObjectArray) {
- children = children.children;
- } else {
- children = [children];
- }
- break;
- default:
- break;
- }
- if (children.length === 0) {
- return createNodes(root, parsed.slice(i));
- }
- if (index < children.length) {
- const child = children[index];
- if (!(child instanceof _xfa_object.XFAObject)) {
- (0, _util.warn)(`XFA - Cannot create a node.`);
- return null;
- }
- root = child;
- } else {
- parsed[i].index = index - children.length;
- return createNodes(root, parsed.slice(i));
- }
- }
- return null;
- }
|