diff --git a/dist/services/MimeVerificationService/MimeVerificationService.js b/dist/services/MimeVerificationService/MimeVerificationService.js index 2f9de54a7e83730737efbe311acdc47a9573baea..7c7d62478252d1b6c3dc1481cd89f338ea6d8b07 100644 --- a/dist/services/MimeVerificationService/MimeVerificationService.js +++ b/dist/services/MimeVerificationService/MimeVerificationService.js @@ -105,7 +105,7 @@ const normalizeVendorHtml = (htmlString, vendor) => { const { window } = new JSDOM(htmlString); document = window.document; } - const mimeBody = document.firstChild.lastChild; + const mimeBody = document.body; const amendNodesFunction = vendorAmendingFunctions[vendor]; if (amendNodesFunction) { amendNodesFunction(document); diff --git a/dist/services/MimeVerificationService/normalizationStrategies.js b/dist/services/MimeVerificationService/normalizationStrategies.js index 0071691d0e82953d41c5563071d8b15d203fcaf1..2491cccb5fd9ea2b88fc30b639f5b8dfded01c5a 100644 --- a/dist/services/MimeVerificationService/normalizationStrategies.js +++ b/dist/services/MimeVerificationService/normalizationStrategies.js @@ -53,46 +53,58 @@ exports.amendOutlookNodes = (document) => { /** * Get rid of attachments panes */ - const attachmentsPanes = document.querySelectorAll("#OwaReferenceAttachments"); - const attachmentsPanesEnds = document.querySelectorAll("#OwaReferenceAttachmentsEnd"); - attachmentsPanesEnds.forEach((pane) => pane.parentNode.removeChild(pane)); - attachmentsPanes.forEach((pane) => { - const as = pane.querySelectorAll("a"); - as.forEach((a) => { - cloneAnchorFromPane(a, pane); + const attachmentsPanesConatiner = document.getElementById("OwaReferenceAttachments"); + const attachmentsPanesContainerEnd = document.getElementById("OwaReferenceAttachmentsEnd"); + if (attachmentsPanesConatiner) { + const as = attachmentsPanesConatiner.getElementsByTagName("a"); + Array.from(as).forEach((a) => { + cloneAnchorFromPane(a, attachmentsPanesConatiner); }); - }); - attachmentsPanes.forEach((pane) => { - pane.parentNode.removeChild(pane); - }); + attachmentsPanesConatiner.parentNode.removeChild(attachmentsPanesConatiner); + } + attachmentsPanesContainerEnd && + attachmentsPanesContainerEnd.parentNode.removeChild(attachmentsPanesContainerEnd); /** * Unwind spans, because sometimes Outlook wraps everything into span after sending */ - const spans = document.querySelectorAll("span"); - spans.forEach((span) => { - let child = span.firstChild; - while (child) { - if (child.nodeName.toLowerCase() === "span") { + const spans = document.getElementsByTagName("span"); + /** + * Sort spans by depth to start unwinding the deepest ones, which does not contain nested spans + */ + const spansDepths = {}; + Array.from(spans).forEach((span) => { + let descendant = span; + let parent = descendant.parentNode; + let depth = 0; + while (parent && descendant !== parent) { + descendant = parent; + parent = descendant.parentNode; + depth++; + } + if (!spansDepths[depth]) { + spansDepths[depth] = []; + } + spansDepths[depth].push(span); + }); + Object.keys(spansDepths) + .sort((a, b) => parseInt(b) - parseInt(a)) + .forEach((depth) => { + spansDepths[depth].forEach((span) => { + let child = span.firstChild; + const parent = span.parentNode; + while (child) { + parent.insertBefore(child.cloneNode(true), span); child = child.nextSibling; - continue; - } - let parent = span.parentNode; - let lastSpan = span; - while (parent && parent.nodeName.toLowerCase() === "span") { - lastSpan = parent; - parent = parent.parentNode; } - parent.insertBefore(child.cloneNode(true), lastSpan); - child = child.nextSibling; - } + span.parentNode.removeChild(span); + }); }); - spans.forEach((span) => span.parentNode.removeChild(span)); }; exports.amendGmailNodes = (document) => { /** * Look for attachments panes and remove everything but liks */ - const attachmentsPanes = document.querySelectorAll(".gmail_chip"); + const attachmentsPanes = Array.from(document.getElementsByClassName("gmail_chip")); attachmentsPanes.forEach((pane) => { const as = pane.querySelectorAll("a"); as.forEach((a) => {