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>
*
* 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
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
@ -56,7 +56,7 @@ DeprecatedString MediaList::item(u32 index) const
if (!is_supported_property_index(index))
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
@ -70,9 +70,9 @@ void MediaList::append_medium(DeprecatedString medium)
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) {
if (existing_medium->to_string().release_value_but_fixme_should_propagate_errors() == serialized)
if (existing_medium->to_string() == serialized)
return;
}
@ -87,7 +87,7 @@ void MediaList::delete_medium(DeprecatedString medium)
if (!m)
return;
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.
}
@ -117,7 +117,7 @@ WebIDL::ExceptionOr<JS::Value> MediaList::item_value(size_t index) const
{
if (index >= m_media.size())
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
*/
@ -21,7 +21,7 @@ NonnullRefPtr<MediaQuery> MediaQuery::create_not_all()
return adopt_ref(*media_query);
}
ErrorOr<String> MediaFeatureValue::to_string() const
String MediaFeatureValue::to_string() const
{
return m_value.visit(
[](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(); });
}
ErrorOr<String> MediaFeature::to_string() const
String MediaFeature::to_string() const
{
auto comparison_string = [](Comparison comparison) -> StringView {
switch (comparison) {
@ -61,18 +61,18 @@ ErrorOr<String> MediaFeature::to_string() const
switch (m_type) {
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:
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:
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:
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:
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();
@ -276,17 +276,17 @@ NonnullOwnPtr<MediaCondition> MediaCondition::from_or_list(Vector<NonnullOwnPtr<
return adopt_own(*result);
}
ErrorOr<String> MediaCondition::to_string() const
String MediaCondition::to_string() const
{
StringBuilder builder;
builder.append('(');
switch (type) {
case Type::Single:
builder.append(TRY(feature->to_string()));
builder.append(feature->to_string());
break;
case Type::Not:
builder.append("not "sv);
builder.append(TRY(conditions.first()->to_string()));
builder.append(conditions.first()->to_string());
break;
case Type::And:
builder.join(" and "sv, conditions);
@ -299,7 +299,7 @@ ErrorOr<String> MediaCondition::to_string() const
break;
}
builder.append(')');
return builder.to_string();
return MUST(builder.to_string());
}
MatchResult MediaCondition::evaluate(HTML::Window const& window) const
@ -319,7 +319,7 @@ MatchResult MediaCondition::evaluate(HTML::Window const& window) const
VERIFY_NOT_REACHED();
}
ErrorOr<String> MediaQuery::to_string() const
String MediaQuery::to_string() const
{
StringBuilder builder;
@ -333,10 +333,10 @@ ErrorOr<String> MediaQuery::to_string() const
}
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)
@ -380,7 +380,7 @@ bool MediaQuery::evaluate(HTML::Window const& window)
}
// 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.
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
// 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)

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_length() const { return m_value.has<Length>(); }
@ -145,7 +145,7 @@ public:
}
bool evaluate(HTML::Window const&) const;
ErrorOr<String> to_string() const;
String to_string() const;
private:
enum class Type {
@ -201,7 +201,7 @@ struct MediaCondition {
static NonnullOwnPtr<MediaCondition> from_or_list(Vector<NonnullOwnPtr<MediaCondition>>&&);
MatchResult evaluate(HTML::Window const&) const;
ErrorOr<String> to_string() const;
String to_string() const;
private:
MediaCondition() = default;
@ -240,7 +240,7 @@ public:
bool matches() const { return m_matches; }
bool evaluate(HTML::Window const&);
ErrorOr<String> to_string() const;
String to_string() const;
private:
MediaQuery() = default;
@ -254,7 +254,7 @@ private:
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);
@ -269,7 +269,7 @@ template<>
struct Formatter<Web::CSS::MediaFeature> : Formatter<StringView> {
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> {
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> {
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
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