0
Fork 0
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:
Neil Jenkins 2014-07-03 14:16:20 +01:00
parent bbf765bee7
commit 3b95f7b864
3 changed files with 40 additions and 58 deletions

View file

@ -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;
if ( l > 1 ) {
parent = node.parentNode; parent = node.parentNode;
next = node.nextSibling; while ( match = linkRegExp.exec( data ) ) {
for ( i = 0; i < l; i += 1 ) { index = match.index;
text = parts[i]; endIndex = index + match[0].length;
if ( i ) { if ( index ) {
if ( i % 2 ) { child = doc.createTextNode( data.slice( 0, index ) );
node = doc.createElement( 'A' ); parent.insertBefore( child, node );
node.textContent = text;
node.href = /@/.test( text ) ? 'mailto:' + text :
/^(?:ht|f)tps?:/.test( text ) ?
text : 'http://' + text;
} else {
node = doc.createTextNode( text );
} }
if ( next ) { child = doc.createElement( 'A' );
parent.insertBefore( node, next ); child.textContent = data.slice( index, endIndex );
} else { child.href = match[1] ?
parent.appendChild( node ); /^(?:ht|f)tps?:/.test( match[1] ) ?
} match[1] :
} else { 'http://' + match[1] :
node.data = text; 'mailto:' + match[2];
} parent.insertBefore( child, node );
} node.data = data = data.slice( endIndex );
walker.currentNode = node;
} }
} }
}; };

File diff suppressed because one or more lines are too long

View file

@ -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;
if ( l > 1 ) {
parent = node.parentNode; parent = node.parentNode;
next = node.nextSibling; while ( match = linkRegExp.exec( data ) ) {
for ( i = 0; i < l; i += 1 ) { index = match.index;
text = parts[i]; endIndex = index + match[0].length;
if ( i ) { if ( index ) {
if ( i % 2 ) { child = doc.createTextNode( data.slice( 0, index ) );
node = doc.createElement( 'A' ); parent.insertBefore( child, node );
node.textContent = text;
node.href = /@/.test( text ) ? 'mailto:' + text :
/^(?:ht|f)tps?:/.test( text ) ?
text : 'http://' + text;
} else {
node = doc.createTextNode( text );
} }
if ( next ) { child = doc.createElement( 'A' );
parent.insertBefore( node, next ); child.textContent = data.slice( index, endIndex );
} else { child.href = match[1] ?
parent.appendChild( node ); /^(?:ht|f)tps?:/.test( match[1] ) ?
} match[1] :
} else { 'http://' + match[1] :
node.data = text; 'mailto:' + match[2];
} parent.insertBefore( child, node );
} node.data = data = data.slice( endIndex );
walker.currentNode = node;
} }
} }
}; };