Commit 989fb493 authored by Sébastien Séguin-Gagnon's avatar Sébastien Séguin-Gagnon Committed by Commit Bot

[SH] Remove existing text fragment before adding new one when generating

When generating a link to text, remove existing fragments before
appending the new one(s) to the URL.

Bug: 1143676
Change-Id: Ie49818130219aa9cf1adfc06d5b919c713c58055
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2507629
Commit-Queue: sebsg <sebsg@chromium.org>
Commit-Queue: Sebastien Lalancette <seblalancette@chromium.org>
Reviewed-by: default avatarSebastien Lalancette <seblalancette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822205}
parent 440bbbe7
......@@ -83,9 +83,10 @@ GURL AppendFragmentDirectives(const GURL& base_url,
if (new_ref.find(kFragmentsUrlDelimiter) == std::string::npos) {
new_ref += kFragmentsUrlDelimiter;
} else {
// The URL already had the :~: delimiter, so prepare appending the new
// fragments by appending an ampersand beforehand.
new_ref += "&";
// The URL already had the :~: delimiter, so remove what comes after before
// adding the new fragment(s).
new_ref = new_ref.substr(0, new_ref.find(kFragmentsUrlDelimiter) +
strlen(kFragmentsUrlDelimiter));
}
new_ref += fragments_string;
......
......@@ -73,13 +73,25 @@ TEST(TextFragmentsUtilsTest, AppendFragmentDirectivesURLWithPoundAndValue) {
TEST(TextFragmentsUtilsTest,
AppendFragmentDirectivesURLWithPoundAndExistingFragment) {
GURL base_url("https://www.chromium.org/#:~:text=some%20value");
TextFragment test_fragment("only start");
GURL created_url = AppendFragmentDirectives(base_url, {test_fragment});
EXPECT_EQ(
"https://www.chromium.org/"
"#:~:text=only%20start",
created_url.spec());
}
TEST(TextFragmentsUtilsTest,
AppendFragmentDirectivesURLWithPoundAndExistingFragmentAndAnchor) {
GURL base_url("https://www.chromium.org/#SomeAnchor:~:text=some%20value");
TextFragment test_fragment("only start");
GURL created_url = AppendFragmentDirectives(base_url, {test_fragment});
EXPECT_EQ(
"https://www.chromium.org/"
"#SomeAnchor:~:text=some%20value&text=only%20start",
"#SomeAnchor:~:text=only%20start",
created_url.spec());
}
......
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