mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
Better parsing of links and emails into links.
* Be more liberal in tld, as there are now a billion new ones, of every length. * Fix bug where if the URL contains an @ it would add it as a mailto link instead.
This commit is contained in:
parent
bbf765bee7
commit
3b95f7b864
3 changed files with 40 additions and 58 deletions
|
@ -2205,7 +2205,7 @@ var decreaseListLevel = function ( frag ) {
|
||||||
|
|
||||||
// --- Clean ---
|
// --- Clean ---
|
||||||
|
|
||||||
var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:[\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,4}))/i;
|
var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i;
|
||||||
|
|
||||||
var addLinks = function ( frag ) {
|
var addLinks = function ( frag ) {
|
||||||
var doc = frag.ownerDocument,
|
var doc = frag.ownerDocument,
|
||||||
|
@ -2213,35 +2213,26 @@ var addLinks = function ( frag ) {
|
||||||
function ( node ) {
|
function ( node ) {
|
||||||
return getNearest( node, 'A' ) ? FILTER_SKIP : FILTER_ACCEPT;
|
return getNearest( node, 'A' ) ? FILTER_SKIP : FILTER_ACCEPT;
|
||||||
}, false ),
|
}, false ),
|
||||||
node, parts, i, l, text, parent, next;
|
node, data, parent, match, index, endIndex, child;
|
||||||
while ( node = walker.nextNode() ) {
|
while ( node = walker.nextNode() ) {
|
||||||
parts = node.data.split( linkRegExp );
|
data = node.data;
|
||||||
l = parts.length;
|
parent = node.parentNode;
|
||||||
if ( l > 1 ) {
|
while ( match = linkRegExp.exec( data ) ) {
|
||||||
parent = node.parentNode;
|
index = match.index;
|
||||||
next = node.nextSibling;
|
endIndex = index + match[0].length;
|
||||||
for ( i = 0; i < l; i += 1 ) {
|
if ( index ) {
|
||||||
text = parts[i];
|
child = doc.createTextNode( data.slice( 0, index ) );
|
||||||
if ( i ) {
|
parent.insertBefore( child, node );
|
||||||
if ( i % 2 ) {
|
|
||||||
node = doc.createElement( 'A' );
|
|
||||||
node.textContent = text;
|
|
||||||
node.href = /@/.test( text ) ? 'mailto:' + text :
|
|
||||||
/^(?:ht|f)tps?:/.test( text ) ?
|
|
||||||
text : 'http://' + text;
|
|
||||||
} else {
|
|
||||||
node = doc.createTextNode( text );
|
|
||||||
}
|
|
||||||
if ( next ) {
|
|
||||||
parent.insertBefore( node, next );
|
|
||||||
} else {
|
|
||||||
parent.appendChild( node );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
node.data = text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
walker.currentNode = node;
|
child = doc.createElement( 'A' );
|
||||||
|
child.textContent = data.slice( index, endIndex );
|
||||||
|
child.href = match[1] ?
|
||||||
|
/^(?:ht|f)tps?:/.test( match[1] ) ?
|
||||||
|
match[1] :
|
||||||
|
'http://' + match[1] :
|
||||||
|
'mailto:' + match[2];
|
||||||
|
parent.insertBefore( child, node );
|
||||||
|
node.data = data = data.slice( endIndex );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1092,7 +1092,7 @@ var decreaseListLevel = function ( frag ) {
|
||||||
|
|
||||||
// --- Clean ---
|
// --- Clean ---
|
||||||
|
|
||||||
var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])|(?:[\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,4}))/i;
|
var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i;
|
||||||
|
|
||||||
var addLinks = function ( frag ) {
|
var addLinks = function ( frag ) {
|
||||||
var doc = frag.ownerDocument,
|
var doc = frag.ownerDocument,
|
||||||
|
@ -1100,35 +1100,26 @@ var addLinks = function ( frag ) {
|
||||||
function ( node ) {
|
function ( node ) {
|
||||||
return getNearest( node, 'A' ) ? FILTER_SKIP : FILTER_ACCEPT;
|
return getNearest( node, 'A' ) ? FILTER_SKIP : FILTER_ACCEPT;
|
||||||
}, false ),
|
}, false ),
|
||||||
node, parts, i, l, text, parent, next;
|
node, data, parent, match, index, endIndex, child;
|
||||||
while ( node = walker.nextNode() ) {
|
while ( node = walker.nextNode() ) {
|
||||||
parts = node.data.split( linkRegExp );
|
data = node.data;
|
||||||
l = parts.length;
|
parent = node.parentNode;
|
||||||
if ( l > 1 ) {
|
while ( match = linkRegExp.exec( data ) ) {
|
||||||
parent = node.parentNode;
|
index = match.index;
|
||||||
next = node.nextSibling;
|
endIndex = index + match[0].length;
|
||||||
for ( i = 0; i < l; i += 1 ) {
|
if ( index ) {
|
||||||
text = parts[i];
|
child = doc.createTextNode( data.slice( 0, index ) );
|
||||||
if ( i ) {
|
parent.insertBefore( child, node );
|
||||||
if ( i % 2 ) {
|
|
||||||
node = doc.createElement( 'A' );
|
|
||||||
node.textContent = text;
|
|
||||||
node.href = /@/.test( text ) ? 'mailto:' + text :
|
|
||||||
/^(?:ht|f)tps?:/.test( text ) ?
|
|
||||||
text : 'http://' + text;
|
|
||||||
} else {
|
|
||||||
node = doc.createTextNode( text );
|
|
||||||
}
|
|
||||||
if ( next ) {
|
|
||||||
parent.insertBefore( node, next );
|
|
||||||
} else {
|
|
||||||
parent.appendChild( node );
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
node.data = text;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
walker.currentNode = node;
|
child = doc.createElement( 'A' );
|
||||||
|
child.textContent = data.slice( index, endIndex );
|
||||||
|
child.href = match[1] ?
|
||||||
|
/^(?:ht|f)tps?:/.test( match[1] ) ?
|
||||||
|
match[1] :
|
||||||
|
'http://' + match[1] :
|
||||||
|
'mailto:' + match[2];
|
||||||
|
parent.insertBefore( child, node );
|
||||||
|
node.data = data = data.slice( endIndex );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue