Skip to content

Commit 8d99289

Browse files
authored
feat: use literal replacement for whitespace separators (#85)
1 parent 706c0da commit 8d99289

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

CmdPalWebSearchShortcut/WebSearchShortcut/WebSearchShortcutDataEntry.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ public string Domain
3030

3131
static string UrlEncode(WebSearchShortcutDataEntry shortcut, string query)
3232
{
33-
if (string.IsNullOrWhiteSpace(shortcut.ReplaceWhitespace) || shortcut.ReplaceWhitespace == " ")
34-
{
35-
return WebUtility.UrlEncode(query);
36-
}
37-
if (shortcut.ReplaceWhitespace == "%20")
38-
{
39-
return WebUtility.UrlEncode(query).Replace("+", "%20");
40-
}
41-
return WebUtility.UrlEncode(query.Replace(" ", shortcut.ReplaceWhitespace));
33+
string encoded = WebUtility.UrlEncode(query);
34+
35+
return string.IsNullOrEmpty(shortcut.ReplaceWhitespace)
36+
? encoded
37+
: encoded.Replace("+", shortcut.ReplaceWhitespace);
4238
}
4339

4440
static public string GetSearchUrl(WebSearchShortcutDataEntry shortcut, string query)

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ The URL template for performing the search. Use `%s` as a placeholder for the se
5656

5757
With `ReplaceWhitespace`, you can specify which character(s) to replace a space with when performing a search. This is useful for some websites, such as Wikipedia, which don't use plus signs ("+") to separate words in the URL.
5858

59-
| Value | Result |
60-
|---------------|--------------------|
61-
| `" "` or `""` | `Example+search` |
62-
| `"-"` | `Example-search` |
63-
| `"_"` | `Example_search` |
64-
| `"+"` | `Example%2Bsearch` |
65-
66-
> **Note**: As the string is converted to a URL, any spaces in the string (or `ReplaceWhitespace`) will be replaced with plus signs. Any other characters that are not allowed in a URL will be encoded with [percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding).
59+
| Value | Result |
60+
|----------------|--------------------|
61+
| `""` (Default) | `Example+search` |
62+
| `"+"` | `Example+search` |
63+
| `"%20"` | `Example%20search` |
64+
| `"-"` | `Example-search` |
65+
| `"_"` | `Example_search` |
66+
67+
> **Note**: The character defined in `ReplaceWhitespace` will be used **literally** as the separator without further encoding. While some modern browsers may automatically encode invalid characters, users are responsible for ensuring their chosen character is valid and compatible with the target URL template. For more information on valid URL characters, please refer to [percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding).
6768
6869
### `SuggestionProvider`
6970

0 commit comments

Comments
 (0)