xfa_object.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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.$toHTML = exports.$text = exports.$setValue = exports.$setSetAttributes = exports.$setId = exports.$resolvePrototypes = exports.$removeChild = exports.$onText = exports.$onChildCheck = exports.$onChild = exports.$nsAttributes = exports.$nodeName = exports.$namespaceId = exports.$isTransparent = exports.$isDescendent = exports.$isDataValue = exports.$insertAt = exports.$indexOf = exports.$hasSettableValue = exports.$hasItem = exports.$global = exports.$getRealChildrenByNameIt = exports.$getParent = exports.$getChildrenByNameIt = exports.$getChildrenByName = exports.$getChildrenByClass = exports.$getChildren = exports.$getAttributeIt = exports.$finalize = exports.$extra = exports.$dump = exports.$data = exports.$content = exports.$consumed = exports.$clone = exports.$cleanup = exports.$clean = exports.$childrenToHTML = exports.$appendChild = void 0;
  27. var _utils = require("./utils.js");
  28. var _util = require("../../shared/util.js");
  29. var _namespaces = require("./namespaces.js");
  30. const $appendChild = Symbol();
  31. exports.$appendChild = $appendChild;
  32. const $childrenToHTML = Symbol();
  33. exports.$childrenToHTML = $childrenToHTML;
  34. const $clean = Symbol();
  35. exports.$clean = $clean;
  36. const $cleanup = Symbol();
  37. exports.$cleanup = $cleanup;
  38. const $clone = Symbol();
  39. exports.$clone = $clone;
  40. const $consumed = Symbol();
  41. exports.$consumed = $consumed;
  42. const $content = Symbol("content");
  43. exports.$content = $content;
  44. const $data = Symbol("data");
  45. exports.$data = $data;
  46. const $dump = Symbol();
  47. exports.$dump = $dump;
  48. const $extra = Symbol("extra");
  49. exports.$extra = $extra;
  50. const $finalize = Symbol();
  51. exports.$finalize = $finalize;
  52. const $getAttributeIt = Symbol();
  53. exports.$getAttributeIt = $getAttributeIt;
  54. const $getChildrenByClass = Symbol();
  55. exports.$getChildrenByClass = $getChildrenByClass;
  56. const $getChildrenByName = Symbol();
  57. exports.$getChildrenByName = $getChildrenByName;
  58. const $getChildrenByNameIt = Symbol();
  59. exports.$getChildrenByNameIt = $getChildrenByNameIt;
  60. const $getRealChildrenByNameIt = Symbol();
  61. exports.$getRealChildrenByNameIt = $getRealChildrenByNameIt;
  62. const $getChildren = Symbol();
  63. exports.$getChildren = $getChildren;
  64. const $getParent = Symbol();
  65. exports.$getParent = $getParent;
  66. const $global = Symbol();
  67. exports.$global = $global;
  68. const $hasItem = Symbol();
  69. exports.$hasItem = $hasItem;
  70. const $hasSettableValue = Symbol();
  71. exports.$hasSettableValue = $hasSettableValue;
  72. const $indexOf = Symbol();
  73. exports.$indexOf = $indexOf;
  74. const $insertAt = Symbol();
  75. exports.$insertAt = $insertAt;
  76. const $isDataValue = Symbol();
  77. exports.$isDataValue = $isDataValue;
  78. const $isDescendent = Symbol();
  79. exports.$isDescendent = $isDescendent;
  80. const $isTransparent = Symbol();
  81. exports.$isTransparent = $isTransparent;
  82. const $lastAttribute = Symbol();
  83. const $namespaceId = Symbol("namespaceId");
  84. exports.$namespaceId = $namespaceId;
  85. const $nodeName = Symbol("nodeName");
  86. exports.$nodeName = $nodeName;
  87. const $nsAttributes = Symbol();
  88. exports.$nsAttributes = $nsAttributes;
  89. const $onChild = Symbol();
  90. exports.$onChild = $onChild;
  91. const $onChildCheck = Symbol();
  92. exports.$onChildCheck = $onChildCheck;
  93. const $onText = Symbol();
  94. exports.$onText = $onText;
  95. const $removeChild = Symbol();
  96. exports.$removeChild = $removeChild;
  97. const $resolvePrototypes = Symbol();
  98. exports.$resolvePrototypes = $resolvePrototypes;
  99. const $setId = Symbol();
  100. exports.$setId = $setId;
  101. const $setSetAttributes = Symbol();
  102. exports.$setSetAttributes = $setSetAttributes;
  103. const $setValue = Symbol();
  104. exports.$setValue = $setValue;
  105. const $text = Symbol();
  106. exports.$text = $text;
  107. const $toHTML = Symbol();
  108. exports.$toHTML = $toHTML;
  109. const $toStyle = Symbol();
  110. exports.$toStyle = $toStyle;
  111. const $uid = Symbol("uid");
  112. exports.$uid = $uid;
  113. const _applyPrototype = Symbol();
  114. const _attributes = Symbol();
  115. const _attributeNames = Symbol();
  116. const _children = Symbol("_children");
  117. const _cloneAttribute = Symbol();
  118. const _dataValue = Symbol();
  119. const _defaultValue = Symbol();
  120. const _getPrototype = Symbol();
  121. const _getUnsetAttributes = Symbol();
  122. const _hasChildren = Symbol();
  123. const _max = Symbol();
  124. const _options = Symbol();
  125. const _parent = Symbol("parent");
  126. const _setAttributes = Symbol();
  127. const _validator = Symbol();
  128. let uid = 0;
  129. class XFAObject {
  130. constructor(nsId, name, hasChildren = false) {
  131. this[$namespaceId] = nsId;
  132. this[$nodeName] = name;
  133. this[_hasChildren] = hasChildren;
  134. this[_parent] = null;
  135. this[_children] = [];
  136. this[$uid] = `${name}${uid++}`;
  137. }
  138. [$onChild](child) {
  139. if (!this[_hasChildren] || !this[$onChildCheck](child)) {
  140. return false;
  141. }
  142. const name = child[$nodeName];
  143. const node = this[name];
  144. if (node instanceof XFAObjectArray) {
  145. if (node.push(child)) {
  146. this[$appendChild](child);
  147. return true;
  148. }
  149. } else {
  150. if (node !== null) {
  151. this[$removeChild](node);
  152. }
  153. this[name] = child;
  154. this[$appendChild](child);
  155. return true;
  156. }
  157. let id = "";
  158. if (this.id) {
  159. id = ` (id: ${this.id})`;
  160. } else if (this.name) {
  161. id = ` (name: ${this.name} ${this.h.value})`;
  162. }
  163. (0, _util.warn)(`XFA - node "${this[$nodeName]}"${id} has already enough "${name}"!`);
  164. return false;
  165. }
  166. [$onChildCheck](child) {
  167. return this.hasOwnProperty(child[$nodeName]) && child[$namespaceId] === this[$namespaceId];
  168. }
  169. [$setId](ids) {
  170. if (this.id && this[$namespaceId] === _namespaces.NamespaceIds.template.id) {
  171. ids.set(this.id, this);
  172. }
  173. }
  174. [$appendChild](child) {
  175. child[_parent] = this;
  176. this[_children].push(child);
  177. }
  178. [$removeChild](child) {
  179. const i = this[_children].indexOf(child);
  180. this[_children].splice(i, 1);
  181. }
  182. [$hasSettableValue]() {
  183. return this.hasOwnProperty("value");
  184. }
  185. [$setValue](_) {}
  186. [$onText](_) {}
  187. [$finalize]() {}
  188. [$clean](builder) {
  189. delete this[_hasChildren];
  190. if (this[$cleanup]) {
  191. builder.clean(this[$cleanup]);
  192. delete this[$cleanup];
  193. }
  194. }
  195. [$hasItem]() {
  196. return false;
  197. }
  198. [$indexOf](child) {
  199. return this[_children].indexOf(child);
  200. }
  201. [$insertAt](i, child) {
  202. child[_parent] = this;
  203. this[_children].splice(i, 0, child);
  204. }
  205. [$isTransparent]() {
  206. return this.name === "";
  207. }
  208. [$lastAttribute]() {
  209. return "";
  210. }
  211. [$text]() {
  212. if (this[_children].length === 0) {
  213. return this[$content];
  214. }
  215. return this[_children].map(c => c[$text]()).join("");
  216. }
  217. get [_attributeNames]() {
  218. const proto = Object.getPrototypeOf(this);
  219. if (!proto._attributes) {
  220. const attributes = proto._attributes = new Set();
  221. for (const name of Object.getOwnPropertyNames(this)) {
  222. if (this[name] === null || this[name] instanceof XFAObject || this[name] instanceof XFAObjectArray) {
  223. break;
  224. }
  225. attributes.add(name);
  226. }
  227. }
  228. return (0, _util.shadow)(this, _attributeNames, proto._attributes);
  229. }
  230. [$isDescendent](parent) {
  231. let node = this;
  232. while (node) {
  233. if (node === parent) {
  234. return true;
  235. }
  236. node = node[$getParent]();
  237. }
  238. return false;
  239. }
  240. [$getParent]() {
  241. return this[_parent];
  242. }
  243. [$getChildren](name = null) {
  244. if (!name) {
  245. return this[_children];
  246. }
  247. return this[name];
  248. }
  249. [$dump]() {
  250. const dumped = Object.create(null);
  251. if (this[$content]) {
  252. dumped.$content = this[$content];
  253. }
  254. for (const name of Object.getOwnPropertyNames(this)) {
  255. const value = this[name];
  256. if (value === null) {
  257. continue;
  258. }
  259. if (value instanceof XFAObject) {
  260. dumped[name] = value[$dump]();
  261. } else if (value instanceof XFAObjectArray) {
  262. if (!value.isEmpty()) {
  263. dumped[name] = value.dump();
  264. }
  265. } else {
  266. dumped[name] = value;
  267. }
  268. }
  269. return dumped;
  270. }
  271. [$toStyle]() {
  272. return null;
  273. }
  274. [$toHTML]() {
  275. return null;
  276. }
  277. [$childrenToHTML]({
  278. filter = null,
  279. include = true
  280. }) {
  281. const res = [];
  282. this[$getChildren]().forEach(node => {
  283. if (!filter || include === filter.has(node[$nodeName])) {
  284. const html = node[$toHTML]();
  285. if (html) {
  286. res.push(html);
  287. }
  288. }
  289. });
  290. return res;
  291. }
  292. [$setSetAttributes](attributes) {
  293. if (attributes.use || attributes.id) {
  294. this[_setAttributes] = new Set(Object.keys(attributes));
  295. }
  296. }
  297. [_getUnsetAttributes](protoAttributes) {
  298. const allAttr = this[_attributeNames];
  299. const setAttr = this[_setAttributes];
  300. return [...protoAttributes].filter(x => allAttr.has(x) && !setAttr.has(x));
  301. }
  302. [$resolvePrototypes](ids, ancestors = new Set()) {
  303. for (const child of this[_children]) {
  304. const proto = child[_getPrototype](ids, ancestors);
  305. if (proto) {
  306. child[_applyPrototype](proto, ids, ancestors);
  307. } else {
  308. child[$resolvePrototypes](ids, ancestors);
  309. }
  310. }
  311. }
  312. [_getPrototype](ids, ancestors) {
  313. const {
  314. use
  315. } = this;
  316. if (use && use.startsWith("#")) {
  317. const id = use.slice(1);
  318. const proto = ids.get(id);
  319. this.use = "";
  320. if (!proto) {
  321. (0, _util.warn)(`XFA - Invalid prototype id: ${id}.`);
  322. return null;
  323. }
  324. if (proto[$nodeName] !== this[$nodeName]) {
  325. (0, _util.warn)(`XFA - Incompatible prototype: ${proto[$nodeName]} !== ${this[$nodeName]}.`);
  326. return null;
  327. }
  328. if (ancestors.has(proto)) {
  329. (0, _util.warn)(`XFA - Cycle detected in prototypes use.`);
  330. return null;
  331. }
  332. ancestors.add(proto);
  333. const protoProto = proto[_getPrototype](ids, ancestors);
  334. if (!protoProto) {
  335. ancestors.delete(proto);
  336. return proto;
  337. }
  338. proto[_applyPrototype](protoProto, ids, ancestors);
  339. ancestors.delete(proto);
  340. return proto;
  341. }
  342. return null;
  343. }
  344. [_applyPrototype](proto, ids, ancestors) {
  345. if (ancestors.has(proto)) {
  346. (0, _util.warn)(`XFA - Cycle detected in prototypes use.`);
  347. return;
  348. }
  349. if (!this[$content] && proto[$content]) {
  350. this[$content] = proto[$content];
  351. }
  352. const newAncestors = new Set(ancestors);
  353. newAncestors.add(proto);
  354. for (const unsetAttrName of this[_getUnsetAttributes](proto[_setAttributes])) {
  355. this[unsetAttrName] = proto[unsetAttrName];
  356. if (this[_setAttributes]) {
  357. this[_setAttributes].add(unsetAttrName);
  358. }
  359. }
  360. for (const name of Object.getOwnPropertyNames(this)) {
  361. if (this[_attributeNames].has(name)) {
  362. continue;
  363. }
  364. const value = this[name];
  365. const protoValue = proto[name];
  366. if (value instanceof XFAObjectArray) {
  367. for (const child of value[_children]) {
  368. child[$resolvePrototypes](ids, ancestors);
  369. }
  370. for (let i = value[_children].length, ii = protoValue[_children].length; i < ii; i++) {
  371. const child = proto[_children][i][$clone]();
  372. if (value.push(child)) {
  373. child[_parent] = this;
  374. this[_children].push(child);
  375. child[$resolvePrototypes](ids, newAncestors);
  376. } else {
  377. break;
  378. }
  379. }
  380. continue;
  381. }
  382. if (value !== null) {
  383. value[$resolvePrototypes](ids, ancestors);
  384. continue;
  385. }
  386. if (protoValue !== null) {
  387. const child = protoValue[$clone]();
  388. child[_parent] = this;
  389. this[name] = child;
  390. this[_children].push(child);
  391. child[$resolvePrototypes](ids, newAncestors);
  392. }
  393. }
  394. }
  395. static [_cloneAttribute](obj) {
  396. if (Array.isArray(obj)) {
  397. return obj.map(x => XFAObject[_cloneAttribute](x));
  398. }
  399. if (obj instanceof Object) {
  400. return Object.assign({}, obj);
  401. }
  402. return obj;
  403. }
  404. [$clone]() {
  405. const clone = Object.create(Object.getPrototypeOf(this));
  406. for (const $symbol of Object.getOwnPropertySymbols(this)) {
  407. try {
  408. clone[$symbol] = this[$symbol];
  409. } catch (_) {
  410. (0, _util.shadow)(clone, $symbol, this[$symbol]);
  411. }
  412. }
  413. clone[_children] = [];
  414. for (const name of Object.getOwnPropertyNames(this)) {
  415. if (this[_attributeNames].has(name)) {
  416. clone[name] = XFAObject[_cloneAttribute](this[name]);
  417. continue;
  418. }
  419. const value = this[name];
  420. if (value instanceof XFAObjectArray) {
  421. clone[name] = new XFAObjectArray(value[_max]);
  422. } else {
  423. clone[name] = null;
  424. }
  425. }
  426. for (const child of this[_children]) {
  427. const name = child[$nodeName];
  428. const clonedChild = child[$clone]();
  429. clone[_children].push(clonedChild);
  430. clonedChild[_parent] = clone;
  431. if (clone[name] === null) {
  432. clone[name] = clonedChild;
  433. } else {
  434. clone[name][_children].push(clonedChild);
  435. }
  436. }
  437. return clone;
  438. }
  439. [$getChildren](name = null) {
  440. if (!name) {
  441. return this[_children];
  442. }
  443. return this[_children].filter(c => c[$nodeName] === name);
  444. }
  445. [$getChildrenByClass](name) {
  446. return this[name];
  447. }
  448. [$getChildrenByName](name, allTransparent, first = true) {
  449. return Array.from(this[$getChildrenByNameIt](name, allTransparent, first));
  450. }
  451. *[$getChildrenByNameIt](name, allTransparent, first = true) {
  452. if (name === "parent") {
  453. yield this[_parent];
  454. return;
  455. }
  456. for (const child of this[_children]) {
  457. if (child[$nodeName] === name) {
  458. yield child;
  459. }
  460. if (child.name === name) {
  461. yield child;
  462. }
  463. if (allTransparent || child[$isTransparent]()) {
  464. yield* child[$getChildrenByNameIt](name, allTransparent, false);
  465. }
  466. }
  467. if (first && this[_attributeNames].has(name)) {
  468. yield new XFAAttribute(this, name, this[name]);
  469. }
  470. }
  471. }
  472. exports.XFAObject = XFAObject;
  473. class XFAObjectArray {
  474. constructor(max = Infinity) {
  475. this[_max] = max;
  476. this[_children] = [];
  477. }
  478. push(child) {
  479. const len = this[_children].length;
  480. if (len <= this[_max]) {
  481. this[_children].push(child);
  482. return true;
  483. }
  484. (0, _util.warn)(`XFA - node "${child[$nodeName]}" accepts no more than ${this[_max]} children`);
  485. return false;
  486. }
  487. isEmpty() {
  488. return this[_children].length === 0;
  489. }
  490. dump() {
  491. return this[_children].length === 1 ? this[_children][0][$dump]() : this[_children].map(x => x[$dump]());
  492. }
  493. [$clone]() {
  494. const clone = new XFAObjectArray(this[_max]);
  495. clone[_children] = this[_children].map(c => c[$clone]());
  496. return clone;
  497. }
  498. get children() {
  499. return this[_children];
  500. }
  501. clear() {
  502. this[_children].length = 0;
  503. }
  504. }
  505. exports.XFAObjectArray = XFAObjectArray;
  506. class XFAAttribute {
  507. constructor(node, name, value) {
  508. this[_parent] = node;
  509. this[$nodeName] = name;
  510. this[$content] = value;
  511. this[$consumed] = false;
  512. }
  513. [$getParent]() {
  514. return this[_parent];
  515. }
  516. [$isDataValue]() {
  517. return true;
  518. }
  519. [$text]() {
  520. return this[$content];
  521. }
  522. [$isDescendent](parent) {
  523. return this[_parent] === parent || this[_parent][$isDescendent](parent);
  524. }
  525. }
  526. exports.XFAAttribute = XFAAttribute;
  527. class XmlObject extends XFAObject {
  528. constructor(nsId, name, attributes = {}) {
  529. super(nsId, name);
  530. this[$content] = "";
  531. this[_dataValue] = null;
  532. if (name !== "#text") {
  533. const map = new Map();
  534. this[_attributes] = map;
  535. for (const [attrName, value] of Object.entries(attributes)) {
  536. map.set(attrName, new XFAAttribute(this, attrName, value));
  537. }
  538. if (attributes.hasOwnProperty($nsAttributes)) {
  539. const dataNode = attributes[$nsAttributes].xfa.dataNode;
  540. if (dataNode !== undefined) {
  541. if (dataNode === "dataGroup") {
  542. this[_dataValue] = false;
  543. } else if (dataNode === "dataValue") {
  544. this[_dataValue] = true;
  545. }
  546. }
  547. }
  548. }
  549. this[$consumed] = false;
  550. }
  551. [$onChild](child) {
  552. if (this[$content]) {
  553. const node = new XmlObject(this[$namespaceId], "#text");
  554. this[$appendChild](node);
  555. node[$content] = this[$content];
  556. this[$content] = "";
  557. }
  558. this[$appendChild](child);
  559. return true;
  560. }
  561. [$onText](str) {
  562. this[$content] += str;
  563. }
  564. [$finalize]() {
  565. if (this[$content] && this[_children].length > 0) {
  566. const node = new XmlObject(this[$namespaceId], "#text");
  567. this[$appendChild](node);
  568. node[$content] = this[$content];
  569. delete this[$content];
  570. }
  571. }
  572. [$toHTML]() {
  573. if (this[$nodeName] === "#text") {
  574. return {
  575. name: "#text",
  576. value: this[$content]
  577. };
  578. }
  579. return null;
  580. }
  581. [$getChildren](name = null) {
  582. if (!name) {
  583. return this[_children];
  584. }
  585. return this[_children].filter(c => c[$nodeName] === name);
  586. }
  587. [$getChildrenByClass](name) {
  588. const value = this[_attributes].get(name);
  589. if (value !== undefined) {
  590. return value;
  591. }
  592. return this[$getChildren](name);
  593. }
  594. *[$getChildrenByNameIt](name, allTransparent) {
  595. const value = this[_attributes].get(name);
  596. if (value) {
  597. yield value;
  598. }
  599. for (const child of this[_children]) {
  600. if (child[$nodeName] === name) {
  601. yield child;
  602. }
  603. if (allTransparent) {
  604. yield* child[$getChildrenByNameIt](name, allTransparent);
  605. }
  606. }
  607. }
  608. *[$getAttributeIt](name, skipConsumed) {
  609. const value = this[_attributes].get(name);
  610. if (value && (!skipConsumed || !value[$consumed])) {
  611. yield value;
  612. }
  613. for (const child of this[_children]) {
  614. yield* child[$getAttributeIt](name, skipConsumed);
  615. }
  616. }
  617. *[$getRealChildrenByNameIt](name, allTransparent, skipConsumed) {
  618. for (const child of this[_children]) {
  619. if (child[$nodeName] === name && (!skipConsumed || !child[$consumed])) {
  620. yield child;
  621. }
  622. if (allTransparent) {
  623. yield* child[$getRealChildrenByNameIt](name, allTransparent, skipConsumed);
  624. }
  625. }
  626. }
  627. [$isDataValue]() {
  628. if (this[_dataValue] === null) {
  629. return this[_children].length === 0;
  630. }
  631. return this[_dataValue];
  632. }
  633. [$dump]() {
  634. const dumped = Object.create(null);
  635. if (this[$content]) {
  636. dumped.$content = this[$content];
  637. }
  638. dumped.$name = this[$nodeName];
  639. dumped.children = [];
  640. for (const child of this[_children]) {
  641. dumped.children.push(child[$dump]());
  642. }
  643. dumped.attributes = Object.create(null);
  644. for (const [name, value] of this[_attributes]) {
  645. dumped.attributes[name] = value[$content];
  646. }
  647. return dumped;
  648. }
  649. }
  650. exports.XmlObject = XmlObject;
  651. class ContentObject extends XFAObject {
  652. constructor(nsId, name) {
  653. super(nsId, name);
  654. this[$content] = "";
  655. }
  656. [$onText](text) {
  657. this[$content] += text;
  658. }
  659. [$finalize]() {}
  660. }
  661. exports.ContentObject = ContentObject;
  662. class OptionObject extends ContentObject {
  663. constructor(nsId, name, options) {
  664. super(nsId, name);
  665. this[_options] = options;
  666. }
  667. [$finalize]() {
  668. this[$content] = (0, _utils.getKeyword)({
  669. data: this[$content],
  670. defaultValue: this[_options][0],
  671. validate: k => this[_options].includes(k)
  672. });
  673. }
  674. [$clean](builder) {
  675. super[$clean](builder);
  676. delete this[_options];
  677. }
  678. }
  679. exports.OptionObject = OptionObject;
  680. class StringObject extends ContentObject {
  681. [$finalize]() {
  682. this[$content] = this[$content].trim();
  683. }
  684. }
  685. exports.StringObject = StringObject;
  686. class IntegerObject extends ContentObject {
  687. constructor(nsId, name, defaultValue, validator) {
  688. super(nsId, name);
  689. this[_defaultValue] = defaultValue;
  690. this[_validator] = validator;
  691. }
  692. [$finalize]() {
  693. this[$content] = (0, _utils.getInteger)({
  694. data: this[$content],
  695. defaultValue: this[_defaultValue],
  696. validate: this[_validator]
  697. });
  698. }
  699. [$clean](builder) {
  700. super[$clean](builder);
  701. delete this[_defaultValue];
  702. delete this[_validator];
  703. }
  704. }
  705. exports.IntegerObject = IntegerObject;
  706. class Option01 extends IntegerObject {
  707. constructor(nsId, name) {
  708. super(nsId, name, 0, n => n === 1);
  709. }
  710. }
  711. exports.Option01 = Option01;
  712. class Option10 extends IntegerObject {
  713. constructor(nsId, name) {
  714. super(nsId, name, 1, n => n === 0);
  715. }
  716. }
  717. exports.Option10 = Option10;