From 3b125026ddf9dc0634a859c9d5a74c1eab901eb9 Mon Sep 17 00:00:00 2001
From: igor <igor.markin@vereign.com>
Date: Thu, 17 Dec 2020 11:28:23 +0300
Subject: [PATCH] Add build

---
 dist/HTMLNormalizer/HTMLNormalizer.js      |  2 ++
 dist/HTMLNormalizer/strategies/common.d.ts |  1 +
 dist/HTMLNormalizer/strategies/common.js   | 16 ++++++++++++-
 dist/HTMLNormalizer/strategies/gmail.js    | 26 +++++++++++++++++++++-
 dist/HTMLNormalizer/strategies/outlook.js  | 11 ++++++---
 dist/PlainNormalizer/PlainNormalizer.js    | 16 +++++++++++--
 6 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/dist/HTMLNormalizer/HTMLNormalizer.js b/dist/HTMLNormalizer/HTMLNormalizer.js
index 1fdd9d9..bd37044 100644
--- a/dist/HTMLNormalizer/HTMLNormalizer.js
+++ b/dist/HTMLNormalizer/HTMLNormalizer.js
@@ -7,6 +7,7 @@ const constants_2 = require("../constants");
 const utils_1 = require("../utils");
 const gmail_1 = require("./strategies/gmail");
 const index_1 = require("../index");
