Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4136,12 +4136,19 @@ class AnnotationLayer {
this.#hasAriaAttributesFromStructTree = true;
for (const {
contentElement,
data: { id },
data: { hidden, id, oc },
} of this.#elements) {
const annotationId = (contentElement.id = `${AnnotationPrefix}${id}`);
// An unbound link has no <a>, hidden links aren't exposed, and
// optional-content visibility can change after this one-time setup.
// Keep the structure-tree Link fallback in all three cases; a visible
// optional-content link can therefore remain duplicated, matching the
// pre-existing behavior.
const enableLinkOwnership =
contentElement.localName === "a" && !hidden && !oc;
promises.push(
this.#structTreeLayer
?.getAriaAttributes(annotationId)
?.getAriaAttributes(annotationId, { enableLinkOwnership })
.then(ariaAttributes => {
if (ariaAttributes) {
for (const [key, value] of ariaAttributes) {
Expand Down
6 changes: 1 addition & 5 deletions test/unit/cff_parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ describe("CFFParser", function () {
"8b06f79a93fc7c8c077d99f85695f75e" +
"9908fb6e8cf87393f7108b09a70adf0b" +
"f78e14";
const fontArr = [];
for (let i = 0, ii = exampleFont.length; i < ii; i += 2) {
const hex = exampleFont.substring(i, i + 2);
fontArr.push(parseInt(hex, 16));
}
const fontArr = Uint8Array.fromHex(exampleFont);
fontData = new Stream(fontArr);
});

Expand Down
43 changes: 43 additions & 0 deletions test/unit/struct_tree_layer_builder_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,49 @@ describe("StructTreeLayerBuilder", function () {
expect(row.id).toEqual("");
});

it("only lets an eligible link annotation own its tagged text", async function () {
const annotationId = "pdfjs_internal_id_1R";
const builder = build({
role: "Root",
children: [
{
role: "Link",
children: [
{ type: "content", id: "p1R_mc1" },
{
role: "Lbl",
// BBox is layout-only.
bbox: [0, 0, 10, 10],
children: [{ type: "content", id: "p1R_mc2" }],
},
{ type: "annotation", id: annotationId },
],
},
],
});
const tree = await builder.render();
const link = tree.firstElementChild;
expect(link.getAttribute("role")).toEqual("link");
const [text, label, annotation] = link.children;
expect(await builder.getAnnotationIds()).toEqual(new Set([annotationId]));
expect([text.id, label.id]).not.toContain("");
expect(annotation.getAttribute("aria-owns")).toEqual(annotationId);

// Merely asking for the annotation attributes keeps the fallback.
expect(await builder.getAriaAttributes(annotationId)).toBeUndefined();
expect(link.getAttribute("role")).toEqual("link");

const attributes = await builder.getAriaAttributes(annotationId, {
enableLinkOwnership: true,
});

expect(attributes).toEqual(
new Map([["aria-owns", `${text.id} ${label.id}`]])
);
expect(link.getAttribute("role")).toBeNull();
expect(link.childElementCount).toEqual(3);
});

it("keeps structured annotations in the structure tree", async function () {
const builder = build({
role: "Root",
Expand Down
42 changes: 41 additions & 1 deletion web/struct_tree_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class StructTreeLayerBuilder {

#elementAttributes = new Map();

#pendingLinkOwnership = new Map();

#linkTextId = 0;

#structElementIdPrefix = `pdfjs_internal_struct_${getUuid()}_`;

#structElementIds = new Map();
Expand Down Expand Up @@ -245,9 +249,26 @@ class StructTreeLayerBuilder {
return promise;
}

async getAriaAttributes(annotationId) {
/**
* @param {string} annotationId
* @param {Object} [options]
* @param {boolean} [options.enableLinkOwnership]
* @returns {Promise<Map<string, string>|null|undefined>}
*/
async getAriaAttributes(annotationId, { enableLinkOwnership = false } = {}) {
try {
await this.render();
const ownership = this.#pendingLinkOwnership.get(annotationId);
if (ownership && enableLinkOwnership) {
const { element, ids } = ownership;
element.removeAttribute("role");
if (ids.length > 0) {
this.#elementAttributes
.getOrInsertComputed(annotationId, makeMap)
.set("aria-owns", ids.join(" "));
}
this.#pendingLinkOwnership.delete(annotationId);
}
return this.#elementAttributes.get(annotationId);
} catch {
// If the structTree cannot be fetched, parsed, and/or rendered,
Expand Down Expand Up @@ -639,6 +660,25 @@ class StructTreeLayerBuilder {
parentNodes.pop();
}
}
if (node.role === "Link") {
const annotations = node.children?.filter(
child => child.type === "annotation"
);
if (annotations?.length === 1) {
const annotation = annotations[0];
const ids = [];
for (const child of element.children) {
if (child.getAttribute("aria-owns") === annotation.id) {
continue;
}
child.id ||= `${this.#structElementIdPrefix}link_${this.#linkTextId++}`;
ids.push(child.id);
}
// Keep the structure-tree Link as a fallback until a stable, visible
// link annotation explicitly takes ownership of these children.
this.#pendingLinkOwnership.set(annotation.id, { element, ids });
}
}
return element;
}
}
Expand Down
Loading