2
0

som.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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) {
  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. operator = operators.dotBracket;
  91. break;
  92. case "(":
  93. operator = operators.dotParen;
  94. break;
  95. default:
  96. operator = operators.dot;
  97. break;
  98. }
  99. match = expr.slice(pos).match(namePattern);
  100. if (!match) {
  101. break;
  102. }
  103. [name] = match;
  104. pos += name.length;
  105. parsed.push({
  106. name,
  107. cacheName: expr.slice(spos, pos),
  108. operator,
  109. index: 0,
  110. js: null,
  111. formCalc: null
  112. });
  113. }
  114. return parsed;
  115. }
  116. function searchNode(root, container, expr, dotDotAllowed = true, useCache = true) {
  117. const parsed = parseExpression(expr, dotDotAllowed);
  118. if (!parsed) {
  119. return null;
  120. }
  121. const fn = shortcuts.get(parsed[0].name);
  122. let i = 0;
  123. let isQualified;
  124. if (fn) {
  125. isQualified = true;
  126. root = [fn(root, container)];
  127. i = 1;
  128. } else {
  129. isQualified = container === null;
  130. root = [container || root];
  131. }
  132. for (let ii = parsed.length; i < ii; i++) {
  133. const {
  134. name,
  135. cacheName,
  136. operator,
  137. index
  138. } = parsed[i];
  139. const nodes = [];
  140. for (const node of root) {
  141. if (!(node instanceof _xfa_object.XFAObject)) {
  142. continue;
  143. }
  144. let children, cached;
  145. if (useCache) {
  146. cached = somCache.get(node);
  147. if (!cached) {
  148. cached = new Map();
  149. somCache.set(node, cached);
  150. }
  151. children = cached.get(cacheName);
  152. }
  153. if (!children) {
  154. switch (operator) {
  155. case operators.dot:
  156. children = node[_xfa_object.$getChildrenByName](name, false);
  157. break;
  158. case operators.dotDot:
  159. children = node[_xfa_object.$getChildrenByName](name, true);
  160. break;
  161. case operators.dotHash:
  162. children = node[_xfa_object.$getChildrenByClass](name);
  163. if (children instanceof _xfa_object.XFAObjectArray) {
  164. children = children.children;
  165. } else {
  166. children = [children];
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. if (useCache) {
  173. cached.set(cacheName, children);
  174. }
  175. }
  176. if (children.length > 0) {
  177. nodes.push(children);
  178. }
  179. }
  180. if (nodes.length === 0 && !isQualified && i === 0) {
  181. const parent = container[_xfa_object.$getParent]();
  182. container = parent;
  183. if (!container) {
  184. return null;
  185. }
  186. i = -1;
  187. root = [container];
  188. continue;
  189. }
  190. if (isFinite(index)) {
  191. root = nodes.filter(node => index < node.length).map(node => node[index]);
  192. } else {
  193. root = nodes.reduce((acc, node) => acc.concat(node), []);
  194. }
  195. }
  196. if (root.length === 0) {
  197. return null;
  198. }
  199. return root;
  200. }
  201. function createNodes(root, path) {
  202. let node = null;
  203. for (const {
  204. name,
  205. index
  206. } of path) {
  207. for (let i = 0; i <= index; i++) {
  208. node = new _xfa_object.XmlObject(root[_xfa_object.$namespaceId], name);
  209. root[_xfa_object.$appendChild](node);
  210. }
  211. root = node;
  212. }
  213. return node;
  214. }
  215. function createDataNode(root, container, expr) {
  216. const parsed = parseExpression(expr);
  217. if (!parsed) {
  218. return null;
  219. }
  220. if (parsed.some(x => x.operator === operators.dotDot)) {
  221. return null;
  222. }
  223. const fn = shortcuts.get(parsed[0].name);
  224. let i = 0;
  225. if (fn) {
  226. root = fn(root, container);
  227. i = 1;
  228. } else {
  229. root = container || root;
  230. }
  231. for (let ii = parsed.length; i < ii; i++) {
  232. const {
  233. name,
  234. operator,
  235. index
  236. } = parsed[i];
  237. if (!isFinite(index)) {
  238. parsed[i].index = 0;
  239. return createNodes(root, parsed.slice(i));
  240. }
  241. let children;
  242. switch (operator) {
  243. case operators.dot:
  244. children = root[_xfa_object.$getChildrenByName](name, false);
  245. break;
  246. case operators.dotDot:
  247. children = root[_xfa_object.$getChildrenByName](name, true);
  248. break;
  249. case operators.dotHash:
  250. children = root[_xfa_object.$getChildrenByClass](name);
  251. if (children instanceof _xfa_object.XFAObjectArray) {
  252. children = children.children;
  253. } else {
  254. children = [children];
  255. }
  256. break;
  257. default:
  258. break;
  259. }
  260. if (children.length === 0) {
  261. return createNodes(root, parsed.slice(i));
  262. }
  263. if (index < children.length) {
  264. const child = children[index];
  265. if (!(child instanceof _xfa_object.XFAObject)) {
  266. (0, _util.warn)(`XFA - Cannot create a node.`);
  267. return null;
  268. }
  269. root = child;
  270. } else {
  271. parsed[i].index = children.length - index;
  272. return createNodes(root, parsed.slice(i));
  273. }
  274. }
  275. return null;
  276. }