This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
Extension functions for converting URLs to search terms #10480
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,13 @@ | |
package mozilla.components.feature.search.ext | ||
|
||
import android.graphics.Bitmap | ||
import android.net.Uri | ||
import androidx.annotation.VisibleForTesting | ||
import mozilla.components.browser.state.search.OS_SEARCH_ENGINE_TERMS_PARAM | ||
import mozilla.components.browser.state.search.SearchEngine | ||
import mozilla.components.browser.state.state.SearchState | ||
import mozilla.components.browser.state.state.searchEngines | ||
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine | ||
import mozilla.components.feature.search.internal.SearchUrlBuilder | ||
import mozilla.components.feature.search.storage.SearchEngineReader | ||
import java.io.InputStream | ||
|
@@ -21,7 +27,7 @@ fun createSearchEngine( | |
icon: Bitmap, | ||
suggestUrl: String? = null | ||
): SearchEngine { | ||
if (!url.contains("{searchTerms}")) { | ||
if (!url.contains(OS_SEARCH_ENGINE_TERMS_PARAM)) { | ||
throw IllegalArgumentException("URL does not contain search terms placeholder") | ||
} | ||
|
||
|
@@ -66,3 +72,49 @@ fun parseLegacySearchEngine(id: String, stream: InputStream): SearchEngine { | |
val reader = SearchEngineReader(SearchEngine.Type.CUSTOM) | ||
return reader.loadStream(id, stream) | ||
} | ||
|
||
/** | ||
* Given a [SearchState], determine if the passed-in [url] is a known search results page url | ||
* and what are the associated search terms. | ||
* @return Search terms if [url] is a known search results page, `null` otherwise. | ||
*/ | ||
fun SearchState.parseSearchTerms(url: String): String? { | ||
val parsedUrl = Uri.parse(url) | ||
// Default/selected engine is the most likely to match, check it first. | ||
val currentEngine = this.selectedOrDefaultSearchEngine | ||
// Or go through the rest of known engines. | ||
val fallback: () -> String? = fallback@{ | ||
this.searchEngines.forEach { searchEngine -> | ||
searchEngine.parseSearchTerms(parsedUrl)?.let { return@fallback it } | ||
} | ||
return@fallback null | ||
} | ||
return currentEngine?.parseSearchTerms(parsedUrl) ?: fallback() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this will not work for every search engine. Some (like Wikipedia) do a redirect and the resulting URL will not match the build URL anymore. |
||
|
||
/** | ||
* Given a [SearchEngine], determine if the passed-in [url] matches its results template, | ||
* and what are the associated search terms. | ||
* @return Search terms if [url] matches the results page template, `null` otherwise. | ||
*/ | ||
@VisibleForTesting | ||
fun SearchEngine.parseSearchTerms(url: Uri): String? { | ||
// Basic approach: | ||
// - look at the "base" of the template url; if there's a match, continue | ||
// - see if the GET parameter for the search terms is present in the url | ||
// - if that param present, its value is our answer if it's non-empty | ||
val searchResultsRoot = this.resultsUrl.authority + this.resultsUrl.path | ||
val urlRoot = url.authority + url.path | ||
|
||
return if (searchResultsRoot == urlRoot) { | ||
val searchTerms = try { | ||
url.getQueryParameter(this.searchParameterName) | ||
} catch (e: UnsupportedOperationException) { | ||
// Non-hierarchical url. | ||
null | ||
} | ||
searchTerms.takeUnless { it.isNullOrEmpty() } | ||
} else { | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This could just be
"{searchTerms}"
. :)