xfa_text.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright 2021 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. class XfaText {
  16. /**
  17. * Walk an XFA tree and create an array of text nodes that is compatible
  18. * with a regular PDFs TextContent. Currently, only TextItem.str is supported,
  19. * all other fields and styles haven't been implemented.
  20. *
  21. * @param {Object} xfa - An XFA fake DOM object.
  22. *
  23. * @returns {TextContent}
  24. */
  25. static textContent(xfa) {
  26. const items = [];
  27. const output = {
  28. items,
  29. styles: Object.create(null),
  30. };
  31. function walk(node) {
  32. if (!node) {
  33. return;
  34. }
  35. let str = null;
  36. const name = node.name;
  37. if (name === "#text") {
  38. str = node.value;
  39. } else if (!XfaText.shouldBuildText(name)) {
  40. return;
  41. } else if (node?.attributes?.textContent) {
  42. str = node.attributes.textContent;
  43. } else if (node.value) {
  44. str = node.value;
  45. }
  46. if (str !== null) {
  47. items.push({
  48. str,
  49. });
  50. }
  51. if (!node.children) {
  52. return;
  53. }
  54. for (const child of node.children) {
  55. walk(child);
  56. }
  57. }
  58. walk(xfa);
  59. return output;
  60. }
  61. /**
  62. * @param {string} name - DOM node name. (lower case)
  63. *
  64. * @returns {boolean} true if the DOM node should have a corresponding text
  65. * node.
  66. */
  67. static shouldBuildText(name) {
  68. return !(
  69. name === "textarea" ||
  70. name === "input" ||
  71. name === "option" ||
  72. name === "select"
  73. );
  74. }
  75. }
  76. export { XfaText };