Commit 712cbe98 authored by John Lee's avatar John Lee Committed by Commit Bot

Copy Link To Text: Replace refs in current page's URL

This CL replaces everything after the '#' if it exists in a URL to
avoid chaining multiple refs such as #anchor#:~:text=selected%20text.

Bug: 1136468
Change-Id: I22583af358c1169a2b97bfad12d2e75a2d8e172c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2460797Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: John Lee <johntlee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815427}
parent 780bea5b
...@@ -27,7 +27,13 @@ CopyLinkToTextMenuObserver::~CopyLinkToTextMenuObserver() = default; ...@@ -27,7 +27,13 @@ CopyLinkToTextMenuObserver::~CopyLinkToTextMenuObserver() = default;
void CopyLinkToTextMenuObserver::InitMenu( void CopyLinkToTextMenuObserver::InitMenu(
const content::ContextMenuParams& params) { const content::ContextMenuParams& params) {
url_ = params.page_url; if (params.page_url.has_ref()) {
GURL::Replacements replacements;
replacements.ClearRef();
url_ = params.page_url.ReplaceComponents(replacements);
} else {
url_ = params.page_url;
}
selected_text_ = params.selection_text; selected_text_ = params.selection_text;
proxy_->AddMenuItem( proxy_->AddMenuItem(
......
...@@ -97,3 +97,19 @@ IN_PROC_BROWSER_TEST_F(CopyLinkToTextMenuObserverTest, ...@@ -97,3 +97,19 @@ IN_PROC_BROWSER_TEST_F(CopyLinkToTextMenuObserverTest,
clipboard->ReadText(ui::ClipboardBuffer::kCopyPaste, nullptr, &text); clipboard->ReadText(ui::ClipboardBuffer::kCopyPaste, nullptr, &text);
EXPECT_EQ(base::UTF8ToUTF16("\"hello world\"\nhttp://foo.com/"), text); EXPECT_EQ(base::UTF8ToUTF16("\"hello world\"\nhttp://foo.com/"), text);
} }
IN_PROC_BROWSER_TEST_F(CopyLinkToTextMenuObserverTest, ReplacesRefInURL) {
content::BrowserTestClipboardScope test_clipboard_scope;
content::ContextMenuParams params;
params.page_url = GURL("http://foo.com/#:~:text=hello%20world");
params.selection_text = base::UTF8ToUTF16("hello world");
observer()->OverrideGeneratedSelectorForTesting("hello");
InitMenu(params);
menu()->ExecuteCommand(IDC_CONTENT_CONTEXT_COPYLINKTOTEXT, 0);
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
base::string16 text;
clipboard->ReadText(ui::ClipboardBuffer::kCopyPaste, nullptr, &text);
EXPECT_EQ(base::UTF8ToUTF16("\"hello world\"\nhttp://foo.com/#:~:text=hello"),
text);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment