1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.StructTreeLayerBuilder = void 0;
|
28 | const 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 | };
|
70 | const HEADING_PATTERN = /^H(\d+)$/;
|
71 |
|
72 | class 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 |
|
135 | exports.StructTreeLayerBuilder = StructTreeLayerBuilder; |
\ | No newline at end of file |