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

Trim input value in navigation search input field #19832

Merged
merged 3 commits into from
Mar 12, 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
49 changes: 49 additions & 0 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,55 @@ describe( 'Searching for a link', () => {
);
} );

it( 'should trim search term', async () => {
const searchTerm = ' Hello ';

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

// Search Input UI
const searchInput = container.querySelector(
'input[aria-label="URL"]'
);

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

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

const searchResultTextHighlightElements = Array.from(
container.querySelectorAll(
'[role="listbox"] button[role="option"] mark'
)
);

const invalidResults = searchResultTextHighlightElements.find(
( mark ) => mark.innerHTML !== 'Hello'
);

// Grab the first argument that was passed to the fetchSuggestions
// handler (which is mocked out).
const mockFetchSuggestionsFirstArg =
mockFetchSearchSuggestions.mock.calls[ 0 ][ 0 ];

// Given we're mocking out the results we should always have 4 mark elements.
expect( searchResultTextHighlightElements ).toHaveLength( 4 );

// Make sure there are no `mark` elements which contain anything other
// than the trimmed search term (ie: no whitespace).
expect( invalidResults ).toBeFalsy();

// Implementation detail test to ensure that the fetch handler is called
// with the trimmed search value. We do this because we are mocking out
// the fetch handler in our test so we need to assert it would be called
// correctly in a real world scenario.
expect( mockFetchSuggestionsFirstArg ).toEqual( 'Hello' );
} );

it.each( [
[ 'couldbeurlorentitysearchterm' ],
[ 'ThisCouldAlsoBeAValidURL' ],
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/url-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class URLInput extends Component {

this.props.onChange( inputValue );
if ( ! this.props.disableSuggestions ) {
this.updateSuggestions( inputValue );
this.updateSuggestions( inputValue.trim() );
}
}

Expand All @@ -231,7 +231,7 @@ class URLInput extends Component {
! ( suggestions && suggestions.length )
) {
// Ensure the suggestions are updated with the current input value
this.updateSuggestions( value );
this.updateSuggestions( value.trim() );
Copy link
Member

Choose a reason for hiding this comment

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

If the updateSuggestions() func is expected to always be called with trimmed value, it's probably better to do the trimming inside that very function. Not a blocker though, just a suggestion - feel free to 🚢 this one. Nice work!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO, it's only duplicated once now. When we need trimming once more, it's strikeout and let's refactor.

I cannot think of a case that doesn't need trimming. But we don't know the future.

}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/components/src/text-highlight/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { escapeRegExp } from 'lodash';
import { __experimentalCreateInterpolateElement } from '@wordpress/element';

const TextHighlight = ( { text = '', highlight = '' } ) => {
if ( ! highlight.trim() ) {
const trimmedHighlightText = highlight.trim();

if ( ! trimmedHighlightText ) {
return text;
}

const regex = new RegExp( `(${ escapeRegExp( highlight ) })`, 'gi' );
const regex = new RegExp(
`(${ escapeRegExp( trimmedHighlightText ) })`,
'gi'
);

return __experimentalCreateInterpolateElement(
text.replace( regex, '<mark>$&</mark>' ),
Expand Down