mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
LibWeb: Implement MimeType serialization
This commit is contained in:
parent
d463b8e6fb
commit
b42b950688
2 changed files with 42 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -172,6 +173,45 @@ String MimeType::essence() const
|
|||
return String::formatted("{}/{}", m_type, m_subtype);
|
||||
}
|
||||
|
||||
// https://mimesniff.spec.whatwg.org/#serialize-a-mime-type
|
||||
String MimeType::serialized() const
|
||||
{
|
||||
// 1. Let serialization be the concatenation of mimeType’s type, U+002F (/), and mimeType’s subtype.
|
||||
StringBuilder serialization;
|
||||
serialization.append(m_type);
|
||||
serialization.append('/');
|
||||
serialization.append(m_subtype);
|
||||
|
||||
// 2. For each name → value of mimeType’s parameters:
|
||||
for (auto [name, value] : m_parameters) {
|
||||
// 1. Append U+003B (;) to serialization.
|
||||
serialization.append(';');
|
||||
|
||||
// 2. Append name to serialization.
|
||||
serialization.append(name);
|
||||
|
||||
// 3. Append U+003D (=) to serialization.
|
||||
serialization.append('=');
|
||||
|
||||
// 4. If value does not solely contain HTTP token code points or value is the empty string, then:
|
||||
if (!contains_only_http_token_code_points(value) || value.is_empty()) {
|
||||
// 1. Precede each occurence of U+0022 (") or U+005C (\) in value with U+005C (\).
|
||||
value = value.replace("\""sv, "\\\""sv, ReplaceMode::All);
|
||||
value = value.replace("\\"sv, "\\\\"sv, ReplaceMode::All);
|
||||
|
||||
// 2. Prepend U+0022 (") to value.
|
||||
// 3. Append U+0022 (") to value.
|
||||
value = String::formatted("\"{}\"", value);
|
||||
}
|
||||
|
||||
// 5. Append value to serialization.
|
||||
serialization.append(value);
|
||||
}
|
||||
|
||||
// 3. Return serialization.
|
||||
return serialization.to_string();
|
||||
}
|
||||
|
||||
void MimeType::set_parameter(String const& name, String const& value)
|
||||
{
|
||||
// https://mimesniff.spec.whatwg.org/#parameters
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
||||
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -26,6 +27,7 @@ public:
|
|||
void set_parameter(String const& name, String const& value);
|
||||
|
||||
String essence() const;
|
||||
String serialized() const;
|
||||
|
||||
private:
|
||||
// https://mimesniff.spec.whatwg.org/#type
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue