mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 15:06:15 -04:00
Fix links in messages cut if ! in path; make regex more readable
This commit is contained in:
parent
dabfe58124
commit
e2f0ca6e45
2 changed files with 38 additions and 25 deletions
|
@ -2077,35 +2077,48 @@ class ReceiveTextDialog extends Dialog {
|
||||||
|
|
||||||
let m = 0;
|
let m = 0;
|
||||||
|
|
||||||
const allowedDomainChars = "a-zA-Z0-9áàäčçđéèêŋńñóòôöšŧüžæøåëìíîïðùúýþćěłřśţźǎǐǒǔǥǧǩǯəʒâûœÿãõāēīōūăąĉċďĕėęĝğġģĥħĩĭįıĵķĸĺļľņňŏőŕŗŝşťũŭůűųŵŷżאבגדהוזחטיךכלםמןנסעףפץצקרשתװױײ";
|
const chrs = `a-zA-Z0-9áàäčçđéèêŋńñóòôöšŧüžæøåëìíîïðùúýþćěłřśţźǎǐǒǔǥǧǩǯəʒâûœÿãõāēīōūăąĉċďĕėęĝğġģĥħĩĭįıĵķĸĺļľņňŏőŕŗŝşťũŭůűųŵŷżאבגדהוזחטיךכלםמןנסעףפץצקרשתװױײ`; // allowed chars in domain names
|
||||||
const urlRgx = new RegExp(`(^|\\n|\\s|["><\\-_~:\\/?#\\[\\]@!$&'()*+,;=%.])(((https?:\\/\\/)?(?:[${allowedDomainChars}](?:[${allowedDomainChars}-]{0,61}[${allowedDomainChars}])?\\.)+[${allowedDomainChars}][${allowedDomainChars}-]{0,61}[${allowedDomainChars}])(:?\\d*)\\/?([${allowedDomainChars}_\\/\\-#.]*)(\\?([${allowedDomainChars}\\-_~:\\/?#\\[\\]@!$&'()*+,;=%.]*))?)`, 'g');
|
const rgxWhitespace = `(^|\\n|\\s)`;
|
||||||
|
const rgxScheme = `(https?:\\/\\/)`
|
||||||
|
const rgxSchemeMail = `(mailto:)`
|
||||||
|
const rgxUserinfo = `(?:(?:[${chrs}.%]*(?::[${chrs}.%]*)?)@)`;
|
||||||
|
const rgxHost = `(?:(?:[${chrs}](?:[${chrs}-]{0,61}[${chrs}])?\\.)+[${chrs}][${chrs}-]{0,61}[${chrs}])`;
|
||||||
|
const rgxPort = `(:\\d*)`;
|
||||||
|
const rgxPath = `(?:(?:\\/[${chrs}\\-\\._~!$&'\\(\\)\\*\\+,;=:@%]*)*)`;
|
||||||
|
const rgxQueryAndFragment = `(\\?[${chrs}\\-_~:\\/#\\[\\]@!$&'\\(\\)*+,;=%.]*)`;
|
||||||
|
const rgxUrl = `(${rgxScheme}?${rgxHost}${rgxPort}?${rgxPath}${rgxQueryAndFragment}?)`;
|
||||||
|
const rgxMail = `(${rgxSchemeMail}${rgxUserinfo}${rgxHost})`;
|
||||||
|
const rgxUrlAll = new RegExp(`${rgxWhitespace}${rgxUrl}`, 'g');
|
||||||
|
const rgxMailAll = new RegExp(`${rgxWhitespace}${rgxMail}`, 'g');
|
||||||
|
|
||||||
$textShadow.innerText = text.replace(urlRgx,
|
const replaceMatchWithPlaceholder = function(match, whitespace, url, scheme) {
|
||||||
(match, whitespaceOrSpecial, url, g3, scheme) => {
|
let link = url;
|
||||||
let link = url;
|
|
||||||
|
|
||||||
// prefix www.example.com with http protocol to prevent it from being a relative link
|
// prefix www.example.com with http scheme to prevent it from being a relative link
|
||||||
if (!scheme && link.startsWith('www')) {
|
if (!scheme && link.startsWith('www')) {
|
||||||
link = "http://" + link
|
link = "http://" + link
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUrlValid(link)) {
|
if (!isUrlValid(link)) {
|
||||||
// link is valid -> replace with link node placeholder
|
|
||||||
|
|
||||||
// find linkNodePlaceholder that is not yet present in text node
|
|
||||||
m++;
|
|
||||||
while (occP.includes(`${p}${m}`)) {
|
|
||||||
m++;
|
|
||||||
}
|
|
||||||
let linkNodePlaceholder = `${p}${m}`;
|
|
||||||
|
|
||||||
// add linkNodePlaceholder to text node and save a reference to linkNodes object
|
|
||||||
linkNodes[linkNodePlaceholder] = `<a href="${link}" target="_blank" rel="noreferrer">${url}</a>`;
|
|
||||||
return `${whitespaceOrSpecial}${linkNodePlaceholder}`;
|
|
||||||
}
|
|
||||||
// link is not valid -> do not replace
|
// link is not valid -> do not replace
|
||||||
return match;
|
return match;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
// link is valid -> replace with link node placeholder
|
||||||
|
// find linkNodePlaceholder that is not yet present in text node
|
||||||
|
m++;
|
||||||
|
while (occP.includes(`${p}${m}`)) {
|
||||||
|
m++;
|
||||||
|
}
|
||||||
|
let linkNodePlaceholder = `${p}${m}`;
|
||||||
|
|
||||||
|
// add linkNodePlaceholder to text node and save a reference to linkNodes object
|
||||||
|
linkNodes[linkNodePlaceholder] = `<a href="${link}" target="_blank" rel="noreferrer">${url}</a>`;
|
||||||
|
return `${whitespace}${linkNodePlaceholder}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
text = text.replace(rgxUrlAll, replaceMatchWithPlaceholder);
|
||||||
|
$textShadow.innerText = text.replace(rgxMailAll, replaceMatchWithPlaceholder);
|
||||||
|
|
||||||
|
|
||||||
this.$text.innerHTML = $textShadow.innerHTML.replace(pRgx,
|
this.$text.innerHTML = $textShadow.innerHTML.replace(pRgx,
|
||||||
|
|
|
@ -590,7 +590,7 @@ async function decodeBase64Text(base64) {
|
||||||
|
|
||||||
function isUrlValid(url) {
|
function isUrlValid(url) {
|
||||||
try {
|
try {
|
||||||
let urlObj = new URL(url);
|
new URL(url);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue