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

Allow filtering the HTML of Autosuggest items. #2146

Merged
merged 5 commits into from
Apr 13, 2021
Merged

Conversation

JakePT
Copy link
Contributor

@JakePT JakePT commented Mar 23, 2021

Description of the Change

Applies a epAutosuggestItemHTMLFilter() function to autosuggest items to allow filtering the HTML of autosuggest results.

Alternate Designs

In this implementation the filter callback only receives the option object as the second argument, and the option index as the third argument. These can be used to reconstruct the original HTML, or a variation of it, if desired. This means that a custom filter would need to implement any required escaping, as well as highlighting.

An alternative approach could be to also pass the pre-processed escaped text and highlighted results text as arguments to the callback.

The appropriate approach would depend on how much hand holding we want to do.

Benefits

Developers can provide their own HTML for autosuggest result items.

Possible Drawbacks

The potential for autosuggest results to break if future changes are made to the HTML of results that are not reflected in the HTML that may have been applied by a filter.

Verification Process

  • Check out this branch.
  • Run npm run build.
  • In a theme or plugin, define a window.epAutosuggestItemHTMLFilter() JavaScript function that returns some HTML.
  • Start typing in a search field. The autosuggest results should use the HTML returned by our epAutosuggestItemHTMLFilter() function.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

#2143
#2128

Changelog Entry

Added the ability to filter the HTML of autosuggest results by defining a window.epAutosuggestItemHTMLFilter() function in JavaScript.

@JakePT
Copy link
Contributor Author

JakePT commented Mar 23, 2021

This implements a filter that allows changing the HTML of autosuggest results, however I’m not sure it’s enough to be able to get thumbnails in, which has been mentioned. This is because the results that are passed into the function that creates that HTML have been put through the formatSearchResults() function which returns an object that only contains the URL and result text, but does not have a filter of its own. If we want to support developers adding thumbnails to autosuggest results we may need a filter for that function too.

@felipeelia
Copy link
Member

felipeelia commented Mar 23, 2021

@JakePT you are absolutely right. Initially, I thought window.epDataFilter would cover that part but we'll strip out any additional data in formatSearchResults() indeed. Can you please add it too?

… passed to the epAutosuggestItemHTMLFilter filter.
@JakePT
Copy link
Contributor Author

JakePT commented Mar 24, 2021

@felipeelia I've updated the PR to move the basic functionality of formatSearchResults() into the updateAutosuggestBox() function so that the unformatted option is passed to the new filter.

@mckdemps mckdemps added this to the 3.6.0 milestone Apr 9, 2021
@brandwaffle brandwaffle merged commit 9dfe833 into develop Apr 13, 2021
@brandwaffle brandwaffle deleted the feature/2143 branch April 13, 2021 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants