diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 05:51:17 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 05:51:17 +0000 |
commit | 3c75f0d3cc8f46251ab2e529229f023745ff05eb (patch) | |
tree | e6ea746a798c2fab03547aebec59f02e7d1a7d4e | |
parent | 3494852e2c7da291fdd1a81922b3229790bff754 (diff) | |
download | chromium_src-3c75f0d3cc8f46251ab2e529229f023745ff05eb.zip chromium_src-3c75f0d3cc8f46251ab2e529229f023745ff05eb.tar.gz chromium_src-3c75f0d3cc8f46251ab2e529229f023745ff05eb.tar.bz2 |
The search terms escaping respect forward slash: it is not escaped if the replacement preceeds ? sign.
BUG=19718
Patch by glotov@chromium.org
Original review http://codereview.chromium.org/601004
TEST=Verify that slash is escaped as stated in description; example: create
search engine en.wikipedia.org/%s and search it with parameters wiki/hi
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40364 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/search_engines/template_url.cc | 50 | ||||
-rw-r--r-- | chrome/browser/search_engines/template_url_unittest.cc | 20 |
2 files changed, 40 insertions, 30 deletions
diff --git a/chrome/browser/search_engines/template_url.cc b/chrome/browser/search_engines/template_url.cc index 9ff827c..8432064 100644 --- a/chrome/browser/search_engines/template_url.cc +++ b/chrome/browser/search_engines/template_url.cc @@ -257,42 +257,50 @@ std::wstring TemplateURLRef::ReplaceSearchTerms( // Determine if the search terms are in the query or before. We're escaping // space as '+' in the former case and as '%20' in the latter case. - bool use_plus = true; + bool is_in_query = true; for (Replacements::iterator i = replacements_.begin(); i != replacements_.end(); ++i) { if (i->type == SEARCH_TERMS) { std::wstring::size_type query_start = parsed_url_.find(L'?'); - use_plus = query_start != std::wstring::npos && + is_in_query = query_start != std::wstring::npos && (static_cast<std::wstring::size_type>(i->index) > query_start); break; } } - // Encode the search terms so that we know the encoding. - const std::vector<std::string>& encodings = host.input_encodings(); string16 encoded_terms; string16 encoded_original_query; std::wstring input_encoding; - for (size_t i = 0; i < encodings.size(); ++i) { - if (EscapeQueryParamValue(WideToUTF16Hack(terms), - encodings[i].c_str(), use_plus, &encoded_terms)) { - if (!original_query_for_suggestion.empty()) { - EscapeQueryParamValue(WideToUTF16Hack(original_query_for_suggestion), - encodings[i].c_str(), - true, - &encoded_original_query); + // If the search terms are in query - escape them respecting the encoding. + if (is_in_query) { + // Encode the search terms so that we know the encoding. + const std::vector<std::string>& encodings = host.input_encodings(); + for (size_t i = 0; i < encodings.size(); ++i) { + if (EscapeQueryParamValue(WideToUTF16Hack(terms), + encodings[i].c_str(), true, + &encoded_terms)) { + if (!original_query_for_suggestion.empty()) { + EscapeQueryParamValue(WideToUTF16Hack(original_query_for_suggestion), + encodings[i].c_str(), + true, + &encoded_original_query); + } + input_encoding = ASCIIToWide(encodings[i]); + break; } - input_encoding = ASCIIToWide(encodings[i]); - break; } - } - if (input_encoding.empty()) { - encoded_terms = WideToUTF16Hack(EscapeQueryParamValueUTF8(terms, use_plus)); - if (!original_query_for_suggestion.empty()) { - encoded_original_query = - WideToUTF16Hack( - EscapeQueryParamValueUTF8(original_query_for_suggestion, true)); + if (input_encoding.empty()) { + encoded_terms = WideToUTF16Hack( + EscapeQueryParamValueUTF8(terms, true)); + if (!original_query_for_suggestion.empty()) { + encoded_original_query = + WideToUTF16Hack(EscapeQueryParamValueUTF8( + original_query_for_suggestion, true)); + } + input_encoding = L"UTF-8"; } + } else { + encoded_terms = WideToUTF16Hack(UTF8ToWide(EscapePath(WideToUTF8(terms)))); input_encoding = L"UTF-8"; } diff --git a/chrome/browser/search_engines/template_url_unittest.cc b/chrome/browser/search_engines/template_url_unittest.cc index fd468cb..5c6414b 100644 --- a/chrome/browser/search_engines/template_url_unittest.cc +++ b/chrome/browser/search_engines/template_url_unittest.cc @@ -43,11 +43,13 @@ TEST_F(TemplateURLTest, URLRefTestSearchTerms) { const wchar_t* terms; const char* output; } search_term_cases[] = { - { L"http://foo{searchTerms}", L"sea rch", "http://foosea%20rch/" }, - { L"http://foo{searchTerms}?boo=abc", L"sea rch", - "http://foosea%20rch/?boo=abc" }, - { L"http://foo/?boo={searchTerms}", L"sea rch", - "http://foo/?boo=sea+rch" } + { L"http://foo{searchTerms}", L"sea rch/bar", "http://foosea%20rch/bar" }, + { L"http://foo{searchTerms}?boo=abc", L"sea rch/bar", + "http://foosea%20rch/bar?boo=abc" }, + { L"http://foo/?boo={searchTerms}", L"sea rch/bar", + "http://foo/?boo=sea+rch%2Fbar" }, + { L"http://en.wikipedia.org/{searchTerms}", L"wiki/?", + "http://en.wikipedia.org/wiki/%3F" } }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(search_term_cases); ++i) { const SearchTermsCase& value = search_term_cases[i]; @@ -274,10 +276,10 @@ TEST_F(TemplateURLTest, ReplaceArbitrarySearchTerms) { const std::wstring url; const std::string expected_result; } data[] = { - { "BIG5", L"\x60BD", L"http://foo/{searchTerms}{inputEncoding}", - "http://foo/%B1~BIG5" }, - { "UTF-8", L"blah", L"http://foo/{searchTerms}{inputEncoding}", - "http://foo/blahUTF-8" }, + { "BIG5", L"\x60BD", L"http://foo/?{searchTerms}{inputEncoding}", + "http://foo/?%B1~BIG5" }, + { "UTF-8", L"blah", L"http://foo/?{searchTerms}{inputEncoding}", + "http://foo/?blahUTF-8" }, }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { TemplateURL turl; |