+const common_1 = require("./strategies/common");
 const nodesAmendingFunctions = {
     [constants_2.EMAIL_VENDORS.GMAIL]: gmail_1.amendGmailNodes,
     [constants_2.EMAIL_VENDORS.OUTLOOK]: outlook_1.amendOutlookNodes,
@@ -24,6 +25,7 @@ const vendorPrintingFunctions = {
 };
 const normalizeVendorHtml = (document, vendor) => {
     const mimeBody = document.body;
+    common_1.amendNodes(document);
     const amendNodesFunction = nodesAmendingFunctions[vendor];
     if (amendNodesFunction) {
         amendNodesFunction(document);
diff --git a/dist/HTMLNormalizer/strategies/common.d.ts b/dist/HTMLNormalizer/strategies/common.d.ts
index 15043d7..941768b 100644
--- a/dist/HTMLNormalizer/strategies/common.d.ts
+++ b/dist/HTMLNormalizer/strategies/common.d.ts
@@ -13,6 +13,7 @@ export declare const ATTRIBUTES_TO_KEEP: {
     href: boolean;
     value: boolean;
 };
+export declare const amendNodes: (document: HTMLDocument) => void;
 /**
  * Decides whether node should be removed
  * @param element
diff --git a/dist/HTMLNormalizer/strategies/common.js b/dist/HTMLNormalizer/strategies/common.js
index 8bf68f4..4bcf054 100644
--- a/dist/HTMLNormalizer/strategies/common.js
+++ b/dist/HTMLNormalizer/strategies/common.js
@@ -1,6 +1,6 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.cloneAnchorFromPane = exports.pruneElement = exports.ATTRIBUTES_TO_KEEP = exports.ELEMENT_TYPES_TO_REMOVE = void 0;
+exports.cloneAnchorFromPane = exports.pruneElement = exports.amendNodes = exports.ATTRIBUTES_TO_KEEP = exports.ELEMENT_TYPES_TO_REMOVE = void 0;
 const DUMMY_QR_CODE_ID = "dummyQrCode";
 exports.ELEMENT_TYPES_TO_REMOVE = {
     br: true,
@@ -17,6 +17,20 @@ exports.ATTRIBUTES_TO_KEEP = {
     href: true,
     value: true,
 };
+const amendNodes = (document) => {
+    /**
+     * Unwind Outlook safelink wrappers
+     */
+    const anchors = document.getElementsByTagName("a");
+    for (const anchor of anchors) {
+        const url = new URL(anchor.getAttribute("href"));
+        if (url.host.includes("safelinks.protection.outlook.com")) {
+            const originalUrl = new URL(url.searchParams.get("url"));
+            anchor.setAttribute("href", originalUrl.href);
+        }
+    }
+};
+exports.amendNodes = amendNodes;
 /**
  * Removes dummy QR code from HTML
  * @param element
diff --git a/dist/HTMLNormalizer/strategies/gmail.js b/dist/HTMLNormalizer/strategies/gmail.js
index d179dc2..bb6db84 100644
--- a/dist/HTMLNormalizer/strategies/gmail.js
+++ b/dist/HTMLNormalizer/strategies/gmail.js
@@ -2,14 +2,38 @@
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.cleanupGMailElementAttributes = exports.amendGmailNodes = exports.pruneGmailElement = void 0;
 const common_1 = require("./common");
+const constants_1 = require("../../constants");
 const pruneGmailElement = (element) => {
     return common_1.pruneElement(element);
 };
 exports.pruneGmailElement = pruneGmailElement;
+const qrCodeContainerIds = { vereignWrapperLink: 1 };
+const removeQrCodeNodes = (document) => {
+    const remove = (node) => {
+        let toRemove = [];
+        let child = node.firstChild;
+        while (child) {
+            if (child.nodeType == constants_1.ELEMENT_NODE) {
+                toRemove = [...toRemove, ...remove(child)];
+                const childElement = child;
+                const id = childElement.getAttribute("id");
+                if (id &&
+                    Object.keys(qrCodeContainerIds).find((possibleId) => id.includes(possibleId))) {
+                    toRemove.push(childElement);
+                }
+            }
+            child = child.nextSibling;
+        }
+        return toRemove;
+    };
+    const elementsToRemove = remove(document.body);
+    elementsToRemove.forEach((element) => element.parentNode.removeChild(element));
+};
 const amendGmailNodes = (document) => {
     // unwindTags(document, "span");
+    removeQrCodeNodes(document);
     /**
-     * Look for attachments panes and remove everything but liks
+     * Look for attachments panes and remove everything but links
      */
     const attachmentsPanes = Array.from(document.getElementsByClassName("gmail_chip"));
     attachmentsPanes.forEach((pane) => {
diff --git a/dist/HTMLNormalizer/strategies/outlook.js b/dist/HTMLNormalizer/strategies/outlook.js
index e30b79d..2356919 100644
--- a/dist/HTMLNormalizer/strategies/outlook.js
+++ b/dist/HTMLNormalizer/strategies/outlook.js
@@ -25,7 +25,9 @@ const pruneOutlookElement = (element) => {
     return !!element.nodeName.toLowerCase().startsWith("o:");
 };
 exports.pruneOutlookElement = pruneOutlookElement;
-const qrCodeContainerId = "test-for-us";
+const qrCodeContainerIds = {
+    "test-for-us": 1,
+};
 const removeQrCodeNodes = (document) => {
     const remove = (node) => {
         let toRemove = [];
@@ -35,7 +37,8 @@ const removeQrCodeNodes = (document) => {
                 toRemove = [...toRemove, ...remove(child)];
                 const childElement = child;
                 const id = childElement.getAttribute("id");
-                if (id && id.includes(qrCodeContainerId)) {
+                if (id &&
+                    Object.keys(qrCodeContainerIds).find((possibleId) => id.includes(possibleId))) {
                     toRemove.push(childElement.parentNode);
                 }
             }
@@ -104,7 +107,9 @@ const amendOutlookNodes = (document) => {
     const attachmentsPanesContainerEnd = document.getElementById("OwaReferenceAttachmentsEnd");
     if (attachmentsPanesContainer) {
         const as = attachmentsPanesContainer.getElementsByTagName("a");
-        Array.from(as).forEach((a) => {
+        Array.from(as)
+            .sort((a, b) => a.getAttribute("href").localeCompare(b.getAttribute("href")))
+            .forEach((a) => {
             common_1.cloneAnchorFromPane(a, attachmentsPanesContainer);
         });
         attachmentsPanesContainer.parentNode.removeChild(attachmentsPanesContainer);
diff --git a/dist/PlainNormalizer/PlainNormalizer.js b/dist/PlainNormalizer/PlainNormalizer.js
index 342abdd..afa2a4d 100644
--- a/dist/PlainNormalizer/PlainNormalizer.js
+++ b/dist/PlainNormalizer/PlainNormalizer.js
@@ -5,13 +5,25 @@ const utils_1 = require("../utils");
 const normalizePlainPart = (text) => {
     text = removeListBullets(text);
     text = utils_1.removeSpacesAndLinebreaks(text);
+    text = patchOutlookSafelinksWrappers(text);
     return removeQRCodes(text);
 };
 exports.normalizePlainPart = normalizePlainPart;
+const patchOutlookSafelinksWrappers = (text) => {
+    const links = text.match(/<https:.+?(safelinks\.protection\.outlook\.com).+?>/g);
+    if (links) {
+        links.forEach((link) => {
+            const url = new URL(link.slice(1, link.length - 1));
+            const originalUrl = url.searchParams.get("url");
+            text = text.replace(link, `<${originalUrl}>`);
+        });
+    }
+    return text;
+};
 const removeQRCodes = (s) => {
     return s
-        .replace(/\[qrcode.png]\s*<https:\/\/[\w./?=\-&]+>/g, "")
-        .replace(/<https:\/\/[\w./?=\-&]+>\s*\[qrcode.png]/g, "");
+        .replace(/\[(image:)*qrcode.png]\s*<https:\/\/[\w./?=\-&]+>/g, "")
+        .replace(/<https:\/\/[\w./?=\-&]+>\s*\[(image: )*qrcode.png]/g, "");
 };
 const removeListBullets = (s) => {
     return s.replace("\n[o§]\n+/g", "");
-- 
GitLab