1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-17 19:15:08 +00:00

LibWeb: Make serializing media-queries infallible

This commit is contained in:
Sam Atkins 2023-08-22 12:40:18 +01:00 committed by Sam Atkins
parent 2754c16e97
commit afa27bad19
4 changed files with 34 additions and 34 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org> * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
@ -33,7 +33,7 @@ void MediaList::initialize(JS::Realm& realm)
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
DeprecatedString MediaList::media_text() const DeprecatedString MediaList::media_text() const
{ {
return serialize_a_media_query_list(m_media).release_value_but_fixme_should_propagate_errors().to_deprecated_string(); return serialize_a_media_query_list(m_media).to_deprecated_string();
} }
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext // https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
@ -56,7 +56,7 @@ DeprecatedString MediaList::item(u32 index) const
if (!is_supported_property_index(index)) if (!is_supported_property_index(index))
return {}; return {};
return m_media[index]->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); return m_media[index]->to_string().to_deprecated_string();
} }
// https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium // https://www.w3.org/TR/cssom-1/#dom-medialist-appendmedium
@ -70,9 +70,9 @@ void MediaList::append_medium(DeprecatedString medium)
return; return;
// 3. If comparing m with any of the media queries in the collection of media queries returns true, then return. // 3. If comparing m with any of the media queries in the collection of media queries returns true, then return.
auto serialized = m->to_string().release_value_but_fixme_should_propagate_errors(); auto serialized = m->to_string();
for (auto& existing_medium : m_media) { for (auto& existing_medium : m_media) {
if (existing_medium->to_string().release_value_but_fixme_should_propagate_errors() == serialized) if (existing_medium->to_string() == serialized)
return; return;
} }
@ -87,7 +87,7 @@ void MediaList::delete_medium(DeprecatedString medium)
if (!m) if (!m)
return; return;
m_media.remove_all_matching([&](auto& existing) -> bool { m_media.remove_all_matching([&](auto& existing) -> bool {
return m->to_string().release_value_but_fixme_should_propagate_errors() == existing->to_string().release_value_but_fixme_should_propagate_errors(); return m->to_string() == existing->to_string();
}); });
// FIXME: If nothing was removed, then throw a NotFoundError exception. // FIXME: If nothing was removed, then throw a NotFoundError exception.
} }
@ -117,7 +117,7 @@ WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const
{ {
if (index >= m_media.size()) if (index >= m_media.size())
return JS::js_undefined(); return JS::js_undefined();
return JS::PrimitiveString::create(vm(), m_media[index]->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string()); return JS::PrimitiveString::create(vm(), m_media[index]->to_string().to_deprecated_string());
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -21,7 +21,7 @@ NonnullRefPtr<MediaQuery> MediaQuery::create_not_all()
return adopt_ref(*media_query); return adopt_ref(*media_query);
} }
ErrorOr<String> MediaFeatureValue::to_string() const String MediaFeatureValue::to_string() const
{ {
return m_value.visit( return m_value.visit(
[](ValueID const& ident) { return MUST(String::from_utf8(string_from_value_id(ident))); }, [](ValueID const& ident) { return MUST(String::from_utf8(string_from_value_id(ident))); },
@ -41,7 +41,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
[&](float) { return other.is_number(); }); [&](float) { return other.is_number(); });
} }
ErrorOr<String> MediaFeature::to_string() const String MediaFeature::to_string() const
{ {
auto comparison_string = [](Comparison comparison) -> StringView { auto comparison_string = [](Comparison comparison) -> StringView {
switch (comparison) { switch (comparison) {
@ -61,18 +61,18 @@ ErrorOr<String> MediaFeature::to_string() const
switch (m_type) { switch (m_type) {
case Type::IsTrue: case Type::IsTrue:
return String::from_utf8(string_from_media_feature_id(m_id)); return MUST(String::from_utf8(string_from_media_feature_id(m_id)));
case Type::ExactValue: case Type::ExactValue:
return String::formatted("{}:{}", string_from_media_feature_id(m_id), TRY(m_value->to_string())); return MUST(String::formatted("{}:{}", string_from_media_feature_id(m_id), m_value->to_string()));
case Type::MinValue: case Type::MinValue:
return String::formatted("min-{}:{}", string_from_media_feature_id(m_id), TRY(m_value->to_string())); return MUST(String::formatted("min-{}:{}", string_from_media_feature_id(m_id), m_value->to_string()));
case Type::MaxValue: case Type::MaxValue:
return String::formatted("max-{}:{}", string_from_media_feature_id(m_id), TRY(m_value->to_string())); return MUST(String::formatted("max-{}:{}", string_from_media_feature_id(m_id), m_value->to_string()));
case Type::Range: case Type::Range:
if (!m_range->right_comparison.has_value()) if (!m_range->right_comparison.has_value())
return String::formatted("{} {} {}", TRY(m_range->left_value.to_string()), comparison_string(m_range->left_comparison), string_from_media_feature_id(m_id)); return MUST(String::formatted("{} {} {}", m_range->left_value.to_string(), comparison_string(m_range->left_comparison), string_from_media_feature_id(m_id)));
return String::formatted("{} {} {} {} {}", TRY(m_range->left_value.to_string()), comparison_string(m_range->left_comparison), string_from_media_feature_id(m_id), comparison_string(*m_range->right_comparison), TRY(m_range->right_value->to_string())); return MUST(String::formatted("{} {} {} {} {}", m_range->left_value.to_string(), comparison_string(m_range->left_comparison), string_from_media_feature_id(m_id), comparison_string(*m_range->right_comparison), m_range->right_value->to_string()));
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
@ -276,17 +276,17 @@ NonnullOwnPtr<MediaCondition> MediaCondition::from_or_list(Vector<NonnullOwnPtr<
return adopt_own(*result); return adopt_own(*result);
} }
ErrorOr<String> MediaCondition::to_string() const String MediaCondition::to_string() const
{ {
StringBuilder builder; StringBuilder builder;
builder.append('('); builder.append('(');
switch (type) { switch (type) {
case Type::Single: case Type::Single:
builder.append(TRY(feature->to_string())); builder.append(feature->to_string());
break; break;
case Type::Not: case Type::Not:
builder.append("not "sv); builder.append("not "sv);
builder.append(TRY(conditions.first()->to_string())); builder.append(conditions.first()->to_string());
break; break;
case Type::And: case Type::And:
builder.join(" and "sv, conditions); builder.join(" and "sv, conditions);
@ -299,7 +299,7 @@ ErrorOr<String> MediaCondition::to_string() const
break; break;
} }
builder.append(')'); builder.append(')');
return builder.to_string(); return MUST(builder.to_string());
} }
MatchResult MediaCondition::evaluate(HTML::Window const& window) const MatchResult MediaCondition::evaluate(HTML::Window const& window) const
@ -319,7 +319,7 @@ MatchResult MediaCondition::evaluate(HTML::Window const& window) const
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
ErrorOr<String> MediaQuery::to_string() const String MediaQuery::to_string() const
{ {
StringBuilder builder; StringBuilder builder;
@ -333,10 +333,10 @@ ErrorOr<String> MediaQuery::to_string() const
} }
if (m_media_condition) { if (m_media_condition) {
builder.append(TRY(m_media_condition->to_string())); builder.append(m_media_condition->to_string());
} }
return builder.to_string(); return MUST(builder.to_string());
} }
bool MediaQuery::evaluate(HTML::Window const& window) bool MediaQuery::evaluate(HTML::Window const& window)
@ -380,7 +380,7 @@ bool MediaQuery::evaluate(HTML::Window const& window)
} }
// https://www.w3.org/TR/cssom-1/#serialize-a-media-query-list // https://www.w3.org/TR/cssom-1/#serialize-a-media-query-list
ErrorOr<String> serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> const& media_queries) String serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> const& media_queries)
{ {
// 1. If the media query list is empty, then return the empty string. // 1. If the media query list is empty, then return the empty string.
if (media_queries.is_empty()) if (media_queries.is_empty())
@ -388,7 +388,7 @@ ErrorOr<String> serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> c
// 2. Serialize each media query in the list of media queries, in the same order as they // 2. Serialize each media query in the list of media queries, in the same order as they
// appear in the media query list, and then serialize the list. // appear in the media query list, and then serialize the list.
return String::join(", "sv, media_queries); return MUST(String::join(", "sv, media_queries));
} }
bool is_media_feature_name(StringView name) bool is_media_feature_name(StringView name)

View file

@ -46,7 +46,7 @@ public:
{ {
} }
ErrorOr<String> to_string() const; String to_string() const;
bool is_ident() const { return m_value.has<ValueID>(); } bool is_ident() const { return m_value.has<ValueID>(); }
bool is_length() const { return m_value.has<Length>(); } bool is_length() const { return m_value.has<Length>(); }
@ -145,7 +145,7 @@ public:
} }
bool evaluate(HTML::Window const&) const; bool evaluate(HTML::Window const&) const;
ErrorOr<String> to_string() const; String to_string() const;
private: private:
enum class Type { enum class Type {
@ -201,7 +201,7 @@ struct MediaCondition {
static NonnullOwnPtr<MediaCondition> from_or_list(Vector<NonnullOwnPtr<MediaCondition>>&&); static NonnullOwnPtr<MediaCondition> from_or_list(Vector<NonnullOwnPtr<MediaCondition>>&&);
MatchResult evaluate(HTML::Window const&) const; MatchResult evaluate(HTML::Window const&) const;
ErrorOr<String> to_string() const; String to_string() const;
private: private:
MediaCondition() = default; MediaCondition() = default;
@ -240,7 +240,7 @@ public:
bool matches() const { return m_matches; } bool matches() const { return m_matches; }
bool evaluate(HTML::Window const&); bool evaluate(HTML::Window const&);
ErrorOr<String> to_string() const; String to_string() const;
private: private:
MediaQuery() = default; MediaQuery() = default;
@ -254,7 +254,7 @@ private:
bool m_matches { false }; bool m_matches { false };
}; };
ErrorOr<String> serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> const&); String serialize_a_media_query_list(Vector<NonnullRefPtr<MediaQuery>> const&);
bool is_media_feature_name(StringView name); bool is_media_feature_name(StringView name);
@ -269,7 +269,7 @@ template<>
struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> { struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature) ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaFeature const& media_feature)
{ {
return Formatter<StringView>::format(builder, TRY(media_feature.to_string())); return Formatter<StringView>::format(builder, media_feature.to_string());
} }
}; };
@ -277,7 +277,7 @@ template<>
struct Formatter<Web::CSS::MediaCondition> : Formatter<StringView> { struct Formatter<Web::CSS::MediaCondition> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaCondition const& media_condition) ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaCondition const& media_condition)
{ {
return Formatter<StringView>::format(builder, TRY(media_condition.to_string())); return Formatter<StringView>::format(builder, media_condition.to_string());
} }
}; };
@ -285,7 +285,7 @@ template<>
struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> { struct Formatter<Web::CSS::MediaQuery> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query) ErrorOr<void> format(FormatBuilder& builder, Web::CSS::MediaQuery const& media_query)
{ {
return Formatter<StringView>::format(builder, TRY(media_query.to_string())); return Formatter<StringView>::format(builder, media_query.to_string());
} }
}; };

View file

@ -43,7 +43,7 @@ void MediaQueryList::visit_edges(Cell::Visitor& visitor)
// https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
DeprecatedString MediaQueryList::media() const DeprecatedString MediaQueryList::media() const
{ {
return serialize_a_media_query_list(m_media).release_value_but_fixme_should_propagate_errors().to_deprecated_string(); return serialize_a_media_query_list(m_media).to_deprecated_string();
} }
// https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches