parser.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.XFAParser = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. var _xml_parser = require("../xml_parser.js");
  29. var _builder = require("./builder.js");
  30. var _util = require("../../shared/util.js");
  31. class XFAParser extends _xml_parser.XMLParserBase {
  32. constructor(rootNameSpace = null, richText = false) {
  33. super();
  34. this._builder = new _builder.Builder(rootNameSpace);
  35. this._stack = [];
  36. this._globalData = {
  37. usedTypefaces: new Set()
  38. };
  39. this._ids = new Map();
  40. this._current = this._builder.buildRoot(this._ids);
  41. this._errorCode = _xml_parser.XMLParserErrorCode.NoError;
  42. this._whiteRegex = /^\s+$/;
  43. this._nbsps = /\xa0+/g;
  44. this._richText = richText;
  45. }
  46. parse(data) {
  47. this.parseXml(data);
  48. if (this._errorCode !== _xml_parser.XMLParserErrorCode.NoError) {
  49. return undefined;
  50. }
  51. this._current[_xfa_object.$finalize]();
  52. return this._current.element;
  53. }
  54. onText(text) {
  55. text = text.replace(this._nbsps, match => match.slice(1) + " ");
  56. if (this._richText || this._current[_xfa_object.$acceptWhitespace]()) {
  57. this._current[_xfa_object.$onText](text, this._richText);
  58. return;
  59. }
  60. if (this._whiteRegex.test(text)) {
  61. return;
  62. }
  63. this._current[_xfa_object.$onText](text.trim());
  64. }
  65. onCdata(text) {
  66. this._current[_xfa_object.$onText](text);
  67. }
  68. _mkAttributes(attributes, tagName) {
  69. let namespace = null;
  70. let prefixes = null;
  71. const attributeObj = Object.create({});
  72. for (const {
  73. name,
  74. value
  75. } of attributes) {
  76. if (name === "xmlns") {
  77. if (!namespace) {
  78. namespace = value;
  79. } else {
  80. (0, _util.warn)(`XFA - multiple namespace definition in <${tagName}>`);
  81. }
  82. } else if (name.startsWith("xmlns:")) {
  83. const prefix = name.substring("xmlns:".length);
  84. if (!prefixes) {
  85. prefixes = [];
  86. }
  87. prefixes.push({
  88. prefix,
  89. value
  90. });
  91. } else {
  92. const i = name.indexOf(":");
  93. if (i === -1) {
  94. attributeObj[name] = value;
  95. } else {
  96. let nsAttrs = attributeObj[_xfa_object.$nsAttributes];
  97. if (!nsAttrs) {
  98. nsAttrs = attributeObj[_xfa_object.$nsAttributes] = Object.create(null);
  99. }
  100. const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
  101. let attrs = nsAttrs[ns];
  102. if (!attrs) {
  103. attrs = nsAttrs[ns] = Object.create(null);
  104. }
  105. attrs[attrName] = value;
  106. }
  107. }
  108. }
  109. return [namespace, prefixes, attributeObj];
  110. }
  111. _getNameAndPrefix(name, nsAgnostic) {
  112. const i = name.indexOf(":");
  113. if (i === -1) {
  114. return [name, null];
  115. }
  116. return [name.substring(i + 1), nsAgnostic ? "" : name.substring(0, i)];
  117. }
  118. onBeginElement(tagName, attributes, isEmpty) {
  119. const [namespace, prefixes, attributesObj] = this._mkAttributes(attributes, tagName);
  120. const [name, nsPrefix] = this._getNameAndPrefix(tagName, this._builder.isNsAgnostic());
  121. const node = this._builder.build({
  122. nsPrefix,
  123. name,
  124. attributes: attributesObj,
  125. namespace,
  126. prefixes
  127. });
  128. node[_xfa_object.$globalData] = this._globalData;
  129. if (isEmpty) {
  130. node[_xfa_object.$finalize]();
  131. if (this._current[_xfa_object.$onChild](node)) {
  132. node[_xfa_object.$setId](this._ids);
  133. }
  134. node[_xfa_object.$clean](this._builder);
  135. return;
  136. }
  137. this._stack.push(this._current);
  138. this._current = node;
  139. }
  140. onEndElement(name) {
  141. const node = this._current;
  142. if (node[_xfa_object.$isCDATAXml]() && typeof node[_xfa_object.$content] === "string") {
  143. const parser = new XFAParser();
  144. parser._globalData = this._globalData;
  145. const root = parser.parse(node[_xfa_object.$content]);
  146. node[_xfa_object.$content] = null;
  147. node[_xfa_object.$onChild](root);
  148. }
  149. node[_xfa_object.$finalize]();
  150. this._current = this._stack.pop();
  151. if (this._current[_xfa_object.$onChild](node)) {
  152. node[_xfa_object.$setId](this._ids);
  153. }
  154. node[_xfa_object.$clean](this._builder);
  155. }
  156. onError(code) {
  157. this._errorCode = code;
  158. }
  159. }
  160. exports.XFAParser = XFAParser;