Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-prefix mailto: for email addresses with linkValidation in anchor extension. #1389

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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