2
0

som.js 7.9 KB

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