Debian Codegraph @

addons/html_editor/static/src/main/link/utils.js

raw
1 /* eslint-disable */
2
3 const tldWhitelist = [
4 "com", "net", "org", "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an",
5 "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd",
6 "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "br", "bq",
7 "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch",
8 "ci", "ck", "cl", "cm", "cn", "co", "cr", "cs", "cu", "cv", "cw", "cx",
9 "cy", "cz", "dd", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg",
10 "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga",
11 "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
12 "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu",
13 "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm",
14 "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky",
15 "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly",
16 "ma", "mc", "md", "me", "mf", "mg", "mh", "mk", "ml", "mm", "mn", "mo",
17 "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
18 "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om",
19 "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt",
20 "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd",
21 "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss",
22 "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj",
23 "tk", "tl", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz", "ua",
24 "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn",
25 "vu", "wf", "ws", "ye", "yt", "yu", "za", "zm", "zr", "zw", "co\\.uk"];
26
27 const urlRegexBase = `|(?:www.))[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z][a-zA-Z0-9]{1,62}|(?:[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.(?:${tldWhitelist.join(
28 "|"
29 )})\\b))(?:(?:[/?#])[^\\s]*[^!.,})\\]'"\\s]|(?:[^!(){}.,[\\]'"\\s]+))?`;
30 const httpCapturedRegex = `(https?:\\/\\/)`;
31
32 export const URL_REGEX = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, "i");
33 export const EMAIL_REGEX = /^(mailto:)?[\w-.]+@(?:[\w-]+\.)+[\w-]{2,4}$/i;
34 export const PHONE_REGEX = /^(tel:(?:\/\/)?)?\+?[\d\s.\-()/]{3,25}$/;
35
36 export function cleanZWChars(text) {
37 return text.replace(/\u200B|\uFEFF/g, "");
38 }
39
40 /**
41 * Returns a complete URL if text is a valid email address, http URL or telephone
42 * number, null otherwise.
43 * The optional link parameter is used to prevent protocol switching between
44 * 'http' and 'https'.
45 *
46 * @param {String} text
47 * @param {HTMLAnchorElement} [link]
48 * @returns {String|null}
49 */
50 export function deduceURLfromText(text, link) {
51 const label = cleanZWChars(text).trim();
52 // Check first for e-mail.
53 let match = label.match(EMAIL_REGEX);
54 if (match) {
55 return match[1] ? match[0] : "mailto:" + match[0];
56 }
57 // Check for http link.
58 match = label.match(URL_REGEX);
59 if (match && match[0] === label) {
60 const currentHttpProtocol = (link?.href.match(/^http(s)?:\/\//gi) || [])[0];
61 if (match[2]) {
62 return match[0];
63 } else if (currentHttpProtocol) {
64 // Avoid converting a http link to https.
65 return currentHttpProtocol + match[0];
66 } else {
67 return "https://" + match[0];
68 }
69 }
70 // Check for telephone url.
71 match = label.match(PHONE_REGEX);
72 if (match) {
73 return (match[1] ? match[0] : "tel:" + match[0]).replace(/\s+/g, "");
74 }
75 return null;
76 }
77