UNPKG

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