som.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.createDataNode = createDataNode;
  27. exports.searchNode = searchNode;
  28. var _xfa_object = require("./xfa_object.js");
  29. var _namespaces = require("./namespaces.js");
  30. var _util = require("../../shared/util.js");
  31. const namePattern = /^[^.[]+/;
  32. const indexPattern = /^[^\]]+/;
  33. const operators = {
  34. dot: 0,
  35. dotDot: 1,
  36. dotHash: 2,
  37. dotBracket: 3,
  38. dotParen: 4
  39. };
  40. const shortcuts = new Map([["$data", (root, current) => root.datasets ? root.datasets.data : root], ["$record", (root, current) => (root.datasets ? root.datasets.data : root)[_xfa_object.$getChildren]()[0]], ["$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]]);
  41. const somCache = new WeakMap();
  42. const NS_DATASETS = _namespaces.NamespaceIds.datasets.id;
  43. function parseIndex(index) {
  44. index = index.trim();
  45. if (index === "*") {
  46. return Infinity;
  47. }
  48. return parseInt(index, 10) || 0;
  49. }
  50. function parseExpression(expr, dotDotAllowed, noExpr = true) {
  51. let match = expr.match(namePattern);
  52. if (!match) {
  53. return null;
  54. }
  55. let [name] = match;
  56. const parsed = [{
  57. name,
  58. cacheName: "." + name,
  59. index: 0,
  60. js: null,
  61. formCalc: null,
  62. operator: operators.dot
  63. }];
  64. let pos = name.length;
  65. while (pos < expr.length) {
  66. const spos = pos;
  67. const char = expr.charAt(pos++);
  68. if (char === "[") {
  69. match = expr.slice(pos).match(indexPattern);
  70. if (!match) {
  71. (0, _util.warn)("XFA - Invalid index in SOM expression");
  72. return null;
  73. }
  74. parsed[parsed.length - 1].index = parseIndex(match[0]);
  75. pos += match[0].length + 1;
  76. continue;
  77. }
  78. let operator;
  79. switch (expr.charAt(pos)) {
  80. case ".":
  81. if (!dotDotAllowed) {
  82. return null;
  83. }
  84. pos++;
  85. operator = operators.dotDot;
  86. break;
  87. case "#":
  88. pos++;
  89. operator = operators.dotHash;
  90. break;
  91. case "[":
  92. if (noExpr) {
  93. (0, _util.warn)("XFA - SOM expression contains a FormCalc subexpression which is not supported for now.");
  94. return null;
  95. }
  96. operator = operators.dotBracket;
  97. break;
  98. case "(":
  99. if (noExpr) {
  100. (0, _util.warn)("XFA - SOM expression contains a JavaScript subexpression which is not supported for now.");
  101. return null;
  102. }
  103. operator = operators.dotParen;
  104. break;
  105. default:
  106. operator = operators.dot;
  107. break;
  108. }
  109. match = expr.slice(pos).match(namePattern);
  110. if (!match) {
  111. break;
  112. }
  113. [name] = match;
  114. pos += name.length;
  115. parsed.push({
  116. name,
  117. cacheName: expr.slice(spos, pos),
  118. operator,
  119. index: 0,
  120. js: null,
  121. formCalc: null
  122. });
  123. }
  124. return parsed;
  125. }
  126. function searchNode(root, container, expr, dotDotAllowed = true, useCache = true) {
  127. const parsed = parseExpression(expr, dotDotAllowed);
  128. if (!parsed) {
  129. return null;
  130. }
  131. const fn = shortcuts.get(parsed[0].name);
  132. let i = 0;
  133. let isQualified;
  134. if (fn) {
  135. isQualified = true;
  136. root = [fn(root, container)];
  137. i = 1;
  138. } else {
  139. isQualified = container === null;
  140. root = [container || root];
  141. }
  142. for (let ii = parsed.length; i < ii; i++) {
  143. const {
  144. name,
  145. cacheName,
  146. operator,
  147. index
  148. } = parsed[i];
  149. const nodes = [];
  150. for (const node of root) {
  151. if (!(node instanceof _xfa_object.XFAObject)) {
  152. continue;
  153. }
  154. let children, cached;
  155. if (useCache) {
  156. cached = somCache.get(node);
  157. if (!cached) {
  158. cached = new Map();
  159. somCache.set(node, cached);
  160. }
  161. children = cached.get(cacheName);
  162. }
  163. if (!children) {
  164. switch (operator) {
  165. case operators.dot:
  166. children = node[_xfa_object.$getChildrenByName](name, false);
  167. break;
  168. case operators.dotDot:
  169. children = node[_xfa_object.$getChildrenByName](name, true);
  170. break;
  171. case operators.dotHash:
  172. children = node[_xfa_object.$getChildrenByClass](name);
  173. if (children instanceof _xfa_object.XFAObjectArray) {
  174. children = children.children;
  175. } else {
  176. children = [children];
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. if (useCache) {
  183. cached.set(cacheName, children);
  184. }
  185. }
  186. if (children.length > 0) {
  187. nodes.push(children);
  188. }
  189. }
  190. if (nodes.length === 0 && !isQualified && i === 0) {
  191. const parent = container[_xfa_object.$getParent]();
  192. container = parent;
  193. if (!container) {
  194. return null;
  195. }
  196. i = -1;
  197. root = [container];
  198. continue;
  199. }
  200. if (isFinite(index)) {
  201. root = nodes.filter(node => index < node.length).map(node => node[index]);
  202. } else {
  203. root = nodes.reduce((acc, node) => acc.concat(node), []);
  204. }
  205. }
  206. if (root.length === 0) {
  207. return null;
  208. }
  209. return root;
  210. }
  211. function createNodes(root, path) {
  212. let node = null;
  213. for (const {
  214. name,
  215. index
  216. } of path) {
  217. for (let i = 0, ii = !isFinite(index) ? 0 : index; i <= ii; i++) {
  218. const nsId = root[_xfa_object.$namespaceId] === NS_DATASETS ? -1 : root[_xfa_object.$namespaceId];
  219. node = new _xfa_object.XmlObject(nsId, name);
  220. root[_xfa_object.$appendChild](node);
  221. }
  222. root = node;
  223. }
  224. return node;
  225. }
  226. function createDataNode(root, container, expr) {
  227. const parsed = parseExpression(expr);
  228. if (!parsed) {
  229. return null;
  230. }
  231. if (parsed.some(x => x.operator === operators.dotDot)) {
  232. return null;
  233. }
  234. const fn = shortcuts.get(parsed[0].name);
  235. let i = 0;
  236. if (fn) {
  237. root = fn(root, container);
  238. i = 1;
  239. } else {
  240. root = container || root;
  241. }
  242. for (let ii = parsed.length; i < ii; i++) {
  243. const {
  244. name,
  245. operator,
  246. index
  247. } = parsed[i];
  248. if (!isFinite(index)) {
  249. parsed[i].index = 0;
  250. return createNodes(root, parsed.slice(i));
  251. }
  252. let children;
  253. switch (operator) {
  254. case operators.dot:
  255. children = root[_xfa_object.$getChildrenByName](name, false);
  256. break;
  257. case operators.dotDot:
  258. children = root[_xfa_object.$getChildrenByName](name, true);
  259. break;
  260. case operators.dotHash:
  261. children = root[_xfa_object.$getChildrenByClass](name);
  262. if (children instanceof _xfa_object.XFAObjectArray) {
  263. children = children.children;
  264. } else {
  265. children = [children];
  266. }
  267. break;
  268. default:
  269. break;
  270. }
  271. if (children.length === 0) {
  272. return createNodes(root, parsed.slice(i));
  273. }
  274. if (index < children.length) {
  275. const child = children[index];
  276. if (!(child instanceof _xfa_object.XFAObject)) {
  277. (0, _util.warn)(`XFA - Cannot create a node.`);
  278. return null;
  279. }
  280. root = child;
  281. } else {
  282. parsed[i].index = index - children.length;
  283. return createNodes(root, parsed.slice(i));
  284. }
  285. }
  286. return null;
  287. }