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

Remove protocol in LinkControl suggestiosn #21279

Closed
wants to merge 1 commit into from
Closed
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 @@ -46,7 +46,11 @@ export const LinkControlSearchItem = ( {
aria-hidden={ ! isURL }
className="block-editor-link-control__search-item-info"
>
{ ! isURL && ( safeDecodeURI( suggestion.url ) || '' ) }
{ ! isURL &&
( safeDecodeURI(
suggestion.url.replace( /http(s?):\/\//, '' )
) ||
'' ) }
{ 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