Skip to content

Commit 04798f3

Browse files
auto-prefix mailto: to email addresses when linkValidation is enabled on the anchor extension fixes #1312
1 parent df53c61 commit 04798f3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spec/anchor.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ describe('Anchor Button TestCase', function () {
240240
expect(link.href).toBe('tel:347-999-9999');
241241
});
242242

243+
it('should add mailto: if need be and linkValidation option is set to true', function () {
244+
var editor = this.newMediumEditor('.editor', {
245+
anchor: {
246+
linkValidation: true
247+
}
248+
}),
249+
link,
250+
anchorExtension = editor.getExtensionByName('anchor');
251+
252+
selectElementContentsAndFire(editor.elements[0]);
253+
anchorExtension.showForm('test@example.com');
254+
fireEvent(anchorExtension.getForm().querySelector('a.medium-editor-toolbar-save'), 'click');
255+
256+
link = editor.elements[0].querySelector('a');
257+
expect(link).not.toBeNull();
258+
expect(link.href).toBe('mailto:test@example.com');
259+
});
260+
243261
it('should not change protocol when a valid one is included', function () {
244262
var editor = this.newMediumEditor('.editor', {
245263
anchor: {

src/js/extensions/anchor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,18 @@
261261
scheme = '',
262262
// telRegex is a regex for checking if the string is a telephone number
263263
telRegex = /^\+?\s?\(?(?:\d\s?\-?\)?){3,20}$/,
264+
// emailRegex for checking if the string is an email address
265+
// https://stackoverflow.com/questions/46155/how-to-validate-email-address-in-javascript
266+
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,}))$/,
264267
urlParts = value.match(/^(.*?)(?:\?(.*?))?(?:#(.*))?$/),
265268
path = urlParts[1],
266269
query = urlParts[2],
267270
fragment = urlParts[3];
268271

269272
if (telRegex.test(value)) {
270273
return 'tel:' + value;
274+
} else if (emailRegex.test(value)) {
275+
return 'mailto:' + value;
271276
}
272277

273278
if (!hasScheme) {

0 commit comments

Comments
 (0)