xfa_object.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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.XmlObject = exports.XFAObjectArray = exports.XFAObject = exports.XFAAttribute = exports.StringObject = exports.OptionObject = exports.Option10 = exports.Option01 = exports.IntegerObject = exports.ContentObject = exports.$uid = exports.$toStyle = exports.$toString = exports.$toHTML = exports.$text = exports.$tabIndex = exports.$setValue = exports.$setSetAttributes = exports.$setId = exports.$searchNode = exports.$root = exports.$resolvePrototypes = exports.$removeChild = exports.$pushPara = exports.$pushGlyphs = exports.$popPara = exports.$onText = exports.$onChildCheck = exports.$onChild = exports.$nsAttributes = exports.$nodeName = exports.$namespaceId = exports.$isUsable = exports.$isTransparent = exports.$isThereMoreWidth = exports.$isSplittable = exports.$isNsAgnostic = exports.$isDescendent = exports.$isDataValue = exports.$isCDATAXml = exports.$isBindable = exports.$insertAt = exports.$indexOf = exports.$ids = exports.$hasSettableValue = exports.$globalData = exports.$getTemplateRoot = exports.$getSubformParent = exports.$getRealChildrenByNameIt = exports.$getParent = exports.$getNextPage = exports.$getExtra = exports.$getDataValue = exports.$getContainedChildren = exports.$getChildrenByNameIt = exports.$getChildrenByName = exports.$getChildrenByClass = exports.$getChildren = exports.$getAvailableSpace = exports.$getAttributes = exports.$getAttributeIt = exports.$flushHTML = exports.$finalize = exports.$extra = exports.$dump = exports.$data = exports.$content = exports.$consumed = exports.$clone = exports.$cleanup = exports.$cleanPage = exports.$clean = exports.$childrenToHTML = exports.$appendChild = exports.$addHTML = exports.$acceptWhitespace = void 0;
  27. var _utils = require("./utils.js");
  28. var _util = require("../../shared/util.js");
  29. var _core_utils = require("../core_utils.js");
  30. var _namespaces = require("./namespaces.js");
  31. var _som = require("./som.js");
  32. const $acceptWhitespace = Symbol();
  33. exports.$acceptWhitespace = $acceptWhitespace;
  34. const $addHTML = Symbol();
  35. exports.$addHTML = $addHTML;
  36. const $appendChild = Symbol();
  37. exports.$appendChild = $appendChild;
  38. const $childrenToHTML = Symbol();
  39. exports.$childrenToHTML = $childrenToHTML;
  40. const $clean = Symbol();
  41. exports.$clean = $clean;
  42. const $cleanPage = Symbol();
  43. exports.$cleanPage = $cleanPage;
  44. const $cleanup = Symbol();
  45. exports.$cleanup = $cleanup;
  46. const $clone = Symbol();
  47. exports.$clone = $clone;
  48. const $consumed = Symbol();
  49. exports.$consumed = $consumed;
  50. const $content = Symbol("content");
  51. exports.$content = $content;
  52. const $data = Symbol("data");
  53. exports.$data = $data;
  54. const $dump = Symbol();
  55. exports.$dump = $dump;
  56. const $extra = Symbol("extra");
  57. exports.$extra = $extra;
  58. const $finalize = Symbol();
  59. exports.$finalize = $finalize;
  60. const $flushHTML = Symbol();
  61. exports.$flushHTML = $flushHTML;
  62. const $getAttributeIt = Symbol();
  63. exports.$getAttributeIt = $getAttributeIt;
  64. const $getAttributes = Symbol();
  65. exports.$getAttributes = $getAttributes;
  66. const $getAvailableSpace = Symbol();
  67. exports.$getAvailableSpace = $getAvailableSpace;
  68. const $getChildrenByClass = Symbol();
  69. exports.$getChildrenByClass = $getChildrenByClass;
  70. const $getChildrenByName = Symbol();
  71. exports.$getChildrenByName = $getChildrenByName;
  72. const $getChildrenByNameIt = Symbol();
  73. exports.$getChildrenByNameIt = $getChildrenByNameIt;
  74. const $getDataValue = Symbol();
  75. exports.$getDataValue = $getDataValue;
  76. const $getExtra = Symbol();
  77. exports.$getExtra = $getExtra;
  78. const $getRealChildrenByNameIt = Symbol();
  79. exports.$getRealChildrenByNameIt = $getRealChildrenByNameIt;
  80. const $getChildren = Symbol();
  81. exports.$getChildren = $getChildren;
  82. const $getContainedChildren = Symbol();
  83. exports.$getContainedChildren = $getContainedChildren;
  84. const $getNextPage = Symbol();
  85. exports.$getNextPage = $getNextPage;
  86. const $getSubformParent = Symbol();
  87. exports.$getSubformParent = $getSubformParent;
  88. const $getParent = Symbol();
  89. exports.$getParent = $getParent;
  90. const $getTemplateRoot = Symbol();
  91. exports.$getTemplateRoot = $getTemplateRoot;
  92. const $globalData = Symbol();
  93. exports.$globalData = $globalData;
  94. const $hasSettableValue = Symbol();
  95. exports.$hasSettableValue = $hasSettableValue;
  96. const $ids = Symbol();
  97. exports.$ids = $ids;
  98. const $indexOf = Symbol();
  99. exports.$indexOf = $indexOf;
  100. const $insertAt = Symbol();
  101. exports.$insertAt = $insertAt;
  102. const $isCDATAXml = Symbol();
  103. exports.$isCDATAXml = $isCDATAXml;
  104. const $isBindable = Symbol();
  105. exports.$isBindable = $isBindable;
  106. const $isDataValue = Symbol();
  107. exports.$isDataValue = $isDataValue;
  108. const $isDescendent = Symbol();
  109. exports.$isDescendent = $isDescendent;
  110. const $isNsAgnostic = Symbol();
  111. exports.$isNsAgnostic = $isNsAgnostic;
  112. const $isSplittable = Symbol();
  113. exports.$isSplittable = $isSplittable;
  114. const $isThereMoreWidth = Symbol();
  115. exports.$isThereMoreWidth = $isThereMoreWidth;
  116. const $isTransparent = Symbol();
  117. exports.$isTransparent = $isTransparent;
  118. const $isUsable = Symbol();
  119. exports.$isUsable = $isUsable;
  120. const $lastAttribute = Symbol();
  121. const $namespaceId = Symbol("namespaceId");
  122. exports.$namespaceId = $namespaceId;
  123. const $nodeName = Symbol("nodeName");
  124. exports.$nodeName = $nodeName;
  125. const $nsAttributes = Symbol();
  126. exports.$nsAttributes = $nsAttributes;
  127. const $onChild = Symbol();
  128. exports.$onChild = $onChild;
  129. const $onChildCheck = Symbol();
  130. exports.$onChildCheck = $onChildCheck;
  131. const $onText = Symbol();
  132. exports.$onText = $onText;
  133. const $pushGlyphs = Symbol();
  134. exports.$pushGlyphs = $pushGlyphs;
  135. const $popPara = Symbol();
  136. exports.$popPara = $popPara;
  137. const $pushPara = Symbol();
  138. exports.$pushPara = $pushPara;
  139. const $removeChild = Symbol();
  140. exports.$removeChild = $removeChild;
  141. const $root = Symbol("root");
  142. exports.$root = $root;
  143. const $resolvePrototypes = Symbol();
  144. exports.$resolvePrototypes = $resolvePrototypes;
  145. const $searchNode = Symbol();
  146. exports.$searchNode = $searchNode;
  147. const $setId = Symbol();
  148. exports.$setId = $setId;
  149. const $setSetAttributes = Symbol();
  150. exports.$setSetAttributes = $setSetAttributes;
  151. const $setValue = Symbol();
  152. exports.$setValue = $setValue;
  153. const $tabIndex = Symbol();
  154. exports.$tabIndex = $tabIndex;
  155. const $text = Symbol();
  156. exports.$text = $text;
  157. const $toHTML = Symbol();
  158. exports.$toHTML = $toHTML;
  159. const $toString = Symbol();
  160. exports.$toString = $toString;
  161. const $toStyle = Symbol();
  162. exports.$toStyle = $toStyle;
  163. const $uid = Symbol("uid");
  164. exports.$uid = $uid;
  165. const _applyPrototype = Symbol();
  166. const _attributes = Symbol();
  167. const _attributeNames = Symbol();
  168. const _children = Symbol("_children");
  169. const _cloneAttribute = Symbol();
  170. const _dataValue = Symbol();
  171. const _defaultValue = Symbol();
  172. const _filteredChildrenGenerator = Symbol();
  173. const _getPrototype = Symbol();
  174. const _getUnsetAttributes = Symbol();
  175. const _hasChildren = Symbol();
  176. const _max = Symbol();
  177. const _options = Symbol();
  178. const _parent = Symbol("parent");
  179. const _resolvePrototypesHelper = Symbol();
  180. const _setAttributes = Symbol();
  181. const _validator = Symbol();
  182. let uid = 0;
  183. const NS_DATASETS = _namespaces.NamespaceIds.datasets.id;
  184. class XFAObject {
  185. constructor(nsId, name, hasChildren = false) {
  186. this[$namespaceId] = nsId;
  187. this[$nodeName] = name;
  188. this[_hasChildren] = hasChildren;
  189. this[_parent] = null;
  190. this[_children] = [];
  191. this[$uid] = `${name}${uid++}`;
  192. this[$globalData] = null;
  193. }
  194. [$onChild](child) {
  195. if (!this[_hasChildren] || !this[$onChildCheck](child)) {
  196. return false;
  197. }
  198. const name = child[$nodeName];
  199. const node = this[name];
  200. if (node instanceof XFAObjectArray) {
  201. if (node.push(child)) {
  202. this[$appendChild](child);
  203. return true;
  204. }
  205. } else {
  206. if (node !== null) {
  207. this[$removeChild](node);
  208. }
  209. this[name] = child;
  210. this[$appendChild](child);
  211. return true;
  212. }
  213. let id = "";
  214. if (this.id) {
  215. id = ` (id: ${this.id})`;
  216. } else if (this.name) {
  217. id = ` (name: ${this.name} ${this.h.value})`;
  218. }
  219. (0, _util.warn)(`XFA - node "${this[$nodeName]}"${id} has already enough "${name}"!`);
  220. return false;
  221. }
  222. [$onChildCheck](child) {
  223. return this.hasOwnProperty(child[$nodeName]) && child[$namespaceId] === this[$namespaceId];
  224. }
  225. [$isNsAgnostic]() {
  226. return false;
  227. }
  228. [$acceptWhitespace]() {
  229. return false;
  230. }
  231. [$isCDATAXml]() {
  232. return false;
  233. }
  234. [$isBindable]() {
  235. return false;
  236. }
  237. [$popPara]() {
  238. if (this.para) {
  239. this[$getTemplateRoot]()[$extra].paraStack.pop();
  240. }
  241. }
  242. [$pushPara]() {
  243. this[$getTemplateRoot]()[$extra].paraStack.push(this.para);
  244. }
  245. [$setId](ids) {
  246. if (this.id && this[$namespaceId] === _namespaces.NamespaceIds.template.id) {
  247. ids.set(this.id, this);
  248. }
  249. }
  250. [$getTemplateRoot]() {
  251. return this[$globalData].template;
  252. }
  253. [$isSplittable]() {
  254. return false;
  255. }
  256. [$isThereMoreWidth]() {
  257. return false;
  258. }
  259. [$appendChild](child) {
  260. child[_parent] = this;
  261. this[_children].push(child);
  262. if (!child[$globalData] && this[$globalData]) {
  263. child[$globalData] = this[$globalData];
  264. }
  265. }
  266. [$removeChild](child) {
  267. const i = this[_children].indexOf(child);
  268. this[_children].splice(i, 1);
  269. }
  270. [$hasSettableValue]() {
  271. return this.hasOwnProperty("value");
  272. }
  273. [$setValue](_) {}
  274. [$onText](_) {}
  275. [$finalize]() {}
  276. [$clean](builder) {
  277. delete this[_hasChildren];
  278. if (this[$cleanup]) {
  279. builder.clean(this[$cleanup]);
  280. delete this[$cleanup];
  281. }
  282. }
  283. [$indexOf](child) {
  284. return this[_children].indexOf(child);
  285. }
  286. [$insertAt](i, child) {
  287. child[_parent] = this;
  288. this[_children].splice(i, 0, child);
  289. if (!child[$globalData] && this[$globalData]) {
  290. child[$globalData] = this[$globalData];
  291. }
  292. }
  293. [$isTransparent]() {
  294. return !this.name;
  295. }
  296. [$lastAttribute]() {
  297. return "";
  298. }
  299. [$text]() {
  300. if (this[_children].length === 0) {
  301. return this[$content];
  302. }
  303. return this[_children].map(c => c[$text]()).join("");
  304. }
  305. get [_attributeNames]() {
  306. const proto = Object.getPrototypeOf(this);
  307. if (!proto._attributes) {
  308. const attributes = proto._attributes = new Set();
  309. for (const name of Object.getOwnPropertyNames(this)) {
  310. if (this[name] === null || this[name] instanceof XFAObject || this[name] instanceof XFAObjectArray) {
  311. break;
  312. }
  313. attributes.add(name);
  314. }
  315. }
  316. return (0, _util.shadow)(this, _attributeNames, proto._attributes);
  317. }
  318. [$isDescendent](parent) {
  319. let node = this;
  320. while (node) {
  321. if (node === parent) {
  322. return true;
  323. }
  324. node = node[$getParent]();
  325. }
  326. return false;
  327. }
  328. [$getParent]() {
  329. return this[_parent];
  330. }
  331. [$getSubformParent]() {
  332. return this[$getParent]();
  333. }
  334. [$getChildren](name = null) {
  335. if (!name) {
  336. return this[_children];
  337. }
  338. return this[name];
  339. }
  340. [$dump]() {
  341. const dumped = Object.create(null);
  342. if (this[$content]) {
  343. dumped.$content = this[$content];
  344. }
  345. for (const name of Object.getOwnPropertyNames(this)) {
  346. const value = this[name];
  347. if (value === null) {
  348. continue;
  349. }
  350. if (value instanceof XFAObject) {
  351. dumped[name] = value[$dump]();
  352. } else if (value instanceof XFAObjectArray) {
  353. if (!value.isEmpty()) {
  354. dumped[name] = value.dump();
  355. }
  356. } else {
  357. dumped[name] = value;
  358. }
  359. }
  360. return dumped;
  361. }
  362. [$toStyle]() {
  363. return null;
  364. }
  365. [$toHTML]() {
  366. return _utils.HTMLResult.EMPTY;
  367. }
  368. *[$getContainedChildren]() {
  369. for (const node of this[$getChildren]()) {
  370. yield node;
  371. }
  372. }
  373. *[_filteredChildrenGenerator](filter, include) {
  374. for (const node of this[$getContainedChildren]()) {
  375. if (!filter || include === filter.has(node[$nodeName])) {
  376. const availableSpace = this[$getAvailableSpace]();
  377. const res = node[$toHTML](availableSpace);
  378. if (!res.success) {
  379. this[$extra].failingNode = node;
  380. }
  381. yield res;
  382. }
  383. }
  384. }
  385. [$flushHTML]() {
  386. return null;
  387. }
  388. [$addHTML](html, bbox) {
  389. this[$extra].children.push(html);
  390. }
  391. [$getAvailableSpace]() {}
  392. [$childrenToHTML]({
  393. filter = null,
  394. include = true
  395. }) {
  396. if (!this[$extra].generator) {
  397. this[$extra].generator = this[_filteredChildrenGenerator](filter, include);
  398. } else {
  399. const availableSpace = this[$getAvailableSpace]();
  400. const res = this[$extra].failingNode[$toHTML](availableSpace);
  401. if (!res.success) {
  402. return res;
  403. }
  404. if (res.html) {
  405. this[$addHTML](res.html, res.bbox);
  406. }
  407. delete this[$extra].failingNode;
  408. }
  409. while (true) {
  410. const gen = this[$extra].generator.next();
  411. if (gen.done) {
  412. break;
  413. }
  414. const res = gen.value;
  415. if (!res.success) {
  416. return res;
  417. }
  418. if (res.html) {
  419. this[$addHTML](res.html, res.bbox);
  420. }
  421. }
  422. this[$extra].generator = null;
  423. return _utils.HTMLResult.EMPTY;
  424. }
  425. [$setSetAttributes](attributes) {
  426. this[_setAttributes] = new Set(Object.keys(attributes));
  427. }
  428. [_getUnsetAttributes](protoAttributes) {
  429. const allAttr = this[_attributeNames];
  430. const setAttr = this[_setAttributes];
  431. return [...protoAttributes].filter(x => allAttr.has(x) && !setAttr.has(x));
  432. }
  433. [$resolvePrototypes](ids, ancestors = new Set()) {
  434. for (const child of this[_children]) {
  435. child[_resolvePrototypesHelper](ids, ancestors);
  436. }
  437. }
  438. [_resolvePrototypesHelper](ids, ancestors) {
  439. const proto = this[_getPrototype](ids, ancestors);
  440. if (proto) {
  441. this[_applyPrototype](proto, ids, ancestors);
  442. } else {
  443. this[$resolvePrototypes](ids, ancestors);
  444. }
  445. }
  446. [_getPrototype](ids, ancestors) {
  447. const {
  448. use,
  449. usehref
  450. } = this;
  451. if (!use && !usehref) {
  452. return null;
  453. }
  454. let proto = null;
  455. let somExpression = null;
  456. let id = null;
  457. let ref = use;
  458. if (usehref) {
  459. ref = usehref;
  460. if (usehref.startsWith("#som(") && usehref.endsWith(")")) {
  461. somExpression = usehref.slice("#som(".length, usehref.length - 1);
  462. } else if (usehref.startsWith(".#som(") && usehref.endsWith(")")) {
  463. somExpression = usehref.slice(".#som(".length, usehref.length - 1);
  464. } else if (usehref.startsWith("#")) {
  465. id = usehref.slice(1);
  466. } else if (usehref.startsWith(".#")) {
  467. id = usehref.slice(2);
  468. }
  469. } else if (use.startsWith("#")) {
  470. id = use.slice(1);
  471. } else {
  472. somExpression = use;
  473. }
  474. this.use = this.usehref = "";
  475. if (id) {
  476. proto = ids.get(id);
  477. } else {
  478. proto = (0, _som.searchNode)(ids.get($root), this, somExpression, true, false);
  479. if (proto) {
  480. proto = proto[0];
  481. }
  482. }
  483. if (!proto) {
  484. (0, _util.warn)(`XFA - Invalid prototype reference: ${ref}.`);
  485. return null;
  486. }
  487. if (proto[$nodeName] !== this[$nodeName]) {
  488. (0, _util.warn)(`XFA - Incompatible prototype: ${proto[$nodeName]} !== ${this[$nodeName]}.`);
  489. return null;
  490. }
  491. if (ancestors.has(proto)) {
  492. (0, _util.warn)(`XFA - Cycle detected in prototypes use.`);
  493. return null;
  494. }
  495. ancestors.add(proto);
  496. const protoProto = proto[_getPrototype](ids, ancestors);
  497. if (protoProto) {
  498. proto[_applyPrototype](protoProto, ids, ancestors);
  499. }
  500. proto[$resolvePrototypes](ids, ancestors);
  501. ancestors.delete(proto);
  502. return proto;
  503. }
  504. [_applyPrototype](proto, ids, ancestors) {
  505. if (ancestors.has(proto)) {
  506. (0, _util.warn)(`XFA - Cycle detected in prototypes use.`);
  507. return;
  508. }
  509. if (!this[$content] && proto[$content]) {
  510. this[$content] = proto[$content];
  511. }
  512. const newAncestors = new Set(ancestors);
  513. newAncestors.add(proto);
  514. for (const unsetAttrName of this[_getUnsetAttributes](proto[_setAttributes])) {
  515. this[unsetAttrName] = proto[unsetAttrName];
  516. if (this[_setAttributes]) {
  517. this[_setAttributes].add(unsetAttrName);
  518. }
  519. }
  520. for (const name of Object.getOwnPropertyNames(this)) {
  521. if (this[_attributeNames].has(name)) {
  522. continue;
  523. }
  524. const value = this[name];
  525. const protoValue = proto[name];
  526. if (value instanceof XFAObjectArray) {
  527. for (const child of value[_children]) {
  528. child[_resolvePrototypesHelper](ids, ancestors);
  529. }
  530. for (let i = value[_children].length, ii = protoValue[_children].length; i < ii; i++) {
  531. const child = proto[_children][i][$clone]();
  532. if (value.push(child)) {
  533. child[_parent] = this;
  534. this[_children].push(child);
  535. child[_resolvePrototypesHelper](ids, ancestors);
  536. } else {
  537. break;
  538. }
  539. }
  540. continue;
  541. }
  542. if (value !== null) {
  543. value[$resolvePrototypes](ids, ancestors);
  544. if (protoValue) {
  545. value[_applyPrototype](protoValue, ids, ancestors);
  546. }
  547. continue;
  548. }
  549. if (protoValue !== null) {
  550. const child = protoValue[$clone]();
  551. child[_parent] = this;
  552. this[name] = child;
  553. this[_children].push(child);
  554. child[_resolvePrototypesHelper](ids, ancestors);
  555. }
  556. }
  557. }
  558. static [_cloneAttribute](obj) {
  559. if (Array.isArray(obj)) {
  560. return obj.map(x => XFAObject[_cloneAttribute](x));
  561. }
  562. if (typeof obj === "object" && obj !== null) {
  563. return Object.assign({}, obj);
  564. }
  565. return obj;
  566. }
  567. [$clone]() {
  568. const clone = Object.create(Object.getPrototypeOf(this));
  569. for (const $symbol of Object.getOwnPropertySymbols(this)) {
  570. try {
  571. clone[$symbol] = this[$symbol];
  572. } catch (_) {
  573. (0, _util.shadow)(clone, $symbol, this[$symbol]);
  574. }
  575. }
  576. clone[$uid] = `${clone[$nodeName]}${uid++}`;
  577. clone[_children] = [];
  578. for (const name of Object.getOwnPropertyNames(this)) {
  579. if (this[_attributeNames].has(name)) {
  580. clone[name] = XFAObject[_cloneAttribute](this[name]);
  581. continue;
  582. }
  583. const value = this[name];
  584. if (value instanceof XFAObjectArray) {
  585. clone[name] = new XFAObjectArray(value[_max]);
  586. } else {
  587. clone[name] = null;
  588. }
  589. }
  590. for (const child of this[_children]) {
  591. const name = child[$nodeName];
  592. const clonedChild = child[$clone]();
  593. clone[_children].push(clonedChild);
  594. clonedChild[_parent] = clone;
  595. if (clone[name] === null) {
  596. clone[name] = clonedChild;
  597. } else {
  598. clone[name][_children].push(clonedChild);
  599. }
  600. }
  601. return clone;
  602. }
  603. [$getChildren](name = null) {
  604. if (!name) {
  605. return this[_children];
  606. }
  607. return this[_children].filter(c => c[$nodeName] === name);
  608. }
  609. [$getChildrenByClass](name) {
  610. return this[name];
  611. }
  612. [$getChildrenByName](name, allTransparent, first = true) {
  613. return Array.from(this[$getChildrenByNameIt](name, allTransparent, first));
  614. }
  615. *[$getChildrenByNameIt](name, allTransparent, first = true) {
  616. if (name === "parent") {
  617. yield this[_parent];
  618. return;
  619. }
  620. for (const child of this[_children]) {
  621. if (child[$nodeName] === name) {
  622. yield child;
  623. }
  624. if (child.name === name) {
  625. yield child;
  626. }
  627. if (allTransparent || child[$isTransparent]()) {
  628. yield* child[$getChildrenByNameIt](name, allTransparent, false);
  629. }
  630. }
  631. if (first && this[_attributeNames].has(name)) {
  632. yield new XFAAttribute(this, name, this[name]);
  633. }
  634. }
  635. }
  636. exports.XFAObject = XFAObject;
  637. class XFAObjectArray {
  638. constructor(max = Infinity) {
  639. this[_max] = max;
  640. this[_children] = [];
  641. }
  642. push(child) {
  643. const len = this[_children].length;
  644. if (len <= this[_max]) {
  645. this[_children].push(child);
  646. return true;
  647. }
  648. (0, _util.warn)(`XFA - node "${child[$nodeName]}" accepts no more than ${this[_max]} children`);
  649. return false;
  650. }
  651. isEmpty() {
  652. return this[_children].length === 0;
  653. }
  654. dump() {
  655. return this[_children].length === 1 ? this[_children][0][$dump]() : this[_children].map(x => x[$dump]());
  656. }
  657. [$clone]() {
  658. const clone = new XFAObjectArray(this[_max]);
  659. clone[_children] = this[_children].map(c => c[$clone]());
  660. return clone;
  661. }
  662. get children() {
  663. return this[_children];
  664. }
  665. clear() {
  666. this[_children].length = 0;
  667. }
  668. }
  669. exports.XFAObjectArray = XFAObjectArray;
  670. class XFAAttribute {
  671. constructor(node, name, value) {
  672. this[_parent] = node;
  673. this[$nodeName] = name;
  674. this[$content] = value;
  675. this[$consumed] = false;
  676. this[$uid] = `attribute${uid++}`;
  677. }
  678. [$getParent]() {
  679. return this[_parent];
  680. }
  681. [$isDataValue]() {
  682. return true;
  683. }
  684. [$getDataValue]() {
  685. return this[$content].trim();
  686. }
  687. [$setValue](value) {
  688. value = value.value || "";
  689. this[$content] = value.toString();
  690. }
  691. [$text]() {
  692. return this[$content];
  693. }
  694. [$isDescendent](parent) {
  695. return this[_parent] === parent || this[_parent][$isDescendent](parent);
  696. }
  697. }
  698. exports.XFAAttribute = XFAAttribute;
  699. class XmlObject extends XFAObject {
  700. constructor(nsId, name, attributes = {}) {
  701. super(nsId, name);
  702. this[$content] = "";
  703. this[_dataValue] = null;
  704. if (name !== "#text") {
  705. const map = new Map();
  706. this[_attributes] = map;
  707. for (const [attrName, value] of Object.entries(attributes)) {
  708. map.set(attrName, new XFAAttribute(this, attrName, value));
  709. }
  710. if (attributes.hasOwnProperty($nsAttributes)) {
  711. const dataNode = attributes[$nsAttributes].xfa.dataNode;
  712. if (dataNode !== undefined) {
  713. if (dataNode === "dataGroup") {
  714. this[_dataValue] = false;
  715. } else if (dataNode === "dataValue") {
  716. this[_dataValue] = true;
  717. }
  718. }
  719. }
  720. }
  721. this[$consumed] = false;
  722. }
  723. [$toString](buf) {
  724. const tagName = this[$nodeName];
  725. if (tagName === "#text") {
  726. buf.push((0, _core_utils.encodeToXmlString)(this[$content]));
  727. return;
  728. }
  729. const prefix = this[$namespaceId] === NS_DATASETS ? "xfa:" : "";
  730. buf.push(`<${prefix}${tagName}`);
  731. for (const [name, value] of this[_attributes].entries()) {
  732. buf.push(` ${name}="${(0, _core_utils.encodeToXmlString)(value[$content])}"`);
  733. }
  734. if (this[_dataValue] !== null) {
  735. if (this[_dataValue]) {
  736. buf.push(` xfa:dataNode="dataValue"`);
  737. } else {
  738. buf.push(` xfa:dataNode="dataGroup"`);
  739. }
  740. }
  741. if (!this[$content] && this[_children].length === 0) {
  742. buf.push("/>");
  743. return;
  744. }
  745. buf.push(">");
  746. if (this[$content]) {
  747. if (typeof this[$content] === "string") {
  748. buf.push((0, _core_utils.encodeToXmlString)(this[$content]));
  749. } else {
  750. this[$content][$toString](buf);
  751. }
  752. } else {
  753. for (const child of this[_children]) {
  754. child[$toString](buf);
  755. }
  756. }
  757. buf.push(`</${prefix}${tagName}>`);
  758. }
  759. [$onChild](child) {
  760. if (this[$content]) {
  761. const node = new XmlObject(this[$namespaceId], "#text");
  762. this[$appendChild](node);
  763. node[$content] = this[$content];
  764. this[$content] = "";
  765. }
  766. this[$appendChild](child);
  767. return true;
  768. }
  769. [$onText](str) {
  770. this[$content] += str;
  771. }
  772. [$finalize]() {
  773. if (this[$content] && this[_children].length > 0) {
  774. const node = new XmlObject(this[$namespaceId], "#text");
  775. this[$appendChild](node);
  776. node[$content] = this[$content];
  777. delete this[$content];
  778. }
  779. }
  780. [$toHTML]() {
  781. if (this[$nodeName] === "#text") {
  782. return _utils.HTMLResult.success({
  783. name: "#text",
  784. value: this[$content]
  785. });
  786. }
  787. return _utils.HTMLResult.EMPTY;
  788. }
  789. [$getChildren](name = null) {
  790. if (!name) {
  791. return this[_children];
  792. }
  793. return this[_children].filter(c => c[$nodeName] === name);
  794. }
  795. [$getAttributes]() {
  796. return this[_attributes];
  797. }
  798. [$getChildrenByClass](name) {
  799. const value = this[_attributes].get(name);
  800. if (value !== undefined) {
  801. return value;
  802. }
  803. return this[$getChildren](name);
  804. }
  805. *[$getChildrenByNameIt](name, allTransparent) {
  806. const value = this[_attributes].get(name);
  807. if (value) {
  808. yield value;
  809. }
  810. for (const child of this[_children]) {
  811. if (child[$nodeName] === name) {
  812. yield child;
  813. }
  814. if (allTransparent) {
  815. yield* child[$getChildrenByNameIt](name, allTransparent);
  816. }
  817. }
  818. }
  819. *[$getAttributeIt](name, skipConsumed) {
  820. const value = this[_attributes].get(name);
  821. if (value && (!skipConsumed || !value[$consumed])) {
  822. yield value;
  823. }
  824. for (const child of this[_children]) {
  825. yield* child[$getAttributeIt](name, skipConsumed);
  826. }
  827. }
  828. *[$getRealChildrenByNameIt](name, allTransparent, skipConsumed) {
  829. for (const child of this[_children]) {
  830. if (child[$nodeName] === name && (!skipConsumed || !child[$consumed])) {
  831. yield child;
  832. }
  833. if (allTransparent) {
  834. yield* child[$getRealChildrenByNameIt](name, allTransparent, skipConsumed);
  835. }
  836. }
  837. }
  838. [$isDataValue]() {
  839. if (this[_dataValue] === null) {
  840. return this[_children].length === 0 || this[_children][0][$namespaceId] === _namespaces.NamespaceIds.xhtml.id;
  841. }
  842. return this[_dataValue];
  843. }
  844. [$getDataValue]() {
  845. if (this[_dataValue] === null) {
  846. if (this[_children].length === 0) {
  847. return this[$content].trim();
  848. }
  849. if (this[_children][0][$namespaceId] === _namespaces.NamespaceIds.xhtml.id) {
  850. return this[_children][0][$text]().trim();
  851. }
  852. return null;
  853. }
  854. return this[$content].trim();
  855. }
  856. [$setValue](value) {
  857. value = value.value || "";
  858. this[$content] = value.toString();
  859. }
  860. [$dump](hasNS = false) {
  861. const dumped = Object.create(null);
  862. if (hasNS) {
  863. dumped.$ns = this[$namespaceId];
  864. }
  865. if (this[$content]) {
  866. dumped.$content = this[$content];
  867. }
  868. dumped.$name = this[$nodeName];
  869. dumped.children = [];
  870. for (const child of this[_children]) {
  871. dumped.children.push(child[$dump](hasNS));
  872. }
  873. dumped.attributes = Object.create(null);
  874. for (const [name, value] of this[_attributes]) {
  875. dumped.attributes[name] = value[$content];
  876. }
  877. return dumped;
  878. }
  879. }
  880. exports.XmlObject = XmlObject;
  881. class ContentObject extends XFAObject {
  882. constructor(nsId, name) {
  883. super(nsId, name);
  884. this[$content] = "";
  885. }
  886. [$onText](text) {
  887. this[$content] += text;
  888. }
  889. [$finalize]() {}
  890. }
  891. exports.ContentObject = ContentObject;
  892. class OptionObject extends ContentObject {
  893. constructor(nsId, name, options) {
  894. super(nsId, name);
  895. this[_options] = options;
  896. }
  897. [$finalize]() {
  898. this[$content] = (0, _utils.getKeyword)({
  899. data: this[$content],
  900. defaultValue: this[_options][0],
  901. validate: k => this[_options].includes(k)
  902. });
  903. }
  904. [$clean](builder) {
  905. super[$clean](builder);
  906. delete this[_options];
  907. }
  908. }
  909. exports.OptionObject = OptionObject;
  910. class StringObject extends ContentObject {
  911. [$finalize]() {
  912. this[$content] = this[$content].trim();
  913. }
  914. }
  915. exports.StringObject = StringObject;
  916. class IntegerObject extends ContentObject {
  917. constructor(nsId, name, defaultValue, validator) {
  918. super(nsId, name);
  919. this[_defaultValue] = defaultValue;
  920. this[_validator] = validator;
  921. }
  922. [$finalize]() {
  923. this[$content] = (0, _utils.getInteger)({
  924. data: this[$content],
  925. defaultValue: this[_defaultValue],
  926. validate: this[_validator]
  927. });
  928. }
  929. [$clean](builder) {
  930. super[$clean](builder);
  931. delete this[_defaultValue];
  932. delete this[_validator];
  933. }
  934. }
  935. exports.IntegerObject = IntegerObject;
  936. class Option01 extends IntegerObject {
  937. constructor(nsId, name) {
  938. super(nsId, name, 0, n => n === 1);
  939. }
  940. }
  941. exports.Option01 = Option01;
  942. class Option10 extends IntegerObject {
  943. constructor(nsId, name) {
  944. super(nsId, name, 1, n => n === 0);
  945. }
  946. }
  947. exports.Option10 = Option10;