Fix Error Invalid Group Specifier Name on Safari by removing REGEX lookbehind group construct (fixes #239)

This commit is contained in:
schlagmichdoch 2024-01-12 01:23:14 +01:00
parent 041261be2a
commit 2e15a018da
2 changed files with 19 additions and 3 deletions

View File

@ -2019,8 +2019,8 @@ class ReceiveTextDialog extends Dialog {
if (text.length < 2000) {
// replace URLs with actual links
this.$text.innerHTML = this.$text.innerHTML
.replace(/(^|(?<=(<br>|\s)))(https?:\/\/|www.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%]){2,}\.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%.]){2,})/g,
(url) => {
.replace(/(^|<br>|\s|")((https?:\/\/|www.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%]){2,}\.)(([a-z]|[A-Z]|[0-9]|[\-_~:\/?#\[\]@!$&'()*+,;=%.]){2,}))/g,
(match, whitespace, url) => {
let link = url;
// prefix www.example.com with http protocol to prevent it from being a relative link
@ -2028,7 +2028,13 @@ class ReceiveTextDialog extends Dialog {
link = "http://" + link
}
return `<a href="${link}" target="_blank">${url}</a>`;
// Check if link is valid
if (isUrlValid(link)) {
return `${whitespace}<a href="${link}" target="_blank">${url}</a>`;
}
else {
return match;
}
});
}

View File

@ -583,4 +583,14 @@ async function decodeBase64Text(base64) {
if (!base64) throw new Error('Base64 is empty');
return decodeURIComponent(escape(window.atob(base64)))
}
function isUrlValid(url) {
try {
let urlObj = new URL(url);
return true;
}
catch (e) {
return false;
}
}