From 1c805c09ef1a9dbfb09c0e74fca32bba73aa7318 Mon Sep 17 00:00:00 2001
From: igor <igor.markin@vereign.com>
Date: Thu, 26 Nov 2020 14:32:30 +0300
Subject: [PATCH] Cover "chrome" cases

---
 __tests__/outlookHtml.test.ts            |  2 ++
 src/HTMLNormalizer/strategies/common.ts  | 12 +++++++-
 src/HTMLNormalizer/strategies/outlook.ts | 35 ++++++++++++++++++------
 3 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/__tests__/outlookHtml.test.ts b/__tests__/outlookHtml.test.ts
index 6ff3540..5ce7f0a 100644
--- a/__tests__/outlookHtml.test.ts
+++ b/__tests__/outlookHtml.test.ts
@@ -78,6 +78,8 @@ describe("Outlook emails HTML normalization", () => {
   test("Emails Chrome", async () => {
     const normalizedCases = await getNormalizedTestCases("chrome");
     normalizedCases.forEach(([sent, received]) => {
+      expect(received.length).toBeGreaterThan(0);
+      expect(sent.length).toBeGreaterThan(0);
       expect(received).toContain(sent);
     });
   });
diff --git a/src/HTMLNormalizer/strategies/common.ts b/src/HTMLNormalizer/strategies/common.ts
index b1601a2..d35e7cb 100644
--- a/src/HTMLNormalizer/strategies/common.ts
+++ b/src/HTMLNormalizer/strategies/common.ts
@@ -31,10 +31,20 @@ export const pruneElement = (element: HTMLElement): boolean => {
     return true;
   }
 
+  if (
+    element.nodeName.toLowerCase() === "div" &&
+    element.childNodes.length === 0
+  ) {
+    return true;
+  }
+
   return !!ELEMENT_TYPES_TO_REMOVE[element.nodeName.toLowerCase()];
 };
 
-export const cloneAnchorFromPane = (a: HTMLAnchorElement, pane: HTMLElement): void => {
+export const cloneAnchorFromPane = (
+  a: HTMLAnchorElement,
+  pane: HTMLElement
+): void => {
   try {
     const url = new URL(a.href);
     // If this is external url
diff --git a/src/HTMLNormalizer/strategies/outlook.ts b/src/HTMLNormalizer/strategies/outlook.ts
index 85f35f8..84d6630 100644
--- a/src/HTMLNormalizer/strategies/outlook.ts
+++ b/src/HTMLNormalizer/strategies/outlook.ts
@@ -1,7 +1,11 @@
 // TODO: Move this logic to amendOutlookNodes
-import {printHtmlChildren} from "../HTMLNormalizer";
-import {ELEMENT_NODE} from "../../constants";
-import {ATTRIBUTES_TO_KEEP, cloneAnchorFromPane, pruneElement} from "./common";
+import { printHtmlChildren } from "../HTMLNormalizer";
+import { ELEMENT_NODE } from "../../constants";
+import {
+  ATTRIBUTES_TO_KEEP,
+  cloneAnchorFromPane,
+  pruneElement,
+} from "./common";
 
 export const printOutlookElement = (node: Node): string => {
   if (node.nodeType === ELEMENT_NODE) {
@@ -25,6 +29,21 @@ export const pruneOutlookElement = (element: HTMLElement): boolean => {
 };
 
 export const amendOutlookNodes = (document: HTMLDocument): void => {
+  /**
+   * Remove quoted text
+   */
+  const appendOnSendNode = document.querySelector(
+    "[id*='appendonsend']"
+  ) as Node;
+  if (appendOnSendNode) {
+    let child = appendOnSendNode;
+    while (child) {
+      const nextSibling = child.nextSibling;
+      child.parentNode.removeChild(child);
+      child = nextSibling as Node;
+    }
+  }
+
   /**
    * Get rid of attachments panes
    */
@@ -45,9 +64,9 @@ export const amendOutlookNodes = (document: HTMLDocument): void => {
   }
 
   attachmentsPanesContainerEnd &&
-  attachmentsPanesContainerEnd.parentNode.removeChild(
-    attachmentsPanesContainerEnd
-  );
+    attachmentsPanesContainerEnd.parentNode.removeChild(
+      attachmentsPanesContainerEnd
+    );
 
   /**
    * Unwind spans, because sometimes Outlook wraps everything into span after sending
@@ -55,9 +74,7 @@ export const amendOutlookNodes = (document: HTMLDocument): void => {
 
   const spans = document.getElementsByTagName("span");
 
-  /**
-   * Sort spans by depth to start unwinding the deepest ones, which does not contain nested spans
-   */
+  //Sort spans by depth to start unwinding the deepest ones, which does not contain nested spans
   const spansDepths: { depth?: Array<Node> } = {};
   Array.from(spans).forEach((span: Node) => {
     let descendant = span;
-- 
GitLab