diff --git a/spec/anchor.spec.js b/spec/anchor.spec.js index f4f6b743f..1cfb895e0 100644 --- a/spec/anchor.spec.js +++ b/spec/anchor.spec.js @@ -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: { diff --git a/src/js/extensions/anchor.js b/src/js/extensions/anchor.js index c802cd59c..08f016170 100644 --- a/src/js/extensions/anchor.js +++ b/src/js/extensions/anchor.js @@ -261,6 +261,9 @@ 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], @@ -268,6 +271,8 @@ if (telRegex.test(value)) { return 'tel:' + value; + } else if (emailRegex.test(value)) { + return 'mailto:' + value; } if (!hasScheme) {