diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-14 17:42:14 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-14 17:42:14 +0000 |
commit | 30d32c936fe9ca1f24a555d4ffb77f703607aaa1 (patch) | |
tree | f4ffea67705c1c2e20470a239372e1218c7cad40 /chrome/browser/autocomplete | |
parent | e3c007650c0bbcb7b87a55e34e6ea6f867d0c376 (diff) | |
download | chromium_src-30d32c936fe9ca1f24a555d4ffb77f703607aaa1.zip chromium_src-30d32c936fe9ca1f24a555d4ffb77f703607aaa1.tar.gz chromium_src-30d32c936fe9ca1f24a555d4ffb77f703607aaa1.tar.bz2 |
Color/emphasize correct portions of view-source: URLs. Original patch by Shinichiro Hamaji (see http://codereview.chromium.org/62094/show ), r=me.
BUG=2349
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13671 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete')
-rw-r--r-- | chrome/browser/autocomplete/autocomplete.cc | 41 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete.h | 9 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_win.cc | 17 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_unittest.cc | 45 |
4 files changed, 104 insertions, 8 deletions
diff --git a/chrome/browser/autocomplete/autocomplete.cc b/chrome/browser/autocomplete/autocomplete.cc index 3013638..cbca69d 100644 --- a/chrome/browser/autocomplete/autocomplete.cc +++ b/chrome/browser/autocomplete/autocomplete.cc @@ -236,6 +236,47 @@ AutocompleteInput::Type AutocompleteInput::Parse( return desired_tld.empty() ? UNKNOWN : REQUESTED_URL; } +// static +void AutocompleteInput::ParseForEmphasizeComponents( + const std::wstring& text, + const std::wstring& desired_tld, + url_parse::Component* scheme, + url_parse::Component* host) { + url_parse::Parsed parts; + std::wstring scheme_str; + Parse(text, desired_tld, &parts, &scheme_str); + + *scheme = parts.scheme; + *host = parts.host; + + int after_scheme_and_colon = parts.scheme.end() + 1; + // For the view-source scheme, we should emphasize the scheme and host of + // the URL qualified by the view-source prefix. + if (LowerCaseEqualsASCII(scheme_str, chrome::kViewSourceScheme) && + (static_cast<int>(text.length()) > after_scheme_and_colon)) { + // Obtain the URL prefixed by view-source and parse it. + std::wstring real_url(text.substr(after_scheme_and_colon)); + url_parse::Parsed real_parts; + AutocompleteInput::Parse(real_url, desired_tld, &real_parts, NULL); + if (real_parts.scheme.is_nonempty() || real_parts.host.is_nonempty()) { + if (real_parts.scheme.is_nonempty()) { + *scheme = url_parse::Component( + after_scheme_and_colon + real_parts.scheme.begin, + real_parts.scheme.len); + } else { + scheme->reset(); + } + if (real_parts.host.is_nonempty()) { + *host = url_parse::Component( + after_scheme_and_colon + real_parts.host.begin, + real_parts.host.len); + } else { + host->reset(); + } + } + } +} + bool AutocompleteInput::Equals(const AutocompleteInput& other) const { return (text_ == other.text_) && (type_ == other.type_) && diff --git a/chrome/browser/autocomplete/autocomplete.h b/chrome/browser/autocomplete/autocomplete.h index 4ff9394..98fb7e5 100644 --- a/chrome/browser/autocomplete/autocomplete.h +++ b/chrome/browser/autocomplete/autocomplete.h @@ -193,6 +193,15 @@ class AutocompleteInput { url_parse::Parsed* parts, std::wstring* scheme); + // Parses |text| and fill |scheme| and |host| by the positions of them. + // The results are almost as same as the result of Parse(), but if the scheme + // is view-source, this function returns the positions of scheme and host + // in the URL qualified by "view-source:" prefix. + static void ParseForEmphasizeComponents(const std::wstring& text, + const std::wstring& desired_tld, + url_parse::Component* scheme, + url_parse::Component* host); + // User-provided text to be completed. const std::wstring& text() const { return text_; } diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc index ee9d18a..cfdf3bc 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_win.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_win.cc @@ -1884,9 +1884,10 @@ void AutocompleteEditViewWin::EmphasizeURLComponents() { // this input. This can tell us whether an UNKNOWN input string is going to // be treated as a search or a navigation, and is the same method the Paste // And Go system uses. - url_parse::Parsed parts; - AutocompleteInput::Parse(GetText(), model_->GetDesiredTLD(), &parts, NULL); - const bool emphasize = model_->CurrentTextIsURL() && (parts.host.len > 0); + url_parse::Component scheme, host; + AutocompleteInput::ParseForEmphasizeComponents( + GetText(), model_->GetDesiredTLD(), &scheme, &host); + const bool emphasize = model_->CurrentTextIsURL() && (host.len > 0); // Set the baseline emphasis. CHARFORMAT cf = {0}; @@ -1899,22 +1900,22 @@ void AutocompleteEditViewWin::EmphasizeURLComponents() { if (emphasize) { // We've found a host name, give it more emphasis. cf.crTextColor = GetSysColor(COLOR_WINDOWTEXT); - SetSelection(parts.host.begin, parts.host.end()); + SetSelection(host.begin, host.end()); SetSelectionCharFormat(cf); } // Emphasize the scheme for security UI display purposes (if necessary). insecure_scheme_component_.reset(); - if (!model_->user_input_in_progress() && parts.scheme.is_nonempty() && + if (!model_->user_input_in_progress() && scheme.is_nonempty() && (scheme_security_level_ != ToolbarModel::NORMAL)) { if (scheme_security_level_ == ToolbarModel::SECURE) { cf.crTextColor = kSecureSchemeColor; } else { - insecure_scheme_component_.begin = parts.scheme.begin; - insecure_scheme_component_.len = parts.scheme.len; + insecure_scheme_component_.begin = scheme.begin; + insecure_scheme_component_.len = scheme.len; cf.crTextColor = kInsecureSchemeColor; } - SetSelection(parts.scheme.begin, parts.scheme.end()); + SetSelection(scheme.begin, scheme.end()); SetSelectionCharFormat(cf); } diff --git a/chrome/browser/autocomplete/autocomplete_unittest.cc b/chrome/browser/autocomplete/autocomplete_unittest.cc index e6704d39..6cdd646 100644 --- a/chrome/browser/autocomplete/autocomplete_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_unittest.cc @@ -278,4 +278,49 @@ TEST(AutocompleteMatch, MoreRelevant) { } } +TEST(AutocompleteInput, ParseForEmphasizeComponent) { + using url_parse::Component; + Component kInvalidComponent(0, -1); + struct test_data { + const wchar_t* input; + const Component scheme; + const Component host; + } input_cases[] = { + { L"", kInvalidComponent, kInvalidComponent }, + { L"?", kInvalidComponent, kInvalidComponent }, + { L"?http://foo.com/bar", kInvalidComponent, kInvalidComponent }, + { L"foo/bar baz", kInvalidComponent, Component(0, 3) }, + { L"http://foo/bar baz", Component(0, 4), Component(7, 3) }, + { L"link:foo.com", Component(0, 4), kInvalidComponent }, + { L"www.foo.com:81", kInvalidComponent, Component(0, 11) }, + { L"\u6d4b\u8bd5", kInvalidComponent, Component(0, 2) }, + { L"view-source:http://www.foo.com/", Component(12, 4), Component(19, 11) }, + { L"view-source:https://example.com/", + Component(12, 5), Component(20, 11) }, + { L"view-source:", Component(0, 11), kInvalidComponent }, + { L"view-source:garbage", kInvalidComponent, Component(12, 7) }, + { L"view-source:http://http://foo", Component(12, 4), Component(19, 4) }, + { L"view-source:view-source:http://example.com/", + Component(12, 11), kInvalidComponent } + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { + Component scheme, host; + AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, + std::wstring(), + &scheme, + &host); + AutocompleteInput input(input_cases[i].input, std::wstring(), true, false, + false); + EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin) << "Input: " << + input_cases[i].input; + EXPECT_EQ(input_cases[i].scheme.len, scheme.len) << "Input: " << + input_cases[i].input; + EXPECT_EQ(input_cases[i].host.begin, host.begin) << "Input: " << + input_cases[i].input; + EXPECT_EQ(input_cases[i].host.len, host.len) << "Input: " << + input_cases[i].input; + } +} + } // namespace |