Skip to content

Commit e091242

Browse files
committed
Add optional method to sanitize search input
1 parent eeb1b13 commit e091242

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

WooCommerce/Classes/ViewRelated/Search/SearchUICommand.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,24 @@ protocol SearchUICommand {
8484

8585
/// The Accessibility Identifier for the cancel button
8686
var cancelButtonAccessibilityIdentifier: String { get }
87+
88+
/// Optionally sanitizes the search keyword.
89+
///
90+
/// - Parameter keyword: user-entered search keyword.
91+
/// - Returns: sanitized search keyword.
92+
func sanitizeKeyword(_ keyword: String) -> String
8793
}
8894

8995
// MARK: - Default implementation
9096
extension SearchUICommand {
9197
func configureActionButton(_ button: UIButton, onDismiss: @escaping () -> Void) {
9298
// If not implemented, keeps the default cancel UI/UX
9399
}
100+
101+
func sanitizeKeyword(_ keyword: String) -> String {
102+
// If not implemented, returns the keyword as entered
103+
return keyword
104+
}
94105
}
95106

96107
// MARK: - SearchUICommand using EmptySearchResultsViewController

WooCommerce/Classes/ViewRelated/Search/SearchViewController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where Cell.SearchModel == Command.CellViewModel {
7070
/// Returns the active Keyword
7171
///
7272
private var keyword: String {
73-
return searchBar.text ?? String()
73+
return searchUICommand.sanitizeKeyword(searchBar.text ?? String())
7474
}
7575

7676
/// UI Active State
@@ -205,7 +205,8 @@ where Cell.SearchModel == Command.CellViewModel {
205205
// MARK: - UISearchBarDelegate Conformance
206206
//
207207
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
208-
synchronizeSearchResults(with: searchText)
208+
let sanitizedSearchText = searchUICommand.sanitizeKeyword(searchText)
209+
synchronizeSearchResults(with: sanitizedSearchText)
209210
}
210211

211212
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

0 commit comments

Comments
 (0)