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

Don't show protocol on suggestions in navigation #20350

Merged
merged 2 commits into from
May 18, 2020
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
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 )
) ||
'' ) }
Comment on lines +49 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

filterURLForDisplay() also strips off www in addition to protocols. I'm not sure that's what we want here.

Copy link
Member

@aduth aduth Apr 2, 2020

Choose a reason for hiding this comment

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

It might not be what was prescribed by #19670, but if how we're presenting the link here aligns to the purpose of how a function like filterURLForDisplay is expected to be used, then we should embrace the consistency that comes with it. We might decide that trimming www. is not something we want to do for URLs filtered for display, but it's a change which should happen in the implementation of filterURLForDisplay, and not a decision we should want to make at any particular instance of where that function is used.

Or, if it is a decision that depends on the context of where the URL is presented, then we can incorporate that as an option of the function. I'm inclined to think we should try to avoid that if possible, for the sake of consistency (URLs appearing same everywhere), simplicity (smaller function signature), and maintainability (needing to make decisions is a hurdle we can often avoid).

{ 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