struct_tree_layer_builder.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.StructTreeLayerBuilder = void 0;
  27. const PDF_ROLE_TO_HTML_ROLE = {
  28. Document: null,
  29. DocumentFragment: null,
  30. Part: "group",
  31. Sect: "group",
  32. Div: "group",
  33. Aside: "note",
  34. NonStruct: "none",
  35. P: null,
  36. H: "heading",
  37. Title: null,
  38. FENote: "note",
  39. Sub: "group",
  40. Lbl: null,
  41. Span: null,
  42. Em: null,
  43. Strong: null,
  44. Link: "link",
  45. Annot: "note",
  46. Form: "form",
  47. Ruby: null,
  48. RB: null,
  49. RT: null,
  50. RP: null,
  51. Warichu: null,
  52. WT: null,
  53. WP: null,
  54. L: "list",
  55. LI: "listitem",
  56. LBody: null,
  57. Table: "table",
  58. TR: "row",
  59. TH: "columnheader",
  60. TD: "cell",
  61. THead: "columnheader",
  62. TBody: null,
  63. TFoot: null,
  64. Caption: null,
  65. Figure: "figure",
  66. Formula: null,
  67. Artifact: null
  68. };
  69. const HEADING_PATTERN = /^H(\d+)$/;
  70. class StructTreeLayerBuilder {
  71. constructor({
  72. pdfPage
  73. }) {
  74. this.pdfPage = pdfPage;
  75. }
  76. render(structTree) {
  77. return this._walk(structTree);
  78. }
  79. _setAttributes(structElement, htmlElement) {
  80. if (structElement.alt !== undefined) {
  81. htmlElement.setAttribute("aria-label", structElement.alt);
  82. }
  83. if (structElement.id !== undefined) {
  84. htmlElement.setAttribute("aria-owns", structElement.id);
  85. }
  86. if (structElement.lang !== undefined) {
  87. htmlElement.setAttribute("lang", structElement.lang);
  88. }
  89. }
  90. _walk(node) {
  91. if (!node) {
  92. return null;
  93. }
  94. const element = document.createElement("span");
  95. if ("role" in node) {
  96. const {
  97. role
  98. } = node;
  99. const match = role.match(HEADING_PATTERN);
  100. if (match) {
  101. element.setAttribute("role", "heading");
  102. element.setAttribute("aria-level", match[1]);
  103. } else if (PDF_ROLE_TO_HTML_ROLE[role]) {
  104. element.setAttribute("role", PDF_ROLE_TO_HTML_ROLE[role]);
  105. }
  106. }
  107. this._setAttributes(node, element);
  108. if (node.children) {
  109. if (node.children.length === 1 && "id" in node.children[0]) {
  110. this._setAttributes(node.children[0], element);
  111. } else {
  112. for (const kid of node.children) {
  113. element.append(this._walk(kid));
  114. }
  115. }
  116. }
  117. return element;
  118. }
  119. }
  120. exports.StructTreeLayerBuilder = StructTreeLayerBuilder;