parser.js 4.4 KB

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