xfa_object.js 23 KB

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