forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxfa_layer.js
More file actions
280 lines (255 loc) · 8.11 KB
/
xfa_layer.js
File metadata and controls
280 lines (255 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @typedef {import("./display_utils").PageViewport} PageViewport */
/** @typedef {import("../../web/interfaces").IPDFLinkService} IPDFLinkService */
import { XfaText } from "./xfa_text.js";
/**
* @typedef {Object} XfaLayerParameters
* @property {PageViewport} viewport
* @property {HTMLDivElement} div
* @property {Object} xfaHtml
* @property {AnnotationStorage} [annotationStorage]
* @property {IPDFLinkService} linkService
* @property {string} [intent] - (default value is 'display').
*/
class XfaLayer {
static setupStorage(html, id, element, storage, intent) {
const storedData = storage.getValue(id, { value: null });
switch (element.name) {
case "textarea":
if (storedData.value !== null) {
html.textContent = storedData.value;
}
if (intent === "print") {
break;
}
html.addEventListener("input", event => {
storage.setValue(id, { value: event.target.value });
});
break;
case "input":
if (
element.attributes.type === "radio" ||
element.attributes.type === "checkbox"
) {
if (storedData.value === element.attributes.xfaOn) {
html.setAttribute("checked", true);
} else if (storedData.value === element.attributes.xfaOff) {
// The checked attribute may have been set when opening the file,
// unset through the UI and we're here because of printing.
html.removeAttribute("checked");
}
if (intent === "print") {
break;
}
html.addEventListener("change", event => {
storage.setValue(id, {
value: event.target.checked
? event.target.getAttribute("xfaOn")
: event.target.getAttribute("xfaOff"),
});
});
} else {
if (storedData.value !== null) {
html.setAttribute("value", storedData.value);
}
if (intent === "print") {
break;
}
html.addEventListener("input", event => {
storage.setValue(id, { value: event.target.value });
});
}
break;
case "select":
if (storedData.value !== null) {
for (const option of element.children) {
if (option.attributes.value === storedData.value) {
option.attributes.selected = true;
}
}
}
html.addEventListener("input", event => {
const options = event.target.options;
const value =
options.selectedIndex === -1
? ""
: options[options.selectedIndex].value;
storage.setValue(id, { value });
});
break;
}
}
static setAttributes({ html, element, storage = null, intent, linkService }) {
const { attributes } = element;
const isHTMLAnchorElement = html instanceof HTMLAnchorElement;
if (attributes.type === "radio") {
// Avoid to have a radio group when printing with the same as one
// already displayed.
attributes.name = `${attributes.name}-${intent}`;
}
for (const [key, value] of Object.entries(attributes)) {
if (value === null || value === undefined) {
continue;
}
switch (key) {
case "class":
if (value.length) {
html.setAttribute(key, value.join(" "));
}
break;
case "dataId":
// We don't need to add dataId in the html object but it can
// be useful to know its value when writing printing tests:
// in this case, don't skip dataId to have its value.
break;
case "id":
html.setAttribute("data-element-id", value);
break;
case "style":
Object.assign(html.style, value);
break;
case "textContent":
html.textContent = value;
break;
default:
if (!isHTMLAnchorElement || (key !== "href" && key !== "newWindow")) {
html.setAttribute(key, value);
}
}
}
if (isHTMLAnchorElement) {
linkService.addLinkAttributes(
html,
attributes.href,
attributes.newWindow
);
}
// Set the value after the others to be sure to overwrite any other values.
if (storage && attributes.dataId) {
this.setupStorage(html, attributes.dataId, element, storage);
}
}
/**
* Render the XFA layer.
*
* @param {XfaLayerParameters} parameters
*/
static render(parameters) {
const storage = parameters.annotationStorage;
const linkService = parameters.linkService;
const root = parameters.xfaHtml;
const intent = parameters.intent || "display";
const rootHtml = document.createElement(root.name);
if (root.attributes) {
this.setAttributes({
html: rootHtml,
element: root,
intent,
linkService,
});
}
const stack = [[root, -1, rootHtml]];
const rootDiv = parameters.div;
rootDiv.append(rootHtml);
if (parameters.viewport) {
const transform = `matrix(${parameters.viewport.transform.join(",")})`;
rootDiv.style.transform = transform;
}
// Set defaults.
if (intent !== "richText") {
rootDiv.setAttribute("class", "xfaLayer xfaFont");
}
// Text nodes used for the text highlighter.
const textDivs = [];
while (stack.length > 0) {
const [parent, i, html] = stack.at(-1);
if (i + 1 === parent.children.length) {
stack.pop();
continue;
}
const child = parent.children[++stack.at(-1)[1]];
if (child === null) {
continue;
}
const { name } = child;
if (name === "#text") {
const node = document.createTextNode(child.value);
textDivs.push(node);
html.append(node);
continue;
}
let childHtml;
if (child?.attributes?.xmlns) {
childHtml = document.createElementNS(child.attributes.xmlns, name);
} else {
childHtml = document.createElement(name);
}
html.append(childHtml);
if (child.attributes) {
this.setAttributes({
html: childHtml,
element: child,
storage,
intent,
linkService,
});
}
if (child.children && child.children.length > 0) {
stack.push([child, -1, childHtml]);
} else if (child.value) {
const node = document.createTextNode(child.value);
if (XfaText.shouldBuildText(name)) {
textDivs.push(node);
}
childHtml.append(node);
}
}
/**
* TODO: re-enable that stuff once we've JS implementation.
* See https://bugzilla.mozilla.org/show_bug.cgi?id=1719465.
*
* for (const el of rootDiv.querySelectorAll(
* ".xfaDisabled input, .xfaDisabled textarea"
* )) {
* el.setAttribute("disabled", true);
* }
* for (const el of rootDiv.querySelectorAll(
* ".xfaReadOnly input, .xfaReadOnly textarea"
* )) {
* el.setAttribute("readOnly", true);
* }
*/
for (const el of rootDiv.querySelectorAll(
".xfaNonInteractive input, .xfaNonInteractive textarea"
)) {
el.setAttribute("readOnly", true);
}
return {
textDivs,
};
}
/**
* Update the XFA layer.
*
* @param {XfaLayerParameters} parameters
*/
static update(parameters) {
const transform = `matrix(${parameters.viewport.transform.join(",")})`;
parameters.div.style.transform = transform;
parameters.div.hidden = false;
}
}
export { XfaLayer };