From b29df3350764af41bfa38b08a2ab3ee5dcc0e91a Mon Sep 17 00:00:00 2001
From: igor <igor.markin@vereign.com>
Date: Tue, 1 Dec 2020 13:46:26 +0300
Subject: [PATCH] Properly remove QR code from outlook.

---
 __tests__/files/gmail-outlook/empty      |  0
 __tests__/html-outlook-outlook.test.ts   | 54 ++----------------------
 src/HTMLNormalizer/strategies/outlook.ts | 35 +++++++++++++++
 3 files changed, 38 insertions(+), 51 deletions(-)
 delete mode 100644 __tests__/files/gmail-outlook/empty

diff --git a/__tests__/files/gmail-outlook/empty b/__tests__/files/gmail-outlook/empty
deleted file mode 100644
index e69de29..0000000
diff --git a/__tests__/html-outlook-outlook.test.ts b/__tests__/html-outlook-outlook.test.ts
index 0fa3d3a..e74341f 100644
--- a/__tests__/html-outlook-outlook.test.ts
+++ b/__tests__/html-outlook-outlook.test.ts
@@ -12,57 +12,9 @@ describe("Outlook emails HTML normalization", () => {
     EMAIL_VENDORS.OUTLOOK
   );
 
-  describe(
-    "Emails Chrome",
-    describeFunction("chrome", [
-      "20",
-      "20forward",
-      "20reply",
-      "21",
-      "21forward",
-      "21reply",
-      "22",
-      "23",
-      "24",
-      "25",
-      "26",
-      "28",
-    ])
-  );
-
-  describe(
-    "Emails Edge",
-    describeFunction("edge", [
-      "20",
-      "20forward",
-      "20reply",
-      "21",
-      "22",
-      "23",
-      "24",
-      "25",
-      "26",
-      "28",
-    ])
-  );
-  describe(
-    "Emails Safari",
-    describeFunction("safari", [
-      "04",
-      "20",
-      "20forward",
-      "20reply",
-      "21",
-      "21forward",
-      "21reply",
-      "22",
-      "23",
-      "24",
-      "25",
-      "26",
-      "28",
-    ])
-  );
+  describe("Emails Chrome", describeFunction("chrome"));
+  describe("Emails Edge", describeFunction("edge", ["21"]));
+  describe("Emails Safari", describeFunction("safari", ["04"]));
   // describe(
   //   "Emails MacOS",
   //   describeFunction("macos", ["20", "21", "22", "23", "24", "25", "26"])
diff --git a/src/HTMLNormalizer/strategies/outlook.ts b/src/HTMLNormalizer/strategies/outlook.ts
index b67248f..ab31b1a 100644
--- a/src/HTMLNormalizer/strategies/outlook.ts
+++ b/src/HTMLNormalizer/strategies/outlook.ts
@@ -28,6 +28,36 @@ export const pruneOutlookElement = (element: HTMLElement): boolean => {
   return !!element.nodeName.toLowerCase().startsWith("o:");
 };
 
+const qrCodeContainerId = "test-for-us";
+const removeQrCodeNodes = (document: HTMLDocument) => {
+  const remove = (node: Element) => {
+    let toRemove = [];
+
+    let child = node.firstChild;
+
+    while (child) {
+      if (child.nodeType == child.ELEMENT_NODE) {
+        toRemove = [...toRemove, ...remove(child as Element)];
+
+        const childElement = child as Element;
+        const id = childElement.getAttribute("id");
+        if (id && id.includes(qrCodeContainerId)) {
+          toRemove.push(childElement.parentNode);
+        }
+      }
+
+      child = child.nextSibling;
+    }
+
+    return toRemove;
+  };
+
+  const elementsToRemove = remove(document.body);
+  elementsToRemove.forEach((element) =>
+    element.parentNode.removeChild(element)
+  );
+};
+
 export const amendOutlookNodes = (document: HTMLDocument): void => {
   /**
    * Remove quoted text
@@ -78,6 +108,11 @@ export const amendOutlookNodes = (document: HTMLDocument): void => {
   //     child = nextSibling as Node;
   //   }
   // }
+  /**
+   * Remove QR code entries
+   */
+
+  removeQrCodeNodes(document);
 
   /**
    * Get rid of attachments panes
-- 
GitLab