From 1e7274e9c60c46b351591ec5b7b1b035ee2d402d Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Wed, 26 Oct 2022 19:43:17 +0200
Subject: [PATCH] [api-minor] Move the handling of unbalanced markedContent to
 the worker-thread (PR 15630 follow-up)

---
 src/core/evaluator.js     | 16 ++++++++++++++++
 src/display/text_layer.js | 11 +----------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index 84a58fe2b..2f982b575 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2283,12 +2283,16 @@ class PartialEvaluator {
     sink,
     seenStyles = new Set(),
     viewBox,
+    markedContentData = null,
   }) {
     // Ensure that `resources`/`stateManager` is correctly initialized,
     // even if the provided parameter is e.g. `null`.
     resources = resources || Dict.empty;
     stateManager = stateManager || new StateManager(new TextState());
 
+    if (includeMarkedContent) {
+      markedContentData = markedContentData || { level: 0 };
+    }
     const NormalizedUnicodes = getNormalizedUnicodes();
 
     const textContent = {
@@ -3225,6 +3229,7 @@ class PartialEvaluator {
                     sink: sinkWrapper,
                     seenStyles,
                     viewBox,
+                    markedContentData,
                   })
                   .then(function () {
                     if (!sinkWrapper.enqueueInvoked) {
@@ -3305,6 +3310,8 @@ class PartialEvaluator {
           case OPS.beginMarkedContent:
             flushTextContentItem();
             if (includeMarkedContent) {
+              markedContentData.level++;
+
               textContent.items.push({
                 type: "beginMarkedContent",
                 tag: args[0] instanceof Name ? args[0].name : null,
@@ -3314,6 +3321,8 @@ class PartialEvaluator {
           case OPS.beginMarkedContentProps:
             flushTextContentItem();
             if (includeMarkedContent) {
+              markedContentData.level++;
+
               let mcid = null;
               if (args[1] instanceof Dict) {
                 mcid = args[1].get("MCID");
@@ -3330,6 +3339,13 @@ class PartialEvaluator {
           case OPS.endMarkedContent:
             flushTextContentItem();
             if (includeMarkedContent) {
+              if (markedContentData.level === 0) {
+                // Handle unbalanced beginMarkedContent/endMarkedContent
+                // operators (fixes issue15629.pdf).
+                break;
+              }
+              markedContentData.level--;
+
               textContent.items.push({
                 type: "endMarkedContent",
               });
diff --git a/src/display/text_layer.js b/src/display/text_layer.js
index 5d43cdc0c..bf674bcb3 100644
--- a/src/display/text_layer.js
+++ b/src/display/text_layer.js
@@ -224,8 +224,6 @@ function render(task) {
 }
 
 class TextLayerRenderTask {
-  #initialContainer = null;
-
   constructor({
     textContent,
     textContentStream,
@@ -237,7 +235,6 @@ class TextLayerRenderTask {
     this._textContent = textContent;
     this._textContentStream = textContentStream;
     this._container = container;
-    this.#initialContainer = container;
     this._document = container.ownerDocument;
     this._viewport = viewport;
     this._textDivs = textDivs || [];
@@ -321,13 +318,7 @@ class TextLayerRenderTask {
           }
           parent.append(this._container);
         } else if (item.type === "endMarkedContent") {
-          const parent = this._container.parentNode;
-          if (!parent || this._container === this.#initialContainer) {
-            // Handle unbalanced beginMarkedContent/endMarkedContent operators
-            // (fixes issue15629.pdf).
-            continue;
-          }
-          this._container = parent;
+          this._container = this._container.parentNode;
         }
         continue;
       }