Skip to content
Snippets Groups Projects
Commit 2e982391 authored by Igor Markin's avatar Igor Markin
Browse files

Add build

parent 182b2184
No related branches found
No related tags found
1 merge request!23Fix outlook validation error
......@@ -18,12 +18,13 @@ const index_1 = require("./index");
const index_2 = require("../../index");
const VerificationError_1 = require("../VerificationService/VerificationError");
const stringUtils_1 = require("../../utils/stringUtils");
const ELEMENT_NODE = 1;
const TEXT_NODE = 3;
const DOCUMENT_NODE = 9;
const vendorProcessingFunctions = {
[StatusesService_1.EMAIL_VENDORS.GMAIL]: normalizationStrategies_1.processGmailElement,
[StatusesService_1.EMAIL_VENDORS.OUTLOOK]: normalizationStrategies_1.processOutlookElement,
const utils_1 = require("./utils");
const vendorPruningFunctions = {
[StatusesService_1.EMAIL_VENDORS.GMAIL]: normalizationStrategies_1.pruneGmailElement,
[StatusesService_1.EMAIL_VENDORS.OUTLOOK]: normalizationStrategies_1.pruneOutlookElement,
};
const vendorPrintingFunctions = {
[StatusesService_1.EMAIL_VENDORS.OUTLOOK]: utils_1.printOutlookElement,
};
// Load JSDOM dynamically for Node env only, because built CRA is crashing with it
let JSDOM;
......@@ -97,100 +98,17 @@ const normalizeVendorHtml = (htmlString, vendor) => {
document = window.document;
}
const mimeBody = document.firstChild.lastChild;
const elementProcessingFunction = vendorProcessingFunctions[vendor];
if (!elementProcessingFunction) {
const elementPruningFunction = vendorPruningFunctions[vendor];
if (!elementPruningFunction) {
throw new Error(`Vendor "${vendor}" is not supported. Please, develop a normalization strategy for it.`);
}
pruneHtmlNode(document, elementProcessingFunction);
return printHtmlChildren(mimeBody, 0);
utils_1.pruneHtmlNode(document, elementPruningFunction);
const vendorPrintFunction = vendorPrintingFunctions[vendor];
return utils_1.printHtmlChildren(mimeBody, vendorPrintFunction, 0);
};
const normalizePlainPart = (text) => {
return stringUtils_1.removeSpacesAndLinebreaks(text);
};
const printHtmlChildren = (node, depth) => {
let child = node.firstChild;
if (!child) {
return "";
}
if (child == node.lastChild && child.nodeType == TEXT_NODE) {
return printHtmlNode(child, depth);
}
else {
let result = "";
while (child) {
result = result.concat(printHtmlNode(child, depth));
child = child.nextSibling;
}
return result;
}
};
const printHtmlNode = (node, depth) => {
let result = "";
switch (node.nodeType) {
case TEXT_NODE:
result += "<TEXT>";
result += stringUtils_1.removeSpacesAndLinebreaks(node.textContent);
result += "</TEXT>";
result += "\n";
break;
case DOCUMENT_NODE:
result += printHtmlChildren(node, depth);
break;
case ELEMENT_NODE:
result += "<" + node.nodeName;
Array.from(node.attributes)
.sort((a, b) => a.name.localeCompare(b.name))
.forEach((attribute) => {
result += ` ${attribute.name}`;
if (attribute.value) {
result += `="${common_1.escapeHtmlString(attribute.value)}"`;
}
});
if (node.firstChild) {
result += ">";
result += "\n";
result += printHtmlChildren(node, depth + 1);
result += "</" + node.nodeName + ">";
}
else {
result += "/>";
}
result += "\n";
}
return result;
};
const pruneHtmlNode = (node, pruneElement) => {
let toBeRemoved = false;
switch (node.nodeType) {
case node.COMMENT_NODE:
case node.DOCUMENT_TYPE_NODE:
toBeRemoved = true;
break;
case node.TEXT_NODE: {
const trimmedText = node.textContent.trim();
if (trimmedText === "") {
toBeRemoved = true;
}
else {
node.textContent = trimmedText;
}
break;
}
case node.ELEMENT_NODE:
toBeRemoved = pruneElement(node);
}
if (toBeRemoved) {
return true;
}
const childrenToRemove = [];
let child = node.firstChild;
while (child) {
pruneHtmlNode(child, pruneElement) && childrenToRemove.push(child);
child = child.nextSibling;
}
childrenToRemove.forEach((child) => node.removeChild(child));
return false;
};
exports.default = {
normalizeVendorHtml,
getMimeHtmlAndPlainParts,
......
export declare const processGmailElement: (element: HTMLElement) => boolean;
export declare const pruneGmailElement: (element: HTMLElement) => boolean;
/**
* Returns true if element should be completely removed
* @param element
*/
export declare const processOutlookElement: (element: HTMLElement) => boolean;
export declare const pruneOutlookElement: (element: HTMLElement) => boolean;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processOutlookElement = exports.processGmailElement = void 0;
exports.pruneOutlookElement = exports.pruneGmailElement = void 0;
const DUMMY_QR_CODE_ID = "dummyQrCode";
const deleteAttributesWithPrefix = (element, prefix) => {
for (const attribute of element.attributes) {
......@@ -17,7 +17,7 @@ const isDummyQrCode = (element) => {
return true;
}
};
exports.processGmailElement = (element) => {
exports.pruneGmailElement = (element) => {
if (isDummyQrCode(element)) {
return true;
}
......@@ -35,10 +35,14 @@ exports.processGmailElement = (element) => {
* Returns true if element should be completely removed
* @param element
*/
exports.processOutlookElement = (element) => {
exports.pruneOutlookElement = (element) => {
if (isDummyQrCode(element)) {
return true;
}
// Remove Outlook generic <o:p> tags
if (element.nodeName.toLowerCase().startsWith("o:")) {
return true;
}
if (element.attributes.length > 0) {
deleteDataAttributes(element);
// TODO Prepare for demo. fix later. the value at style attribute is changed (additional spaces and semicolon)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment