| 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 kFormatUrlExperimentalElideAfterHost = 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 // Omit the path, which is a single trailing slash. There's no query or ref. |
| 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 & kFormatUrlExperimentalElideAfterHost) && |
| 518 url.IsStandard() && !url.SchemeIsFile() && |
| 519 !url.SchemeIsFileSystem()) { |
| 520 // Replace everything after the host with a forward slash and ellipsis. |
| 521 url_string.push_back('/'); |
| 522 constexpr base::char16 kEllipsisUTF16[] = {0x2026, 0}; |
| 523 url_string.append(kEllipsisUTF16); |
| 524 |
| 525 // Compute the length of everything we're replacing. For the path, we are |
| 526 // removing everything but the first slash. |
| 527 size_t old_length = parsed.path.len - 1; |
| 528 |
| 529 // We're also removing any query, plus the delimiting '?'. |
| 530 if (parsed.query.is_valid()) |
| 531 old_length += parsed.query.len + 1; |
| 532 |
| 533 // We're also removing any ref, plus the delimiting '#'. |
| 534 if (parsed.ref.is_valid()) |
| 535 old_length += parsed.ref.len + 1; |
| 536 |
| 537 // We're replacing all of these with a single character (an ellipsis). The |
| 538 // adjustment begins after the forward slash at the beginning of the path. |
| 539 adjustments->push_back( |
| 540 base::OffsetAdjuster::Adjustment(parsed.path.begin + 1, old_length, 1)); |
| 541 } else { |
| 542 // Append the formatted path, query, and ref. |
| 543 AppendFormattedComponent(spec, parsed.path, |
| 544 NonHostComponentTransform(unescape_rules), |
| 545 &url_string, &new_parsed->path, adjustments); |
| 546 |
| 547 if (parsed.query.is_valid()) |
| 548 url_string.push_back('?'); |
| 549 AppendFormattedComponent(spec, parsed.query, |
| 550 NonHostComponentTransform(unescape_rules), |
| 551 &url_string, &new_parsed->query, adjustments); |
| 552 |
| 553 // Ref. This is valid, unescaped UTF-8, so we can just convert. |
| 554 if (parsed.ref.is_valid()) |
| 555 url_string.push_back('#'); |
| 556 AppendFormattedComponent(spec, parsed.ref, |
| 557 NonHostComponentTransform(net::UnescapeRule::NONE), |
| 558 &url_string, &new_parsed->ref, adjustments); |
| 519 } | 559 } |
| 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 | 560 |
| 533 // If we need to strip out http do it after the fact. | 561 // If we need to strip out http do it after the fact. |
| 534 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), | 562 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), |
| 535 base::CompareCase::SENSITIVE)) { | 563 base::CompareCase::SENSITIVE)) { |
| 536 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 564 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
| 537 url_string = url_string.substr(kHTTPSize); | 565 url_string = url_string.substr(kHTTPSize); |
| 538 // Because offsets in the |adjustments| are already calculated with respect | 566 // Because offsets in the |adjustments| are already calculated with respect |
| 539 // to the string with the http:// prefix in it, those offsets remain correct | 567 // 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 | 568 // after stripping the prefix. The only thing necessary is to add an |
| 541 // adjustment to reflect the stripped prefix. | 569 // 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) | 605 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) |
| 578 ? text.substr(www.length()) : text; | 606 ? text.substr(www.length()) : text; |
| 579 } | 607 } |
| 580 | 608 |
| 581 base::string16 StripWWWFromHost(const GURL& url) { | 609 base::string16 StripWWWFromHost(const GURL& url) { |
| 582 DCHECK(url.is_valid()); | 610 DCHECK(url.is_valid()); |
| 583 return StripWWW(base::ASCIIToUTF16(url.host_piece())); | 611 return StripWWW(base::ASCIIToUTF16(url.host_piece())); |
| 584 } | 612 } |
| 585 | 613 |
| 586 } // namespace url_formatter | 614 } // namespace url_formatter |
| OLD | NEW |