diff --git a/source/Editor.js b/source/Editor.js index 04689bc..2920f9c 100644 --- a/source/Editor.js +++ b/source/Editor.js @@ -1783,7 +1783,7 @@ proto.insertImage = function ( src, attributes ) { return img; }; -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 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)(?:(?:\?(?:(?!&|\?)(?:\S))+=(?:(?!&|\?)(?:\S))+)(?:&(?:(?!&|\?)(?:\S))+=(?:(?!&|\?)(?:\S))+)*)?/i;; var addLinks = function ( frag, root, self ) { var doc = frag.ownerDocument, @@ -1808,7 +1808,7 @@ var addLinks = function ( frag, root, self ) { /^(?:ht|f)tps?:/.test( match[1] ) ? match[1] : 'http://' + match[1] : - 'mailto:' + match[2] + 'mailto:' + match[0] }, defaultAttributes, false )); child.textContent = data.slice( index, endIndex ); parent.insertBefore( child, node ); diff --git a/test/squire.spec.js b/test/squire.spec.js index fc6794b..a1ea44c 100644 --- a/test/squire.spec.js +++ b/test/squire.spec.js @@ -361,6 +361,65 @@ describe('Squire RTE', function () { editor.insertHTML('
text1text2
'); expect(editor.getHTML(), 'to contain', '
text1
text2
'); }); + + var LINK_MAP = { + "dewdw@fre.fr": "mailto:dewdw@fre.fr", + "dew@free.fr?dew=dew": "mailto:dew@free.fr?dew=dew", + "dew@free.fr?subject=dew": "mailto:dew@free.fr?subject=dew", + "test@example.com?subject=foo&body=bar": "mailto:test@example.com?subject=foo&body=bar", + "dew@fre.fr dewdwe @dew": "mailto:dew@fre.fr", + "http://free.fr": "http://free.fr", + "http://google.com": "http://google.com", + "https://google.com": "https://google.com", + "https://www.google.com": "https://www.google.com", + "https://www.google.com/": "https://www.google.com/", + "https://google.com/?": "https://google.com/", + "https://google.com?": "https://google.com", + "https://google.com?a": "https://google.com/?a", + "https://google.com?a=": "https://google.com/?a=", + "https://google.com?a=b": "https://google.com/?a=b", + "https://google.com?a=b?": "https://google.com/?a=b", + "https://google.com?a=b&": "https://google.com/?a=b&", + "https://google.com?a=b&c": "https://google.com/?a=b&c", + "https://google.com?a=b&c=": "https://google.com/?a=b&c=", + "https://google.com?a=b&c=d": "https://google.com/?a=b&c=", + "https://google.com?a=b&c=d?": "https://google.com/?a=b&c=d", + "https://google.com?a=b&c=d&": "https://google.com/?a=b&c=d&", + "https://google.com?a=b&c=d&e=": "https://google.com/?a=b&c=d&e=", + "https://google.com?a=b&c=d&e=f": "https://google.com/?a=b&c=d&e=f" + }; + + Object.keys(LINK_MAP).forEach((input) => { + it('should auto convert links to anchor: ' + input, function() { + editor.insertHTML(input); + var link = editor.getDocument().querySelector('a'); + expect(link.href, 'to contain', LINK_MAP[input]); + editor.setHTML(''); + }); + }); + + it('should auto convert a part of the link to an anchor', function() { + editor.insertHTML(` + dew@fre.fr dewdwe @dew + `); + var link = editor.getDocument().querySelector('a'); + expect(link.textContent, 'to be', 'dew@fre.fr'); + expect(link.href, 'to be', 'mailto:dew@fre.fr'); + editor.setHTML(''); + }); + + it('should not auto convert non links to anchor', function() { + editor.insertHTML(` + dewdwe @dew + deww.de + monique.fre + + google.com + `); + var link = editor.getDocument().querySelector('a'); + expect(link, 'to be', null); + editor.setHTML(''); + }); }); afterEach(function () {