| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file defines helper functions shared by the various implementations | 5 // This file defines helper functions shared by the various implementations |
| 6 // of OmniboxView. | 6 // of OmniboxView. |
| 7 | 7 |
| 8 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 8 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 9 | 9 |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 14 #include "ui/base/clipboard/clipboard.h" | 14 #include "ui/base/clipboard/clipboard.h" |
| 15 | 15 |
| 16 // static | 16 // static |
| 17 string16 OmniboxView::StripJavascriptSchemas(const string16& text) { | 17 string16 OmniboxView::StripJavascriptSchemas(const string16& text) { |
| 18 const string16 kJsPrefix(ASCIIToUTF16(chrome::kJavaScriptScheme) + | 18 const string16 kJsPrefix(ASCIIToUTF16(chrome::kJavaScriptScheme) + |
| 19 ASCIIToUTF16(":")); | 19 ASCIIToUTF16(":")); |
| 20 string16 out(text); | 20 string16 out(text); |
| 21 while (StartsWith(out, kJsPrefix, false)) | 21 while (StartsWith(out, kJsPrefix, false)) |
| 22 TrimWhitespace(out.substr(kJsPrefix.length()), TRIM_LEADING, &out); | 22 TrimWhitespace(out.substr(kJsPrefix.length()), TRIM_LEADING, &out); |
| 23 return out; | 23 return out; |
| 24 } | 24 } |
| 25 | 25 |
| 26 // static | 26 // static |
| 27 string16 OmniboxView::SanitizeTextForPaste(const string16& text) { |
| 28 // If the input contains non-newline whitespace, treat it as |
| 29 // search data and convert newlines to spaces. For instance, a |
| 30 // street address. |
| 31 // TODO(shess): It may also make sense to ignore leading or |
| 32 // trailing whitespace when making this determination. |
| 33 for (size_t i = 0; i < text.size(); ++i) { |
| 34 if (IsWhitespace(text[i]) && text[i] != '\n' && text[i] != '\r') { |
| 35 const string16 collapsed = CollapseWhitespace(text, false); |
| 36 // If the user is pasting all-whitespace, paste a single space |
| 37 // rather than nothing, since pasting nothing feels broken. |
| 38 return collapsed.empty() ? |
| 39 ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed); |
| 40 } |
| 41 } |
| 42 |
| 43 // Otherwise, the only whitespace in |text| is newlines. Remove |
| 44 // these entirely, because users are most likely pasting URLs |
| 45 // split into multiple lines by terminals, email programs, etc. |
| 46 return StripJavascriptSchemas(CollapseWhitespace(text, true)); |
| 47 } |
| 48 |
| 49 // static |
| 27 string16 OmniboxView::GetClipboardText() { | 50 string16 OmniboxView::GetClipboardText() { |
| 28 // Try text format. | 51 // Try text format. |
| 29 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | 52 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 30 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | 53 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
| 31 ui::Clipboard::BUFFER_STANDARD)) { | 54 ui::Clipboard::BUFFER_STANDARD)) { |
| 32 string16 text; | 55 string16 text; |
| 33 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text); | 56 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text); |
| 34 | 57 return SanitizeTextForPaste(text); |
| 35 // If the input contains non-newline whitespace, treat it as | |
| 36 // search data and convert newlines to spaces. For instance, a | |
| 37 // street address. | |
| 38 // TODO(shess): It may also make sense to ignore leading or | |
| 39 // trailing whitespace when making this determination. | |
| 40 for (size_t i = 0; i < text.size(); ++i) { | |
| 41 if (IsWhitespace(text[i]) && text[i] != '\n' && text[i] != '\r') { | |
| 42 const string16 collapsed = CollapseWhitespace(text, false); | |
| 43 // If the user is pasting all-whitespace, paste a single space | |
| 44 // rather than nothing, since pasting nothing feels broken. | |
| 45 return collapsed.empty() ? | |
| 46 ASCIIToUTF16(" ") : StripJavascriptSchemas(collapsed); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // Otherwise, the only whitespace in |text| is newlines. Remove | |
| 51 // these entirely, because users are most likely pasting URLs | |
| 52 // split into multiple lines by terminals, email programs, etc. | |
| 53 return StripJavascriptSchemas(CollapseWhitespace(text, true)); | |
| 54 } | 58 } |
| 55 | 59 |
| 56 // Try bookmark format. | 60 // Try bookmark format. |
| 57 // | 61 // |
| 58 // It is tempting to try bookmark format first, but the URL we get out of a | 62 // It is tempting to try bookmark format first, but the URL we get out of a |
| 59 // bookmark has been cannonicalized via GURL. This means if a user copies | 63 // bookmark has been cannonicalized via GURL. This means if a user copies |
| 60 // and pastes from the URL bar to itself, the text will get fixed up and | 64 // and pastes from the URL bar to itself, the text will get fixed up and |
| 61 // cannonicalized, which is not what the user expects. By pasting in this | 65 // cannonicalized, which is not what the user expects. By pasting in this |
| 62 // order, we are sure to paste what the user copied. | 66 // order, we are sure to paste what the user copied. |
| 63 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(), | 67 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(), |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // |profile| can be NULL in tests. | 154 // |profile| can be NULL in tests. |
| 151 if (profile) | 155 if (profile) |
| 152 model_.reset(new OmniboxEditModel(this, controller, profile)); | 156 model_.reset(new OmniboxEditModel(this, controller, profile)); |
| 153 } | 157 } |
| 154 | 158 |
| 155 void OmniboxView::TextChanged() { | 159 void OmniboxView::TextChanged() { |
| 156 EmphasizeURLComponents(); | 160 EmphasizeURLComponents(); |
| 157 if (model_.get()) | 161 if (model_.get()) |
| 158 model_->OnChanged(); | 162 model_->OnChanged(); |
| 159 } | 163 } |
| OLD | NEW |