Skip to content

Commit

Permalink
auto-prefix mailto: to email addresses when linkValidation is enabled…
Browse files Browse the repository at this point in the history
… on the anchor extension fixes #1312
  • Loading branch information
neverthemachineforever committed Nov 17, 2017
1 parent df53c61 commit 04798f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ describe('Anchor Button TestCase', function () {
expect(link.href).toBe('tel:347-999-9999');
});

it('should add mailto: if need be and linkValidation option is set to true', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
linkValidation: true
}
}),
link,
anchorExtension = editor.getExtensionByName('anchor');

selectElementContentsAndFire(editor.elements[0]);
anchorExtension.showForm('test@example.com');
fireEvent(anchorExtension.getForm().querySelector('a.medium-editor-toolbar-save'), 'click');

link = editor.elements[0].querySelector('a');
expect(link).not.toBeNull();
expect(link.href).toBe('mailto:test@example.com');
});

it('should not change protocol when a valid one is included', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
Expand Down
5 changes: 5 additions & 0 deletions src/js/extensions/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,18 @@
scheme = '',
// telRegex is a regex for checking if the string is a telephone number
telRegex = /^\+?\s?\(?(?:\d\s?\-?\)?){3,20}$/,
// emailRegex for checking if the string is an email address
// https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript
emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
urlParts = value.match(/^(.*?)(?:\?(.*?))?(?:#(.*))?$/),
path = urlParts[1],
query = urlParts[2],
fragment = urlParts[3];

if (telRegex.test(value)) {
return 'tel:' + value;
} else if (emailRegex.test(value)) {
return 'mailto:' + value;
}

if (!hasScheme) {
Expand Down

0 comments on commit 04798f3

Please sign in to comment.