From be8dc5d9c495567183e3b1bc19c1687fd3e71b1c Mon Sep 17 00:00:00 2001 From: igor <igor.markin@vereign.com> Date: Tue, 15 Dec 2020 13:25:34 +0300 Subject: [PATCH] Strip qr code from gmail plain and html --- __tests__/html-gmail-gmail.test.ts | 2 +- ...l.test.ts => htmltext-gmail-gmail.test.ts} | 0 ...test.ts => htmltext-gmail-outlook.test.ts} | 0 ...st.ts => htmltext-outlook-outlook.test.ts} | 0 __tests__/utils.ts | 30 +++++++++++--- src/HTMLNormalizer/strategies/gmail.ts | 40 ++++++++++++++++++- src/HTMLNormalizer/strategies/outlook.ts | 11 ++++- src/PlainNormalizer/PlainNormalizer.ts | 4 +- 8 files changed, 76 insertions(+), 11 deletions(-) rename __tests__/{pseudoplain-gmail-gmail.test.ts => htmltext-gmail-gmail.test.ts} (100%) rename __tests__/{pseudoplain-gmail-outlook.test.ts => htmltext-gmail-outlook.test.ts} (100%) rename __tests__/{pseudoplain-outlook-outlook.test.ts => htmltext-outlook-outlook.test.ts} (100%) diff --git a/__tests__/html-gmail-gmail.test.ts b/__tests__/html-gmail-gmail.test.ts index c0901b7..6b2a782 100644 --- a/__tests__/html-gmail-gmail.test.ts +++ b/__tests__/html-gmail-gmail.test.ts @@ -13,5 +13,5 @@ describe("[HTML] GMail-GMail", () => { EMAIL_VENDORS.GMAIL ); - describe("One", describeFunction("one", [""])); + describe("One", describeFunction("one", ["sssas"])); }); diff --git a/__tests__/pseudoplain-gmail-gmail.test.ts b/__tests__/htmltext-gmail-gmail.test.ts similarity index 100% rename from __tests__/pseudoplain-gmail-gmail.test.ts rename to __tests__/htmltext-gmail-gmail.test.ts diff --git a/__tests__/pseudoplain-gmail-outlook.test.ts b/__tests__/htmltext-gmail-outlook.test.ts similarity index 100% rename from __tests__/pseudoplain-gmail-outlook.test.ts rename to __tests__/htmltext-gmail-outlook.test.ts diff --git a/__tests__/pseudoplain-outlook-outlook.test.ts b/__tests__/htmltext-outlook-outlook.test.ts similarity index 100% rename from __tests__/pseudoplain-outlook-outlook.test.ts rename to __tests__/htmltext-outlook-outlook.test.ts diff --git a/__tests__/utils.ts b/__tests__/utils.ts index ef875f2..9d0a0aa 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -8,7 +8,23 @@ const RECEIVED_PLAIN_NAME = "r_plainContent.data"; import { PlainNormalizer, HTMLNormalizer } from "../src"; import { expect, test } from "@jest/globals"; import { DOM } from "@vereign/dom"; -//import { diffStringsUnified } from "jest-diff"; +import { diffStringsUnified } from "jest-diff"; + +expect.extend({ + toContainWithDiff(target, source) { + let pass = true; + try { + expect(target).toContain(source); + } catch (e) { + pass = false; + } + + return { + pass, + message: pass ? () => "Pass" : () => diffStringsUnified(source, target), + }; + }, +}); export const getNormalizedPlain = ( testCasePath: string @@ -106,7 +122,10 @@ export const createDescribeHtmlTestCases = ( // expect(receivedHtml.length).toBeGreaterThan(0); // expect(sentHtml.length).toBeGreaterThan(0); - expect(receivedHtml).toContain(sentHtml); + + // eslint-disable-next-line + // @ts-ignore + expect(receivedHtml).toContainWithDiff(sentHtml); }); }; @@ -136,9 +155,10 @@ export const createDescribePlainTestCases = (testsPath: string) => ( // expect(sentPlain.length).toBeGreaterThan(0); // expect(receivedPlain.length).toBeGreaterThan(0); - expect(receivedPlain).toContain(sentPlain); - // const diff = diffStringsUnified(sentPlain, receivedPlain); - // console.log(diff); + + // eslint-disable-next-line + // @ts-ignore + expect(receivedPlain).toContainWithDiff(sentPlain); }); }; diff --git a/src/HTMLNormalizer/strategies/gmail.ts b/src/HTMLNormalizer/strategies/gmail.ts index fa82935..55f4f9f 100644 --- a/src/HTMLNormalizer/strategies/gmail.ts +++ b/src/HTMLNormalizer/strategies/gmail.ts @@ -3,16 +3,54 @@ import { cloneAnchorFromPane, pruneElement, } from "./common"; +import { ELEMENT_NODE } from "../../constants"; export const pruneGmailElement = (element: HTMLElement): boolean => { return pruneElement(element); }; +const qrCodeContainerIds = { vereignWrapperLink: 1 }; +const removeQrCodeNodes = (document: HTMLDocument) => { + const remove = (node: Element) => { + let toRemove = []; + + let child = node.firstChild; + + while (child) { + if (child.nodeType == ELEMENT_NODE) { + toRemove = [...toRemove, ...remove(child as Element)]; + + const childElement = child as Element; + 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) + ); +}; + export const amendGmailNodes = (document: HTMLDocument): void => { // 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( diff --git a/src/HTMLNormalizer/strategies/outlook.ts b/src/HTMLNormalizer/strategies/outlook.ts index 2f47a2b..21d9eda 100644 --- a/src/HTMLNormalizer/strategies/outlook.ts +++ b/src/HTMLNormalizer/strategies/outlook.ts @@ -28,7 +28,9 @@ export const pruneOutlookElement = (element: HTMLElement): boolean => { return !!element.nodeName.toLowerCase().startsWith("o:"); }; -const qrCodeContainerId = "test-for-us"; +const qrCodeContainerIds = { + "test-for-us": 1, +}; const removeQrCodeNodes = (document: HTMLDocument) => { const remove = (node: Element) => { let toRemove = []; @@ -41,7 +43,12 @@ const removeQrCodeNodes = (document: HTMLDocument) => { const childElement = child as Element; const id = childElement.getAttribute("id"); - if (id && id.includes(qrCodeContainerId)) { + if ( + id && + Object.keys(qrCodeContainerIds).find((possibleId) => + id.includes(possibleId) + ) + ) { toRemove.push(childElement.parentNode); } } diff --git a/src/PlainNormalizer/PlainNormalizer.ts b/src/PlainNormalizer/PlainNormalizer.ts index 16a59c9..2dd46a7 100644 --- a/src/PlainNormalizer/PlainNormalizer.ts +++ b/src/PlainNormalizer/PlainNormalizer.ts @@ -8,8 +8,8 @@ export const normalizePlainPart = (text: string): string => { const removeQRCodes = (s: string): string => { 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: string): string => { -- GitLab