All files / Scripts Strings.ts

45% Statements 18/40
75% Branches 12/16
50% Functions 5/10
43.24% Lines 16/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 13211x   11x                                                               11x                                                                                                                           220x 220x 220x 220x       226x 226x 13108x 220x       226x 220x     6x                         16x 30x      
import { diagnostics } from "./Diag";
 
export class Strings {
    public static copyToClipboard(str: string): void {
        const textArea: HTMLTextAreaElement = document.createElement("textarea");
        textArea.style.position = "absolute";
        textArea.style.opacity = "0";
        textArea.value = str;
        document.body.appendChild(textArea);
        textArea.select();
        const succeeded: boolean = document.execCommand("copy");
        document.body.removeChild(textArea);
        diagnostics.trackEvent({ name: "copy", properties: { succeeded: succeeded, style: "textarea" } });
 
        if (!succeeded) {
            try {
                navigator.clipboard.writeText(str).then(function (): void {
                    diagnostics.trackEvent({ name: "copy", properties: { succeeded: "true", style: "navigator" } });
                }, function () {
                    diagnostics.trackEvent({ name: "copy", properties: { succeeded: "false", style: "navigator" } });
                });
            }
            catch { /**/ }
        }
 
        try {
            const queryOpts: PermissionDescriptor = { name: "clipboard-write" as PermissionName };
            navigator.permissions.query(queryOpts).then(function (result: PermissionStatus) {
                diagnostics.trackEvent({ name: "copy", properties: { succeeded: succeeded, style: "permissions", clipboardWrite: result.state } });
            });
        }
        catch { /**/ }
    }
 
