AK: Add spec comments to URL::serialize

This commit is contained in:
Shannon Booth 2023-07-25 20:04:09 +12:00 committed by Tim Flynn
parent 177b04dcfc
commit c8da880806
Notes: sideshowbarker 2024-07-17 03:30:41 +09:00

View file

@ -299,49 +299,68 @@ DeprecatedString URL::serialize(ExcludeFragment exclude_fragment) const
{ {
if (m_scheme == "data") if (m_scheme == "data")
return serialize_data_url(); return serialize_data_url();
StringBuilder builder;
builder.append(m_scheme);
builder.append(':');
// 1. Let output be urls scheme and U+003A (:) concatenated.
StringBuilder output;
output.append(m_scheme);
output.append(':');
// 2. If urls host is non-null:
if (!m_host.is_null()) { if (!m_host.is_null()) {
builder.append("//"sv); // 1. Append "//" to output.
output.append("//"sv);
// 2. If url includes credentials, then:
if (includes_credentials()) { if (includes_credentials()) {
builder.append(m_username); // 1. Append urls username to output.
output.append(m_username);
// 2. If urls password is not the empty string, then append U+003A (:), followed by urls password, to output.
if (!m_password.is_empty()) { if (!m_password.is_empty()) {
builder.append(':'); output.append(':');
builder.append(m_password); output.append(m_password);
} }
builder.append('@');
// 3. Append U+0040 (@) to output.
output.append('@');
} }
builder.append(m_host); // 3. Append urls host, serialized, to output.
output.append(m_host);
// 4. If urls port is non-null, append U+003A (:) followed by urls port, serialized, to output.
if (m_port.has_value()) if (m_port.has_value())
builder.appendff(":{}", *m_port); output.appendff(":{}", *m_port);
} }
// 3. If urls host is null, url does not have an opaque path, urls paths size is greater than 1, and urls path[0] is the empty string, then append U+002F (/) followed by U+002E (.) to output.
// 4. Append the result of URL path serializing url to output.
// FIXME: Implement this closer to spec steps.
if (cannot_be_a_base_url()) { if (cannot_be_a_base_url()) {
builder.append(m_paths[0]); output.append(m_paths[0]);
} else { } else {
if (m_host.is_null() && m_paths.size() > 1 && m_paths[0].is_empty()) if (m_host.is_null() && m_paths.size() > 1 && m_paths[0].is_empty())
builder.append("/."sv); output.append("/."sv);
for (auto& segment : m_paths) { for (auto& segment : m_paths) {
builder.append('/'); output.append('/');
builder.append(segment); output.append(segment);
} }
} }
// 5. If urls query is non-null, append U+003F (?), followed by urls query, to output.
if (!m_query.is_null()) { if (!m_query.is_null()) {
builder.append('?'); output.append('?');
builder.append(m_query); output.append(m_query);
} }
// 6. If exclude fragment is false and urls fragment is non-null, then append U+0023 (#), followed by urls fragment, to output.
if (exclude_fragment == ExcludeFragment::No && !m_fragment.is_null()) { if (exclude_fragment == ExcludeFragment::No && !m_fragment.is_null()) {
builder.append('#'); output.append('#');
builder.append(m_fragment); output.append(m_fragment);
} }
return builder.to_deprecated_string(); // 7. Return output.
return output.to_deprecated_string();
} }
// https://url.spec.whatwg.org/#url-rendering // https://url.spec.whatwg.org/#url-rendering