diff --git a/dist/HTMLNormalizer/HTMLNormalizer.js b/dist/HTMLNormalizer/HTMLNormalizer.js index 1fdd9d92969cc0029c87b36379160a54a3963009..bd37044fe90955725bc951a73dd8962d8d6fd5d2 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 15043d7a714331196c28bc66682601d860582228..941768bf2d735ef85586db2b570dfcd32f7432ca 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 8bf68f4807488fe2eddcdcabfc5e327f187779ec..4bcf054551b512b6c8b66944bf378caeaa458de6 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 d179dc21794aea0fc1eba35678477a9527928a22..bb6db84be869a40f4ebce5b44491531cd8718f0b 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 e30b79d3a9b51a0c19e1f6a4615a8042e75bd8bb..2356919cda8c677993e63fbbf1e132925c8a511e 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 342abdd0887e6066cd81d5c2515291417666585d..afa2a4d0f31a45ac4bfc1f78efdd697e93255da4 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", "");