    private static headerToURLMap: string[][] = [
        ["Accept-Language", "https://tools.ietf.org/html/rfc3282"],
        ["Archived-At", "https://tools.ietf.org/html/rfc5064"],
        ["ARC-Authentication-Results", "https://tools.ietf.org/html/rfc8617#section-4.1.1"],
        ["ARC-Message-Signature", "https://tools.ietf.org/html/rfc8617#section-4.1.2"],
        ["ARC-Seal", "https://tools.ietf.org/html/rfc8617#section-4.1.3"],
        ["Authentication-Results", "https://tools.ietf.org/html/rfc8601"],
        ["BCC", "https://tools.ietf.org/html/rfc5322#section-3.6.3"],
        ["CC", "https://tools.ietf.org/html/rfc5322#section-3.6.3"],
        ["Content-Description", "https://tools.ietf.org/html/rfc2045#section-8"],
        ["Content-Disposition", "https://tools.ietf.org/html/rfc2183"],
        ["Content-Id", "https://tools.ietf.org/html/rfc2045#section-7"],
        ["Content-Language", "https://tools.ietf.org/html/rfc3282"],
        ["Content-Transfer-Encoding", "https://tools.ietf.org/html/rfc2045#section-6"],
        ["Content-Type", "https://tools.ietf.org/html/rfc2045#section-5"],
        ["Date", "https://tools.ietf.org/html/rfc5322#section-3.6.1"],
        ["Deferred-Delivery", "https://tools.ietf.org/html/rfc4021#section-2.1.65"],
        ["DKIM-Signature", "https://tools.ietf.org/html/rfc6376"],
        ["From", "https://tools.ietf.org/html/rfc5322#section-3.6.2"],
        ["In-Reply-To", "https://tools.ietf.org/html/rfc5322#section-3.6.4"],
        ["Importance", "https://tools.ietf.org/html/rfc2156#section-5.3"],
        ["List-Help", "https://tools.ietf.org/html/rfc2369"],
        ["List-ID", "https://tools.ietf.org/html/rfc2919"],
        ["List-Subscribe", "https://tools.ietf.org/html/rfc2369"],
        ["List-Unsubscribe", "https://tools.ietf.org/html/rfc2369"],
        ["Message-ID", "https://tools.ietf.org/html/rfc5322#section-3.6.4"],
        ["MIME-Version", "https://tools.ietf.org/html/rfc2045#section-4"],
        ["Received", "https://tools.ietf.org/html/rfc5321#section-4.4"],
        ["Received-SPF", "https://tools.ietf.org/html/rfc4408#section-7"],
        ["References", "https://tools.ietf.org/html/rfc5322#section-3.6.4"],
        ["Reply-To", "https://tools.ietf.org/html/rfc5322#section-3.6.2"],
        ["Return-Path", "https://tools.ietf.org/html/rfc5322#section-3.6.7"],
        ["Sender", "https://tools.ietf.org/html/rfc5322#section-3.6.2"],
        ["Subject", "https://tools.ietf.org/html/rfc5322#section-3.6.5"],
        ["Thread-Index", "https://msdn.microsoft.com/en-us/library/ms526219"],
        ["Thread-Topic", "https://msdn.microsoft.com/en-us/library/ms526986"],
        ["To", "https://tools.ietf.org/html/rfc5322#section-3.6.3"],
        ["X-Auto-Response-Suppress", "https://msdn.microsoft.com/en-us/library/ee219609"],
        ["X-Forefront-Antispam-Report", "https://docs.microsoft.com/en-us/microsoft-365/security/office-365-security/anti-spam-message-headers"],
        ["X-Forefront-Antispam-Report-Untrusted", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-Forefront-Prvs", "https://technet.microsoft.com/en-us/library/dd639361"],
        ["X-Message-Flag", "https://msdn.microsoft.com/en-us/library/exchange/ms875195"],
        ["X-Microsoft-Antispam", "https://docs.microsoft.com/en-us/microsoft-365/security/office-365-security/anti-spam-message-headers"],
        ["X-MS-Exchange-Organization-Antispam-Report", "https://technet.microsoft.com/en-us/library/aa996878"],
        ["X-MS-Exchange-Organization-AuthAs", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-AuthMechanism", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-AuthSource", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-AutoForwarded", "https://msdn.microsoft.com/en-us/library/ee178180"],
        ["X-MS-Exchange-Organization-AVStamp-Enterprise", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-AVStamp-Mailbox", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-Network-Message-Id", "https://technet.microsoft.com/en-us/library/bb232136"],
        ["X-MS-Exchange-Organization-PCL", "https://technet.microsoft.com/en-us/library/aa996878"],
        ["X-MS-Exchange-Organization-SCL", "https://technet.microsoft.com/en-us/library/aa996878"],
        ["X-MS-Exchange-Organization-SenderIdResult", "https://technet.microsoft.com/en-us/library/aa996878"],
        ["X-MS-Has-Attach", "https://msdn.microsoft.com/en-us/library/ee178420"],
        ["X-MS-TNEF-Correlator", "https://msdn.microsoft.com/en-us/library/ee219198"],
        ["X-Originating-IP", "https://en.wikipedia.org/wiki/X-Originating-IP"],
        ["X-Priority", "https://technet.microsoft.com/en-us/library/bb691107"],
        ["SFS", "https://docs.microsoft.com/en-us/exchange/monitoring/trace-an-email-message/run-a-message-trace-and-view-results"]
    ];
 
    public static htmlEncode(value: string): string {
        Iif (!value) return "";
        const div = document.createElement("div");
        div.textContent = value;
        return div.innerHTML;
    }
 
    public static mapHeaderToURL(headerName: string, text?: string): string {
        let url = "";
        Strings.headerToURLMap.forEach((h: string[]) => {
            if (url === "" && headerName.toLowerCase() === h[0]?.toLowerCase()) {
                url = h[1] ?? "";
            }
        });
 
        if (url !== "") {
            return ["<a href = '", url, "' target = '_blank'>", this.htmlEncode(text || headerName), "</a>"].join("");
        }
 
        return "";
    }
 
    public static mapValueToURL(text: string): string {
        try {
            return ["<a href='", text, "' target='_blank'>", this.htmlEncode(text), "</a>"].join("");
        } catch {
            return text;
        }
    }
 
    // Join an array with char, dropping empty/missing entries
    public static joinArray(array: (string | number | null)[] | null, char: string): string {
        if (!array) return "";
        return (array.filter(function (item) { return item; })).join(char);
    }
}