Skip to content

Commit

Permalink
[api-minor] Let PDFPageProxy.getStructTree return null, rather th…
Browse files Browse the repository at this point in the history
…an an empty structTree, for documents without any accessibility data (PR 13171 follow-up)

This is first of all consistent with existing API-methods, where we return `null` when the data in question doesn't exist. Secondly, it should also be (slightly) more efficient since there's less dummy-data that we need to transfer between threads.
Finally, this prevents us from adding an empty/unnecessary span to *every* single page even in documents without any structure tree data.
  • Loading branch information
Snuffleupagus committed Apr 11, 2021
1 parent ff4dae0 commit 5adee0c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ class StructTreePage {
}
nodeToSerializable(child, root);
}

if (root.children.length === 0) {
return null;
}
return root;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,8 @@ class PDFPageProxy {

/**
* @returns {Promise<StructTreeNode>} A promise that is resolved with a
* {@link StructTreeNode} object that represents the page's structure tree.
* {@link StructTreeNode} object that represents the page's structure tree,
* or `null` when no structure tree is present for the current page.
*/
getStructTree() {
return (this._structTreePromise ||= this._transport.getStructTree(
Expand Down
63 changes: 63 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,69 @@ describe("api", function () {
.catch(done.fail);
});

it("gets empty structure tree", async function () {
const tree = await page.getStructTree();

expect(tree).toEqual(null);
});
it("gets simple structure tree", async function () {
const loadingTask = getDocument(
buildGetDocumentParams("structure_simple.pdf")
);
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);
const tree = await pdfPage.getStructTree();

expect(tree).toEqual({
role: "Root",
children: [
{
role: "Document",
children: [
{
role: "H1",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid0" }],
},
],
},
{
role: "P",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid1" }],
},
],
},
{
role: "H2",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid2" }],
},
],
},
{
role: "P",
children: [
{
role: "NonStruct",
children: [{ type: "content", id: "page2R_mcid3" }],
},
],
},
],
},
],
});

await loadingTask.destroy();
});

it("gets operator list", function (done) {
const promise = page.getOperatorList();
promise
Expand Down
3 changes: 3 additions & 0 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ class PDFPageView {
this.eventBus._off("textlayerrendered", this._onTextLayerRendered);
this._onTextLayerRendered = null;
this.pdfPage.getStructTree().then(tree => {
if (!tree) {
return;
}
const treeDom = this.structTreeLayer.render(tree);
treeDom.classList.add("structTree");
this.canvas.appendChild(treeDom);
Expand Down

0 comments on commit 5adee0c

Please sign in to comment.