Skip to content

Commit

Permalink
Added the option to encode the web search's search term (Fixes #104)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Jan 30, 2019
1 parent 8753966 commit 972d22c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions main.html
Expand Up @@ -587,6 +587,7 @@ <h1 class="setting-section-title">User Settings</h1>
<th>Prefix</th>
<th>Url</th>
<th>Whitespace Character</th>
<th class="text-center">Encode Searchterm</th>
<th>Priority</th>
<th class="text-center">Fallback</th>
<th>Icon</th>
Expand All @@ -600,6 +601,7 @@ <h1 class="setting-section-title">User Settings</h1>
<td><input class="setting-text-input" type="text" v-model="webSearch.prefix" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.url" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.whitespaceCharacter" v-on:blur="updateUserConfig"></td>
<td class="text-center"><input type="checkbox" v-model="webSearch.encodeSearchTerm" v-on:change="updateUserConfig"></td>
<td><input class="setting-text-input" type="number" min="0" v-model="webSearch.priority" v-on:blur="updateUserConfig" :disabled="!webSearch.isFallback"></td>
<td class="text-center"><input type="checkbox" v-model="webSearch.isFallback" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.icon" v-on:blur="updateUserConfig"></td>
Expand Down
46 changes: 35 additions & 11 deletions src/tests/unit/builders/web-search-builder.test.ts
Expand Up @@ -3,16 +3,16 @@ import { WebSearch } from "../../../ts/web-search";
import { WebSearchHelpers } from "../../../ts/helpers/web-search-helper";
import { UeliHelpers } from "../../../ts/helpers/ueli-helpers";

