Skip to content

Commit

Permalink
Parse url and create anchor with queryparams
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoko authored and neilj committed Jun 23, 2018
1 parent db005b3 commit 10bf787
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/Editor.js
Expand Up @@ -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,
Expand All @@ -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 );
Expand Down
59 changes: 59 additions & 0 deletions test/squire.spec.js
Expand Up @@ -361,6 +361,65 @@ describe('Squire RTE', function () {
editor.insertHTML('<table><tbody><!--StartFragment--><tr><td>text1</td><td>text2</td></tr><!--EndFragment--></tbody></table>');
expect(editor.getHTML(), 'to contain', '<table><tbody><tr><td>text1<br></td><td>text2<br></td></tr></tbody></table>');
});

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 () {
Expand Down

0 comments on commit 10bf787

Please sign in to comment.