parser.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.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() {
  33. super();
  34. this._builder = new _builder.Builder();
  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. }
  45. parse(data) {
  46. this.parseXml(data);
  47. if (this._errorCode !== _xml_parser.XMLParserErrorCode.NoError) {
  48. return undefined;
  49. }
  50. this._current[_xfa_object.$finalize]();
  51. return this._current.element;
  52. }
  53. onText(text) {
  54. text = text.replace(this._nbsps, match => match.slice(1) + " ");
  55. if (this._current[_xfa_object.$acceptWhitespace]()) {
  56. this._current[_xfa_object.$onText](text);
  57. return;
  58. }
  59. if (this._whiteRegex.test(text)) {
  60. return;
  61. }
  62. this._current[_xfa_object.$onText](text.trim());
  63. }
  64. onCdata(text) {
  65. this._current[_xfa_object.$onText](text);
  66. }
  67. _mkAttributes(attributes, tagName) {
  68. let namespace = null;
  69. let prefixes = null;
  70. const attributeObj = Object.create({});
  71. for (const {
  72. name,
  73. value
  74. } of attributes) {
  75. if (name === "xmlns") {
  76. if (!namespace) {
  77. namespace = value;
  78. } else {
  79. (0, _util.warn)(`XFA - multiple namespace definition in <${tagName}>`);
  80. }
  81. } else if (name.startsWith("xmlns:")) {
  82. const prefix = name.substring("xmlns:".length);
  83. if (!prefixes) {
  84. prefixes = [];
  85. }
  86. prefixes.push({
  87. prefix,
  88. value
  89. });
  90. } else {
  91. const i = name.indexOf(":");
  92. if (i === -1) {
  93. attributeObj[name] = value;
  94. } else {
  95. let nsAttrs = attributeObj[_xfa_object.$nsAttributes];
  96. if (!nsAttrs) {
  97. nsAttrs = attributeObj[_xfa_object.$nsAttributes] = Object.create(null);
  98. }
  99. const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
  100. let attrs = nsAttrs[ns];
  101. if (!attrs) {
  102. attrs = nsAttrs[ns] = Object.create(null);
  103. }
  104. attrs[attrName] = value;
  105. }
  106. }
  107. }
  108. return [namespace, prefixes, attributeObj];
  109. }
  110. _getNameAndPrefix(name, nsAgnostic) {
  111. const i = name.indexOf(":");
  112. if (i === -1) {
  113. return [name, null];
  114. }
  115. return [name.substring(i + 1), nsAgnostic ? "" : name.substring(0, i)];
  116. }
  117. onBeginElement(tagName, attributes, isEmpty) {
  118. const [namespace, prefixes, attributesObj] = this._mkAttributes(attributes, tagName);
  119. const [name, nsPrefix] = this._getNameAndPrefix(tagName, this._builder.isNsAgnostic());
  120. const node = this._builder.build({
  121. nsPrefix,
  122. name,
  123. attributes: attributesObj,
  124. namespace,
  125. prefixes
  126. });
  127. node[_xfa_object.$globalData] = this._globalData;
  128. if (isEmpty) {
  129. node[_xfa_object.$finalize]();
  130. if (this._current[_xfa_object.$onChild](node)) {
  131. node[_xfa_object.$setId](this._ids);
  132. }
  133. node[_xfa_object.$clean](this._builder);
  134. return;
  135. }
  136. this._stack.push(this._current);
  137. this._current = node;
  138. }
  139. onEndElement(name) {
  140. const node = this._current;
  141. if (node[_xfa_object.$isCDATAXml]() && typeof node[_xfa_object.$content] === "string") {
  142. const parser = new XFAParser();
  143. parser._globalData = this._globalData;
  144. const root = parser.parse(node[_xfa_object.$content]);
  145. node[_xfa_object.$content] = null;
  146. node[_xfa_object.$onChild](root);
  147. }
  148. node[_xfa_object.$finalize]();
  149. this._current = this._stack.pop();
  150. if (this._current[_xfa_object.$onChild](node)) {
  151. node[_xfa_object.$setId](this._ids);
  152. }
  153. node[_xfa_object.$clean](this._builder);
  154. }
  155. onError(code) {
  156. this._errorCode = code;
  157. }
  158. }
  159. exports.XFAParser = XFAParser;