Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-close any open BMC/BDC when ending text (bug 1898053) #18151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 37 additions & 11 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,7 @@ class PartialEvaluator {
const stateManager = new StateManager(initialState);
const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
const timeSlotManager = new TimeSlotManager();
let markedContentLevel = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaves subtly different than the code in getTextContent, which keeps the existing level-state when parsing /Form XObjects.

Perhaps that's not actually necessary to do, but my point here is that getOperatorList and getTextContent should handle markedContent-levels in /Form XObjects the same way (whatever that way is).


function closePendingRestoreOPS(argument) {
for (let i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
Expand All @@ -1753,7 +1754,7 @@ class PartialEvaluator {
timeSlotManager.reset();

const operation = {};
let stop, i, ii, cs, name, isValidName;
let stop, cs, name, isValidName;
while (!(stop = timeSlotManager.check())) {
// The arguments parsed by read() are used beyond this loop, so we
// cannot reuse the same array on each iteration. Therefore we pass
Expand Down Expand Up @@ -1914,6 +1915,12 @@ class PartialEvaluator {
break;
case OPS.endText:
parsingText = false;
if (markedContentLevel !== 0) {
for (let i = 0; i < markedContentLevel; i++) {
operatorList.addOp(OPS.endMarkedContent, []);
}
markedContentLevel = 0;
}
break;
case OPS.endInlineImage:
var cacheKey = args[0].cacheKey;
Expand Down Expand Up @@ -2227,6 +2234,7 @@ class PartialEvaluator {
// but doing so is meaningless without knowing the semantics.
continue;
case OPS.beginMarkedContentProps:
markedContentLevel++;
if (!(args[0] instanceof Name)) {
warn(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
operatorList.addOp(OPS.beginMarkedContentProps, ["OC", null]);
Expand Down Expand Up @@ -2269,21 +2277,26 @@ class PartialEvaluator {

break;
case OPS.beginMarkedContent:
markedContentLevel++;
if (args?.some(arg => arg instanceof Dict)) {
warn(`getOperatorList - ignoring operator: ${fn}`);
continue;
}
break;
case OPS.endMarkedContent:
markedContentLevel--;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to protect against this becoming negative, note

pdf.js/src/core/evaluator.js

Lines 3426 to 3434 in b7b8e5e

case OPS.endMarkedContent:
flushTextContentItem();
if (includeMarkedContent) {
if (markedContentData.level === 0) {
// Handle unbalanced beginMarkedContent/endMarkedContent
// operators (fixes issue15629.pdf).
break;
}
markedContentData.level--;

if (args?.some(arg => arg instanceof Dict)) {
warn(`getOperatorList - ignoring operator: ${fn}`);
continue;
}
break;
default:
// Note: Ignore the operator if it has `Dict` arguments, since
// those are non-serializable, otherwise postMessage will throw
// "An object could not be cloned.".
if (args !== null) {
for (i = 0, ii = args.length; i < ii; i++) {
if (args[i] instanceof Dict) {
break;
}
}
if (i < ii) {
warn("getOperatorList - ignoring operator: " + fn);
continue;
}
if (args?.some(arg => arg instanceof Dict)) {
warn(`getOperatorList - ignoring operator: ${fn}`);
continue;
}
}
operatorList.addOp(fn, args);
Expand Down Expand Up @@ -3142,6 +3155,19 @@ class PartialEvaluator {
textState.textMatrix = IDENTITY_MATRIX.slice();
textState.textLineMatrix = IDENTITY_MATRIX.slice();
break;
case OPS.endText:
if (includeMarkedContent) {
if (markedContentData.level !== 0) {
flushTextContentItem();
for (let i = 0; i < markedContentData.level; i++) {
textContent.items.push({
type: "endMarkedContent",
});
}
markedContentData.level = 0;
}
}
break;
case OPS.showSpacedText:
if (!stateManager.state.font) {
self.ensureStateFont(stateManager.state);
Expand Down
26 changes: 26 additions & 0 deletions test/integration/text_layer_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,30 @@ describe("Text layer", () => {
});
});
});

describe("check the dom depth (bug 1898053) ", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait(
"bug1898053_minimal.pdf",
".textLayer .endOfContent"
);
});
afterAll(async () => {
await closePages(pages);
});

it("must have the right number of children", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const count = await page.evaluate(
() =>
document.querySelectorAll(".textLayer > .markedContent").length
);
expect(count).toBe(6);
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,4 @@
!tracemonkey_freetext.pdf
!issue17998.pdf
!pdfjs_wikipedia.pdf
!bug1898053_minimal.pdf
Binary file added test/pdfs/bug1898053_minimal.pdf
Binary file not shown.