Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "components/url_formatter/url_formatter.h" | 5 #include "components/url_formatter/url_formatter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 | 353 |
| 354 } // namespace | 354 } // namespace |
| 355 | 355 |
| 356 const FormatUrlType kFormatUrlOmitNothing = 0; | 356 const FormatUrlType kFormatUrlOmitNothing = 0; |
| 357 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; | 357 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; |
| 358 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; | 358 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; |
| 359 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; | 359 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; |
| 360 const FormatUrlType kFormatUrlOmitAll = | 360 const FormatUrlType kFormatUrlOmitAll = |
| 361 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | | 361 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | |
| 362 kFormatUrlOmitTrailingSlashOnBareHostname; | 362 kFormatUrlOmitTrailingSlashOnBareHostname; |
| 363 const FormatUrlType kFormatUrlExperimentalEllipsizePath = 1 << 3; | |
| 363 | 364 |
| 364 base::string16 FormatUrl(const GURL& url, | 365 base::string16 FormatUrl(const GURL& url, |
| 365 FormatUrlTypes format_types, | 366 FormatUrlTypes format_types, |
| 366 net::UnescapeRule::Type unescape_rules, | 367 net::UnescapeRule::Type unescape_rules, |
| 367 url::Parsed* new_parsed, | 368 url::Parsed* new_parsed, |
| 368 size_t* prefix_end, | 369 size_t* prefix_end, |
| 369 size_t* offset_for_adjustment) { | 370 size_t* offset_for_adjustment) { |
| 370 std::vector<size_t> offsets; | 371 std::vector<size_t> offsets; |
| 371 if (offset_for_adjustment) | 372 if (offset_for_adjustment) |
| 372 offsets.push_back(*offset_for_adjustment); | 373 offsets.push_back(*offset_for_adjustment); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 url_string.push_back(':'); | 500 url_string.push_back(':'); |
| 500 new_parsed->port.begin = url_string.length(); | 501 new_parsed->port.begin = url_string.length(); |
| 501 url_string.insert(url_string.end(), spec.begin() + parsed.port.begin, | 502 url_string.insert(url_string.end(), spec.begin() + parsed.port.begin, |
| 502 spec.begin() + parsed.port.end()); | 503 spec.begin() + parsed.port.end()); |
| 503 new_parsed->port.len = url_string.length() - new_parsed->port.begin; | 504 new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
| 504 } else { | 505 } else { |
| 505 new_parsed->port.reset(); | 506 new_parsed->port.reset(); |
| 506 } | 507 } |
| 507 | 508 |
| 508 // Path & query. Both get the same general unescape & convert treatment. | 509 // Path & query. Both get the same general unescape & convert treatment. |
| 509 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) || | 510 if ((format_types & kFormatUrlOmitTrailingSlashOnBareHostname) && |
| 510 !CanStripTrailingSlash(url)) { | 511 CanStripTrailingSlash(url)) { |
| 511 AppendFormattedComponent(spec, parsed.path, | 512 // Case when it's just a single trailing slash we can strip. |
| 512 NonHostComponentTransform(unescape_rules), | |
| 513 &url_string, &new_parsed->path, adjustments); | |
| 514 } else { | |
| 515 if (parsed.path.len > 0) { | 513 if (parsed.path.len > 0) { |
| 516 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 514 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
| 517 parsed.path.begin, parsed.path.len, 0)); | 515 parsed.path.begin, parsed.path.len, 0)); |
| 518 } | 516 } |
| 517 } else if ((format_types & kFormatUrlExperimentalEllipsizePath) && | |
| 518 url.IsStandard() && !url.SchemeIsFile() && | |
| 519 !url.SchemeIsFileSystem()) { | |
| 520 // Case when we replace the path with an ellipsis. | |
| 521 url_string.push_back('/'); | |
| 522 constexpr base::char16 kEllipsisUTF16[] = {0x2026, 0}; | |
| 523 url_string.append(kEllipsisUTF16); | |
|
tommycli
2017/06/28 21:21:40
As a change from PS1, I also added a copy of kElli
| |
| 524 | |
| 525 // Start at begin + 1, since we are preserving the first forward slash. | |
| 526 // Subtract one from the length to account for the first forward slash. | |
| 527 // New length is one to account for the new ellipsis character. | |
| 528 size_t old_length = parsed.path.len - 1; | |
| 529 if (parsed.query.is_valid()) | |
| 530 old_length += parsed.query.len + 1; | |
| 531 if (parsed.ref.is_valid()) | |
| 532 old_length += parsed.ref.len + 1; | |
| 533 adjustments->push_back( | |
| 534 base::OffsetAdjuster::Adjustment(parsed.path.begin + 1, old_length, 1)); | |
| 535 } else { | |
| 536 // Default case. | |
| 537 AppendFormattedComponent(spec, parsed.path, | |
| 538 NonHostComponentTransform(unescape_rules), | |
| 539 &url_string, &new_parsed->path, adjustments); | |
| 540 | |
| 541 if (parsed.query.is_valid()) | |
| 542 url_string.push_back('?'); | |
| 543 AppendFormattedComponent(spec, parsed.query, | |
| 544 NonHostComponentTransform(unescape_rules), | |
| 545 &url_string, &new_parsed->query, adjustments); | |
| 546 | |
| 547 // Ref. This is valid, unescaped UTF-8, so we can just convert. | |
| 548 if (parsed.ref.is_valid()) | |
| 549 url_string.push_back('#'); | |
| 550 AppendFormattedComponent(spec, parsed.ref, | |
| 551 NonHostComponentTransform(net::UnescapeRule::NONE), | |
| 552 &url_string, &new_parsed->ref, adjustments); | |
| 519 } | 553 } |
| 520 if (parsed.query.is_valid()) | |
| 521 url_string.push_back('?'); | |
| 522 AppendFormattedComponent(spec, parsed.query, | |
| 523 NonHostComponentTransform(unescape_rules), | |
| 524 &url_string, &new_parsed->query, adjustments); | |
| 525 | |
| 526 // Ref. This is valid, unescaped UTF-8, so we can just convert. | |
| 527 if (parsed.ref.is_valid()) | |
| 528 url_string.push_back('#'); | |
| 529 AppendFormattedComponent(spec, parsed.ref, | |
| 530 NonHostComponentTransform(net::UnescapeRule::NONE), | |
| 531 &url_string, &new_parsed->ref, adjustments); | |
| 532 | 554 |
| 533 // If we need to strip out http do it after the fact. | 555 // If we need to strip out http do it after the fact. |
| 534 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), | 556 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), |
| 535 base::CompareCase::SENSITIVE)) { | 557 base::CompareCase::SENSITIVE)) { |
| 536 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 558 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
| 537 url_string = url_string.substr(kHTTPSize); | 559 url_string = url_string.substr(kHTTPSize); |
| 538 // Because offsets in the |adjustments| are already calculated with respect | 560 // Because offsets in the |adjustments| are already calculated with respect |
| 539 // to the string with the http:// prefix in it, those offsets remain correct | 561 // to the string with the http:// prefix in it, those offsets remain correct |
| 540 // after stripping the prefix. The only thing necessary is to add an | 562 // after stripping the prefix. The only thing necessary is to add an |
| 541 // adjustment to reflect the stripped prefix. | 563 // adjustment to reflect the stripped prefix. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) | 599 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) |
| 578 ? text.substr(www.length()) : text; | 600 ? text.substr(www.length()) : text; |
| 579 } | 601 } |
| 580 | 602 |
| 581 base::string16 StripWWWFromHost(const GURL& url) { | 603 base::string16 StripWWWFromHost(const GURL& url) { |
| 582 DCHECK(url.is_valid()); | 604 DCHECK(url.is_valid()); |
| 583 return StripWWW(base::ASCIIToUTF16(url.host_piece())); | 605 return StripWWW(base::ASCIIToUTF16(url.host_piece())); |
| 584 } | 606 } |
| 585 | 607 |
| 586 } // namespace url_formatter | 608 } // namespace url_formatter |
| OLD | NEW |