From a9ae99cb1b61c72525342f98d401d7321a25be89 Mon Sep 17 00:00:00 2001 From: KHeo Date: Fri, 21 Feb 2020 12:05:16 +0900 Subject: [PATCH 1/2] Add test. --- .../src/components/link-control/test/index.js | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/link-control/test/index.js b/packages/block-editor/src/components/link-control/test/index.js index 759e1b47a43bb5..3b3b8318a1104e 100644 --- a/packages/block-editor/src/components/link-control/test/index.js +++ b/packages/block-editor/src/components/link-control/test/index.js @@ -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 */ @@ -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( , 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(); From 7f1bf3918039cad5442d150bbf53ed29973eca38 Mon Sep 17 00:00:00 2001 From: KHeo Date: Fri, 21 Feb 2020 12:09:55 +0900 Subject: [PATCH 2/2] Remove protocol. --- .../src/components/link-control/search-item.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/link-control/search-item.js b/packages/block-editor/src/components/link-control/search-item.js index e90a31ec35072f..a7ef58e36d4911 100644 --- a/packages/block-editor/src/components/link-control/search-item.js +++ b/packages/block-editor/src/components/link-control/search-item.js @@ -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'; @@ -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' ) }