xfa_object.js 28 KB

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