Skip to content

Commit

Permalink
Don't show protocol on suggestions in navigation (#20350)
Browse files Browse the repository at this point in the history
* Add test.

* Remove protocol.
  • Loading branch information
sainthkh authored May 18, 2020
1 parent 4fd5ba0 commit 31bb9a6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { safeDecodeURI } from '@wordpress/url';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import { Button, TextHighlight } from '@wordpress/components';
import { Icon, globe } from '@wordpress/icons';
Expand Down Expand Up @@ -46,7 +46,11 @@ export const LinkControlSearchItem = ( {
aria-hidden={ ! isURL }
className="block-editor-link-control__search-item-info"
>
{ ! isURL && ( safeDecodeURI( suggestion.url ) || '' ) }
{ ! isURL &&
( filterURLForDisplay(
safeDecodeURI( suggestion.url )
) ||
'' ) }
{ isURL && __( 'Press ENTER to add this link' ) }
</span>
</span>
Expand Down
52 changes: 51 additions & 1 deletion packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { first, last, nth } from 'lodash';
import { first, last, nth, uniqueId } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -85,6 +85,56 @@ describe( 'Basic rendering', () => {
expect( container.innerHTML ).toMatchSnapshot();
} );

it( 'should not render protocol in links', async () => {
mockFetchSearchSuggestions.mockImplementation( () =>
Promise.resolve( [
{
id: uniqueId(),
title: 'Hello Page',
type: 'Page',
info: '2 days ago',
url: `http://example.com/?p=${ uniqueId() }`,
},
{
id: uniqueId(),
title: 'Hello Post',
type: 'Post',
info: '19 days ago',
url: `https://example.com/${ uniqueId() }`,
},
] )
);

const searchTerm = 'Hello';

act( () => {
render( <LinkControl />, container );
} );

// Search Input UI
const searchInput = getURLInput();

// Simulate searching for a term
act( () => {
Simulate.change( searchInput, { target: { value: searchTerm } } );
} );

// fetchFauxEntitySuggestions resolves on next "tick" of event loop
await eventLoopTick();

// Find all elements with link
// Filter out the element with the text 'ENTER' because it doesn't contain link
const linkElements = Array.from(
container.querySelectorAll(
'.block-editor-link-control__search-item-info'
)
).filter( ( elem ) => ! elem.innerHTML.includes( 'ENTER' ) );

linkElements.forEach( ( elem ) => {
expect( elem.innerHTML ).not.toContain( '://' );
} );
} );

describe( 'forceIsEditingLink', () => {
const isEditing = () => !! getURLInput();

Expand Down

0 comments on commit 31bb9a6

Please sign in to comment.