describe(WebSearchBuilder.name, (): void => {
describe(WebSearchBuilder.buildExecutionUrl.name, (): void => {
it("should build the execution url correctly", (): void => {
describe(WebSearchBuilder.name, () => {
describe(WebSearchBuilder.buildExecutionUrl.name, () => {
it("should build the execution url correctly", () => {
const webSearch = { url: "https://my-web-search-engine.com/search?q=" } as WebSearch;
const searchTerm = "something";
const actual = WebSearchBuilder.buildExecutionUrl(searchTerm, webSearch);
expect(actual).toBe(`${webSearch.url}${searchTerm}`);
});

it("should trim the search term", (): void => {
it("should trim the search term", () => {
const webSearch = {
url: `https://my-web-search-engine.com/search?q=${UeliHelpers.websearchQueryPlaceholder}&suffix=foo`,
whitespaceCharacter: "+",
Expand All @@ -23,7 +23,7 @@ describe(WebSearchBuilder.name, (): void => {
expect(actual).toBe(expected);
});

it("should replace the query placeholder with the search term if it is set", (): void => {
it("should replace the query placeholder with the search term if it is set", () => {
const userInput = "something";
const webSearchUrl = `https://my-web-search-engine.com/?query=${UeliHelpers.websearchQueryPlaceholder}&suffix=asdf`;
const webSearch = { url: webSearchUrl } as WebSearch;
Expand All @@ -33,7 +33,7 @@ describe(WebSearchBuilder.name, (): void => {
expect(actual).toBe(webSearchUrl.replace(UeliHelpers.websearchQueryPlaceholder, userInput));
});

it("should replace all whitespace by specified whitespace string if it is set", (): void => {
it("should replace all whitespace by specified whitespace string if it is set", () => {
const userInput = "this contains whitespace";
const webSearchUrl = "https://my-search-engine.com/?query=";
const whitespaceCharacter = "+";
Expand All @@ -42,10 +42,34 @@ describe(WebSearchBuilder.name, (): void => {
const actual = WebSearchBuilder.buildExecutionUrl(userInput, webSearch);
expect(actual).toBe(expected);
});

it("should encode the search term if encodeSearchTerm is set to true", () => {
const searchTerm = "c#";
const webSearch = {
encodeSearchTerm: true,
prefix: "g",
url: "https://google.com/q=",
} as WebSearch;
const expected = `${webSearch.url}${encodeURIComponent(searchTerm)}`;
const actual = WebSearchBuilder.buildExecutionUrl(searchTerm, webSearch);
expect(actual).toBe(expected);
});

it("should not encode the search term if encodeSearchTerm is set to false", () => {
const searchTerm = "c#";
const webSearch = {
encodeSearchTerm: false,
prefix: "g",
url: "https://google.com/q=",
} as WebSearch;
const expected = `${webSearch.url}${searchTerm}`;
const actual = WebSearchBuilder.buildExecutionUrl(searchTerm, webSearch);
expect(actual).toBe(expected);
});
});

describe(WebSearchBuilder.buildSearchResultItem.name, (): void => {
it("should build the search result item correctly if userinput is not an empty string", (): void => {
describe(WebSearchBuilder.buildSearchResultItem.name, () => {
it("should build the search result item correctly if userinput is not an empty string", () => {
const userInput = "something";
const webSearch = {
icon: "<svg>...</svg>",
Expand All @@ -61,7 +85,7 @@ describe(WebSearchBuilder.name, (): void => {
expect(actual.name).toBe(`Search ${webSearch.name} for '${userInput}'`);
});

it("should build the search result item correctly if userinput is an empty string", (): void => {
it("should build the search result item correctly if userinput is an empty string", () => {
const userInput = "";

const webSearch = {
Expand All @@ -79,8 +103,8 @@ describe(WebSearchBuilder.name, (): void => {
});
});

describe(WebSearchBuilder.buildSearchTerm.name, (): void => {
it("should build the search term correctly", (): void => {
describe(WebSearchBuilder.buildSearchTerm.name, () => {
it("should build the search term correctly", () => {
const prefix = "m";
const searchTerm = "something";
const userInput = `${prefix}${WebSearchHelpers.webSearchSeparator}${searchTerm}`;
Expand Down
2 changes: 2 additions & 0 deletions src/tests/unit/searcher/fallback-web-search-searcher.test.ts
Expand Up @@ -12,6 +12,7 @@ describe(FallbackWebSearchSercher.name, (): void => {
it("should return a search result for earch fallback web searcher", (): void => {
const webSearches: WebSearch[] = [
{
encodeSearchTerm: false,
icon: "",
isFallback: false,
name: "non fallback",
Expand All @@ -20,6 +21,7 @@ describe(FallbackWebSearchSercher.name, (): void => {
url: "url",
},
{
encodeSearchTerm: false,
icon: "",
isFallback: true,
name: "fallback",
Expand Down
4 changes: 4 additions & 0 deletions src/ts/builders/web-search-builder.ts
Expand Up @@ -12,6 +12,10 @@ export class WebSearchBuilder {
public static buildExecutionUrl(searchTerm: string, webSearch: WebSearch): string {
searchTerm = searchTerm.trim();

if (webSearch.encodeSearchTerm) {
searchTerm = encodeURIComponent(searchTerm);
}

if (webSearch.whitespaceCharacter !== undefined && webSearch.whitespaceCharacter.length > 0) {
searchTerm = StringHelpers.replaceWhitespaceWithString(searchTerm, webSearch.whitespaceCharacter);
}
Expand Down
6 changes: 6 additions & 0 deletions src/ts/user-config/default-web-searches.ts
Expand Up @@ -2,6 +2,7 @@ import { WebSearch } from "../web-search";

export const defaultWebSearches: WebSearch[] = [
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 48 48" style="enable-background:new 0 0 48 48;" xml:space="preserve">
<path style="fill:#FF3D00;" d="M44,24c0,11-9,20-20,20S4,35,4,24S13,4,24,4S44,13,44,24z"></path>
<path style="fill:#FFFFFF;" d="M26,16.2c-0.6-0.6-1.5-0.9-2.5-1.1c-0.4-0.5-1-1-1.9-1.5c-1.6-0.8-3.5-1.2-5.3-0.9h-0.4 c-0.1,0-0.2,0.1-0.4,0.1c0.2,0,1,0.4,1.6,0.6c-0.3,0.2-0.8,0.2-1.1,0.4c0,0,0,0-0.1,0L15.7,14c-0.1,0.2-0.2,0.4-0.2,0.5 c1.3-0.1,3.2,0,4.6,0.4C19,15,18,15.3,17.3,15.7c-0.5,0.3-1,0.6-1.3,1.1c-1.2,1.3-1.7,3.5-1.3,5.9c0.5,2.7,2.4,11.4,3.4,16.3 l0.3,1.6c0,0,3.5,0.4,5.6,0.4c1.2,0,3.2,0.3,3.7-0.2c-0.1,0-0.6-0.6-0.8-1.1c-0.5-1-1-1.9-1.4-2.6c-1.2-2.5-2.5-5.9-1.9-8.1 c0.1-0.4,0.1-2.1,0.4-2.3c2.6-1.7,2.4-0.1,3.5-0.8c0.5-0.4,1-0.9,1.2-1.5C29.4,22.1,27.8,18,26,16.2z"></path>
Expand All @@ -18,6 +19,7 @@ export const defaultWebSearches: WebSearch[] = [
url: "https://duckduckgo.com/?q=",
},
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 48 48" version="1.1">
<g id="surface1">
<path style=" fill:#FFC107;" d="M 43.609375 20.082031 L 42 20.082031 L 42 20 L 24 20 L 24 28 L 35.304688 28 C 33.652344 32.65625 29.222656 36 24 36 C 17.371094 36 12 30.628906 12 24 C 12 17.371094 17.371094 12 24 12 C 27.058594 12 29.84375 13.152344 31.960938 15.039063 L 37.617188 9.382813 C 34.046875 6.054688 29.269531 4 24 4 C 12.953125 4 4 12.953125 4 24 C 4 35.046875 12.953125 44 24 44 C 35.046875 44 44 35.046875 44 24 C 44 22.660156 43.863281 21.351563 43.609375 20.082031 Z "></path>
Expand All @@ -33,6 +35,7 @@ export const defaultWebSearches: WebSearch[] = [
url: "https://google.com/search?q=",
},
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 48 48" version="1.1">
<g id="surface1">
<path style=" fill:#F44336;" d="M 37 24 L 30.0625 24 C 26.714844 24 24 21.285156 24 17.9375 L 24 0.574219 C 24 0.355469 24.261719 0.238281 24.421875 0.390625 L 37 12 Z "></path>
Expand All @@ -52,6 +55,7 @@ export const defaultWebSearches: WebSearch[] = [
url: "https://www.google.com/search?tbm=isch&q=",
},
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 40 40" style="enable-background:new 0 0 40 40;" xml:space="preserve">
<g>
<g>
Expand Down Expand Up @@ -85,6 +89,7 @@ export const defaultWebSearches: WebSearch[] = [
url: "http://www.linguee.de/deutsch-englisch/search?source=auto&query=",
},
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 32 32" version="1.1">
<g id="surface1">
<path d="M 30 9.214844 C 30 9.386719 29.863281 9.523438 29.6875 9.523438 L 28.007813 9.523438 L 20.390625 25.738281 C 20.339844 25.847656 20.230469 25.917969 20.113281 25.917969 C 20.109375 25.917969 20.109375 25.917969 20.109375 25.917969 C 19.988281 25.917969 19.882813 25.851563 19.828125 25.746094 L 16.214844 18.578125 L 12.3125 25.757813 C 12.257813 25.859375 12.15625 25.917969 12.03125 25.917969 C 11.914063 25.914063 11.808594 25.847656 11.757813 25.742188 L 4.054688 9.523438 L 2.3125 9.523438 C 2.140625 9.523438 2 9.386719 2 9.214844 L 2 8.390625 C 2 8.21875 2.140625 8.082031 2.3125 8.082031 L 8.523438 8.082031 C 8.695313 8.082031 8.835938 8.21875 8.835938 8.390625 L 8.835938 9.214844 C 8.835938 9.386719 8.695313 9.523438 8.523438 9.523438 L 7.1875 9.523438 L 12.503906 21.785156 L 15.269531 16.617188 L 11.761719 9.527344 L 10.917969 9.527344 C 10.746094 9.527344 10.605469 9.386719 10.605469 9.214844 L 10.605469 8.394531 C 10.605469 8.222656 10.746094 8.082031 10.917969 8.082031 L 15.515625 8.082031 C 15.6875 8.082031 15.824219 8.222656 15.824219 8.394531 L 15.824219 9.214844 C 15.824219 9.386719 15.6875 9.523438 15.515625 9.523438 L 14.703125 9.523438 L 16.722656 13.9375 L 19.125 9.523438 L 17.652344 9.523438 C 17.476563 9.523438 17.339844 9.386719 17.339844 9.214844 L 17.339844 8.394531 C 17.339844 8.222656 17.476563 8.082031 17.652344 8.082031 L 22.117188 8.082031 C 22.289063 8.082031 22.425781 8.222656 22.425781 8.394531 L 22.425781 9.214844 C 22.425781 9.386719 22.289063 9.523438 22.117188 9.523438 L 21.136719 9.523438 L 17.632813 15.894531 L 20.488281 21.769531 L 26 9.523438 L 24.253906 9.523438 C 24.082031 9.523438 23.941406 9.386719 23.941406 9.214844 L 23.941406 8.394531 C 23.941406 8.222656 24.082031 8.082031 24.253906 8.082031 L 29.6875 8.082031 C 29.863281 8.082031 30 8.222656 30 8.394531 Z "></path>
Expand All @@ -97,6 +102,7 @@ export const defaultWebSearches: WebSearch[] = [
url: "https://en.wikipedia.org/wiki/",
},
{
encodeSearchTerm: true,
icon: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 48 48" version="1.1">
<g id="surface1">
<path style=" fill:#FF3D00;" d="M 43.199219 33.898438 C 42.800781 36 41.101563 37.601563 39 37.898438 C 35.699219 38.398438 30.199219 39 24 39 C 17.898438 39 12.398438 38.398438 9 37.898438 C 6.898438 37.601563 5.199219 36 4.800781 33.898438 C 4.398438 31.601563 4 28.199219 4 24 C 4 19.800781 4.398438 16.398438 4.800781 14.101563 C 5.199219 12 6.898438 10.398438 9 10.101563 C 12.300781 9.601563 17.800781 9 24 9 C 30.199219 9 35.601563 9.601563 39 10.101563 C 41.101563 10.398438 42.800781 12 43.199219 14.101563 C 43.601563 16.398438 44.101563 19.800781 44.101563 24 C 44 28.199219 43.601563 31.601563 43.199219 33.898438 Z "></path>
Expand Down
1 change: 1 addition & 0 deletions src/ts/web-search.ts
Expand Up @@ -6,4 +6,5 @@ export interface WebSearch {
priority: number;
isFallback: boolean;
whitespaceCharacter?: string;
encodeSearchTerm: boolean;
}

0 comments on commit 972d22c

Please sign in to comment.