From cb422a80b0b40555c4fe58330b3552f6e6faace6 Mon Sep 17 00:00:00 2001
From: Tim van der Meij <timvandermeij@gmail.com>
Date: Sat, 5 Dec 2020 19:32:13 +0100
Subject: [PATCH] Move quadrilateral rendering logic into a method on the
 `AnnotationElement` class

Doing so avoids some code duplication.
---
 src/display/annotation_layer.js | 43 +++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js
index f91973af8..7b3ccffb6 100644
--- a/src/display/annotation_layer.js
+++ b/src/display/annotation_layer.js
@@ -23,6 +23,7 @@ import {
 import {
   AnnotationBorderStyleType,
   AnnotationType,
+  assert,
   stringToPDFString,
   unreachable,
   Util,
@@ -324,6 +325,28 @@ class AnnotationElement {
     container.appendChild(popup);
   }
 
+  /**
+   * Render the quadrilaterals of the annotation.
+   *
+   * @private
+   * @param {string} className
+   * @memberof AnnotationElement
+   * @returns {Array<HTMLSectionElement>}
+   */
+  _renderQuadrilaterals(className) {
+    if (
+      typeof PDFJSDev === "undefined" ||
+      PDFJSDev.test("!PRODUCTION || TESTING")
+    ) {
+      assert(this.quadrilaterals, "Missing quadrilaterals during rendering");
+    }
+
+    this.quadrilaterals.forEach(quadrilateral => {
+      quadrilateral.className = className;
+    });
+    return this.quadrilaterals;
+  }
+
   /**
    * Render the annotation's HTML element in the empty container.
    *
@@ -1463,10 +1486,7 @@ class HighlightAnnotationElement extends AnnotationElement {
     }
 
     if (this.quadrilaterals) {
-      this.quadrilaterals.forEach(quadrilateral => {
-        quadrilateral.className = "highlightAnnotation";
-      });
-      return this.quadrilaterals;
+      return this._renderQuadrilaterals("highlightAnnotation");
     }
 
     this.container.className = "highlightAnnotation";
@@ -1501,10 +1521,7 @@ class UnderlineAnnotationElement extends AnnotationElement {
     }
 
     if (this.quadrilaterals) {
-      this.quadrilaterals.forEach(quadrilateral => {
-        quadrilateral.className = "underlineAnnotation";
-      });
-      return this.quadrilaterals;
+      return this._renderQuadrilaterals("underlineAnnotation");
     }
 
     this.container.className = "underlineAnnotation";
@@ -1539,10 +1556,7 @@ class SquigglyAnnotationElement extends AnnotationElement {
     }
 
     if (this.quadrilaterals) {
-      this.quadrilaterals.forEach(quadrilateral => {
-        quadrilateral.className = "squigglyAnnotation";
-      });
-      return this.quadrilaterals;
+      return this._renderQuadrilaterals("squigglyAnnotation");
     }
 
     this.container.className = "squigglyAnnotation";
@@ -1577,10 +1591,7 @@ class StrikeOutAnnotationElement extends AnnotationElement {
     }
 
     if (this.quadrilaterals) {
-      this.quadrilaterals.forEach(quadrilateral => {
-        quadrilateral.className = "strikeoutAnnotation";
-      });
-      return this.quadrilaterals;
+      return this._renderQuadrilaterals("strikeoutAnnotation");
     }
 
     this.container.className = "strikeoutAnnotation";