struct_tree_layer_builder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #treeDom = undefined;
  72. get renderingDone() {
  73. return this.#treeDom !== undefined;
  74. }
  75. render(structTree) {
  76. if (this.#treeDom !== undefined) {
  77. return this.#treeDom;
  78. }
  79. const treeDom = this.#walk(structTree);
  80. treeDom?.classList.add("structTree");
  81. return this.#treeDom = treeDom;
  82. }
  83. #setAttributes(structElement, htmlElement) {
  84. if (structElement.alt !== undefined) {
  85. htmlElement.setAttribute("aria-label", structElement.alt);
  86. }
  87. if (structElement.id !== undefined) {
  88. htmlElement.setAttribute("aria-owns", structElement.id);
  89. }
  90. if (structElement.lang !== undefined) {
  91. htmlElement.setAttribute("lang", structElement.lang);
  92. }
  93. }
  94. #walk(node) {
  95. if (!node) {
  96. return null;
  97. }
  98. const element = document.createElement("span");
  99. if ("role" in node) {
  100. const {
  101. role
  102. } = node;
  103. const match = role.match(HEADING_PATTERN);
  104. if (match) {
  105. element.setAttribute("role", "heading");
  106. element.setAttribute("aria-level", match[1]);
  107. } else if (PDF_ROLE_TO_HTML_ROLE[role]) {
  108. element.setAttribute("role", PDF_ROLE_TO_HTML_ROLE[role]);
  109. }
  110. }
  111. this.#setAttributes(node, element);
  112. if (node.children) {
  113. if (node.children.length === 1 && "id" in node.children[0]) {
  114. this.#setAttributes(node.children[0], element);
  115. } else {
  116. for (const kid of node.children) {
  117. element.append(this.#walk(kid));
  118. }
  119. }
  120. }
  121. return element;
  122. }
  123. }
  124. exports.StructTreeLayerBuilder = StructTreeLayerBuilder;