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

Automatically add mailto: to email addresses when linking #14857

Merged
Merged
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
4 changes: 3 additions & 1 deletion packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
removeFormat,
slice,
} from '@wordpress/rich-text';
import { isURL } from '@wordpress/url';
import { isURL, isEmail } from '@wordpress/url';
import { RichTextToolbarButton, RichTextShortcut } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -47,6 +47,8 @@ export const link = {

if ( text && isURL( text ) ) {
onChange( applyFormat( value, { type: name, attributes: { url: text } } ) );
} else if ( text && isEmail( text ) ) {
onChange( applyFormat( value, { type: name, attributes: { url: `mailto:${ text }` } } ) );
} else {
this.setState( { addingLink: true } );
}
Expand Down
18 changes: 18 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ _Returns_

- `boolean`: Whether or not the URL contains the query arg.

<a name="isEmail" href="#isEmail">#</a> **isEmail**

Determines whether the given string looks like an email.

_Usage_

```js
const isEmail = isEmail( 'hello@wordpress.org' ); // true
```

_Parameters_

- _email_ `string`: The string to scrutinise.

_Returns_

- `boolean`: Whether or not it looks like an email.

<a name="isURL" href="#isURL">#</a> **isURL**

Determines whether the given string looks like a URL.
Expand Down
16 changes: 16 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export function isURL( url ) {
return URL_REGEXP.test( url );
}

/**
* Determines whether the given string looks like an email.
*
* @param {string} email The string to scrutinise.
*
* @example
* ```js
* const isEmail = isEmail( 'hello@wordpress.org' ); // true
* ```
*
* @return {boolean} Whether or not it looks like an email.
*/
export function isEmail( email ) {
return EMAIL_REGEXP.test( email );
}

/**
* Returns the protocol part of the URL.
*
Expand Down
33 changes: 33 additions & 0 deletions packages/url/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { every } from 'lodash';
*/
import {
isURL,
isEmail,
getProtocol,
isValidProtocol,
getAuthority,
Expand Down Expand Up @@ -53,6 +54,38 @@ describe( 'isURL', () => {
} );
} );

describe( 'isEmail', () => {
it( 'returns true when given things that look like an email', () => {
const emails = [
'simple@wordpress.org',
'very.common@wordpress.org',
'disposable.style.email.with+symbol@wordpress.org',
'other.email-with-hyphen@wordpress.org',
'fully-qualified-domain@wordpress.org',
'user.name+tag+sorting@wordpress.org',
'x@wordpress.org',
'wordpress-indeed@strange-wordpress.org',
'wordpress@s.wordpress',
];

expect( every( emails, isEmail ) ).toBe( true );
} );

it( 'returns false when given things that don\'t look like an email', () => {
const emails = [
'Abc.wordpress.org',
'A@b@c@wordpress.org',
'a"b(c)d,e:f;g<h>i[j\k]l@wordpress.org',
'just"not"right@wordpress.org',
'this is"not\allowed@wordpress.org',
'this\ still\"not\\allowed@wordpress.org',
'1234567890123456789012345678901234567890123456789012345678901234+x@wordpress.org',
];

expect( every( emails, isEmail ) ).toBe( false );
} );
} );

describe( 'getProtocol', () => {
it( 'returns the protocol part of a URL', () => {
expect( getProtocol( 'http://user:password@www.test-this.com:1020/test-path/file.extension#anchor?query=params&more' ) ).toBe( 'http:' );
Expand Down