Skip to content

Commit

Permalink
Automatically add tel to phone number when linking url (#64865)
Browse files Browse the repository at this point in the history
Co-authored-by: devansh016 <devansh2002@git.wordpress.org>
Co-authored-by: jeryj <jeryj@git.wordpress.org>
Co-authored-by: ajlende <ajlende@git.wordpress.org>
  • Loading branch information
4 people authored Sep 5, 2024
1 parent d2db0f9 commit 57c59d1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
insert,
create,
} from '@wordpress/rich-text';
import { isURL, isEmail } from '@wordpress/url';
import { isURL, isEmail, isPhoneNumber } from '@wordpress/url';
import {
RichTextToolbarButton,
RichTextShortcut,
Expand Down Expand Up @@ -104,6 +104,13 @@ function Edit( {
attributes: { url: `mailto:${ text }` },
} )
);
} else if ( ! isActive && text && isPhoneNumber( text ) ) {
onChange(
applyFormat( value, {
type: name,
attributes: { url: `tel:${ text.replace( /\D/g, '' ) }` },
} )
);
} else {
if ( target ) {
setOpenedBy( {
Expand Down
18 changes: 18 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ _Returns_

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

### isPhoneNumber

Determines whether the given string looks like a phone number.

_Usage_

```js
const isPhoneNumber = isPhoneNumber( '+1 (555) 123-4567' ); // true
```

_Parameters_

- _phoneNumber_ `string`: The string to scrutinize.

_Returns_

- `boolean`: Whether or not it looks like a phone number.

### isURL

Determines whether the given string looks like a URL.
Expand Down
1 change: 1 addition & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { isURL } from './is-url';
export { isEmail } from './is-email';
export { isPhoneNumber } from './is-phone-number';
export { getProtocol } from './get-protocol';
export { isValidProtocol } from './is-valid-protocol';
export { getAuthority } from './get-authority';
Expand Down
19 changes: 19 additions & 0 deletions packages/url/src/is-phone-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const PHONE_REGEXP = /^(tel:)?(\+)?\d{6,15}$/;

/**
* Determines whether the given string looks like a phone number.
*
* @param {string} phoneNumber The string to scrutinize.
*
* @example
* ```js
* const isPhoneNumber = isPhoneNumber('+1 (555) 123-4567'); // true
* ```
*
* @return {boolean} Whether or not it looks like a phone number.
*/
export function isPhoneNumber( phoneNumber ) {
// Remove any seperator from phone number.
phoneNumber = phoneNumber.replace( /[-.() ]/g, '' );
return PHONE_REGEXP.test( phoneNumber );
}
47 changes: 47 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
hasQueryArg,
isEmail,
isURL,
isPhoneNumber,
isValidAuthority,
isValidFragment,
isValidPath,
Expand Down Expand Up @@ -73,6 +74,52 @@ describe( 'isEmail', () => {
);
} );

describe( 'isPhoneNumber', () => {
it.each( [
'+1 (555) 123-4567',
'(555) 123-4567',
'555-123-4567',
'5551234567',
'+91 987 654 3210',
'123-456-7890',
'(123) 456-7890',
'123 456 7890',
'123.456.7890',
'+1 123 456 7890',
'1234567890',
'+44 791 112 3456',
'(123) 4567',
'+1 (123) 45678901',
'12-34-56',
'123456789012345',
'+12 3456789012345',
'tel:+1-123-456-7890',
] )(
'returns true when given things that look like a phone number: %s',
( phoneNumber ) => {
expect( isPhoneNumber( phoneNumber ) ).toBe( true );
}
);

it.each( [
'not a phone number',
'123',
'1234',
'12345',
'+91 123',
'abc-def-ghij',
'a123456789b',
'12-34-5',
'tel:911',
'tel:12345',
] )(
"returns false when given things that don't look like a phone number: %s",
( phoneNumber ) => {
expect( isPhoneNumber( phoneNumber ) ).toBe( false );
}
);
} );

describe( 'getProtocol', () => {
it( 'returns the protocol part of a URL', () => {
expect(
Expand Down

1 comment on commit 57c59d1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 57c59d1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10725402925
📝 Reported issues:

Please sign in to comment.