From 09f79ffa92497bf08940bd1995a9d3effd97ed4a Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 9 Dec 2020 20:48:36 +0100 Subject: [PATCH] Attempt to handle collapsed outline items, in the default viewer, according to the specification (issue 12704, PR 10890 follow-up) This patch *attempts* to actually implement what's described for the `Count`-entry in the PDF specification, see https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf#G11.2095911, which I mostly ignored back in PR 10890 since it seemed unnecessarily complicated[1]. Besides issue 12704, I've also tested a couple of other documents (e.g. the PDF specification) and these changes don't *seem* to break anything else; additional testing would be helpful though! --- [1] At the time, all PDF documents that I tested worked even with a very simple approach and I thus hoped that it'd would suffice. --- web/pdf_outline_viewer.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web/pdf_outline_viewer.js b/web/pdf_outline_viewer.js index 5dbdf1360e327..7d584564c3c34 100644 --- a/web/pdf_outline_viewer.js +++ b/web/pdf_outline_viewer.js @@ -95,7 +95,23 @@ class PDFOutlineViewer extends BaseTreeViewer { * @private */ _addToggleButton(div, { count, items }) { - const hidden = count < 0 && Math.abs(count) === items.length; + let hidden = false; + if (count < 0) { + let totalCount = items.length; + if (totalCount > 0) { + const queue = [...items]; + while (queue.length > 0) { + const { count: nestedCount, items: nestedItems } = queue.shift(); + if (nestedCount > 0 && nestedItems.length > 0) { + totalCount += nestedItems.length; + queue.push(...nestedItems); + } + } + } + if (Math.abs(count) === totalCount) { + hidden = true; + } + } super._addToggleButton(div, hidden); }