1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 16:47:42 +00:00

LibWeb: Use ValueID for media-query identifiers

This commit is contained in:
Sam Atkins 2022-03-08 17:57:37 +00:00 committed by Andreas Kling
parent 0371d33132
commit fd47460141
4 changed files with 26 additions and 24 deletions

View file

@ -23,7 +23,7 @@ NonnullRefPtr<MediaQuery> MediaQuery::create_not_all()
String MediaFeatureValue::to_string() const String MediaFeatureValue::to_string() const
{ {
return m_value.visit( return m_value.visit(
[](String const& ident) { return serialize_an_identifier(ident); }, [](ValueID const& ident) { return String { string_from_value_id(ident) }; },
[](Length const& length) { return length.to_string(); }, [](Length const& length) { return length.to_string(); },
[](Ratio const& ratio) { return ratio.to_string(); }, [](Ratio const& ratio) { return ratio.to_string(); },
[](Resolution const& resolution) { return resolution.to_string(); }, [](Resolution const& resolution) { return resolution.to_string(); },
@ -33,7 +33,7 @@ String MediaFeatureValue::to_string() const
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
{ {
return m_value.visit( return m_value.visit(
[&](String const&) { return other.is_ident(); }, [&](ValueID const&) { return other.is_ident(); },
[&](Length const&) { return other.is_length(); }, [&](Length const&) { return other.is_length(); },
[&](Ratio const&) { return other.is_ratio(); }, [&](Ratio const&) { return other.is_ratio(); },
[&](Resolution const&) { return other.is_resolution(); }, [&](Resolution const&) { return other.is_resolution(); },
@ -96,7 +96,7 @@ bool MediaFeature::evaluate(HTML::Window const& window) const
if (queried_value.is_resolution()) if (queried_value.is_resolution())
return queried_value.resolution().to_dots_per_pixel() != 0; return queried_value.resolution().to_dots_per_pixel() != 0;
if (queried_value.is_ident()) if (queried_value.is_ident())
return queried_value.ident() != "none"; return queried_value.ident() != ValueID::None;
return false; return false;
case Type::ExactValue: case Type::ExactValue:
@ -129,7 +129,7 @@ bool MediaFeature::compare(HTML::Window const& window, MediaFeatureValue left, C
if (left.is_ident()) { if (left.is_ident()) {
if (comparison == Comparison::Equal) if (comparison == Comparison::Equal)
return left.ident().equals_ignoring_case(right.ident()); return left.ident() == right.ident();
return false; return false;
} }

View file

@ -22,7 +22,7 @@ namespace Web::CSS {
// https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value // https://www.w3.org/TR/mediaqueries-4/#typedef-mf-value
class MediaFeatureValue { class MediaFeatureValue {
public: public:
explicit MediaFeatureValue(String ident) explicit MediaFeatureValue(ValueID ident)
: m_value(move(ident)) : m_value(move(ident))
{ {
} }
@ -49,17 +49,17 @@ public:
String to_string() const; String to_string() const;
bool is_ident() const { return m_value.has<String>(); } 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>(); }
bool is_number() const { return m_value.has<double>(); } bool is_number() const { return m_value.has<double>(); }
bool is_ratio() const { return m_value.has<Ratio>(); } bool is_ratio() const { return m_value.has<Ratio>(); }
bool is_resolution() const { return m_value.has<Resolution>(); } bool is_resolution() const { return m_value.has<Resolution>(); }
bool is_same_type(MediaFeatureValue const& other) const; bool is_same_type(MediaFeatureValue const& other) const;
String const& ident() const ValueID const& ident() const
{ {
VERIFY(is_ident()); VERIFY(is_ident());
return m_value.get<String>(); return m_value.get<ValueID>();
} }
Length const& length() const Length const& length() const
@ -87,7 +87,7 @@ public:
} }
private: private:
Variant<String, Length, Ratio, Resolution, double> m_value; Variant<ValueID, Length, Ratio, Resolution, double> m_value;
}; };
// https://www.w3.org/TR/mediaqueries-4/#mq-features // https://www.w3.org/TR/mediaqueries-4/#mq-features

View file

@ -1187,8 +1187,10 @@ Optional<MediaFeatureValue> Parser::parse_media_feature_value(TokenStream<StyleC
} }
// `<ident>` // `<ident>`
if (first.is(Token::Type::Ident) && !tokens.has_next_token()) if (first.is(Token::Type::Ident)) {
return MediaFeatureValue(first.token().ident()); if (auto ident = value_id_from_string(first.token().ident()); ident != ValueID::Invalid)
return MediaFeatureValue(ident);
}
// `<ratio>` // `<ratio>`
// Note that a single <number> is a valid <ratio>, but it gets parsed above as a <number>. // Note that a single <number> is a valid <ratio>, but it gets parsed above as a <number>.

View file

@ -379,15 +379,15 @@ Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID
// MEDIAQUERIES-4 properties - https://www.w3.org/TR/mediaqueries-4/#media-descriptor-table // MEDIAQUERIES-4 properties - https://www.w3.org/TR/mediaqueries-4/#media-descriptor-table
switch (media_feature) { switch (media_feature) {
case CSS::MediaFeatureID::AnyHover: case CSS::MediaFeatureID::AnyHover:
return CSS::MediaFeatureValue("hover"); return CSS::MediaFeatureValue(CSS::ValueID::Hover);
case CSS::MediaFeatureID::AnyPointer: case CSS::MediaFeatureID::AnyPointer:
return CSS::MediaFeatureValue("fine"); return CSS::MediaFeatureValue(CSS::ValueID::Fine);
case CSS::MediaFeatureID::AspectRatio: case CSS::MediaFeatureID::AspectRatio:
return CSS::MediaFeatureValue(CSS::Ratio(inner_width(), inner_height())); return CSS::MediaFeatureValue(CSS::Ratio(inner_width(), inner_height()));
case CSS::MediaFeatureID::Color: case CSS::MediaFeatureID::Color:
return CSS::MediaFeatureValue(8); return CSS::MediaFeatureValue(8);
case CSS::MediaFeatureID::ColorGamut: case CSS::MediaFeatureID::ColorGamut:
return CSS::MediaFeatureValue("srgb"); return CSS::MediaFeatureValue(CSS::ValueID::Srgb);
case CSS::MediaFeatureID::ColorIndex: case CSS::MediaFeatureID::ColorIndex:
return CSS::MediaFeatureValue(0); return CSS::MediaFeatureValue(0);
// FIXME: device-aspect-ratio // FIXME: device-aspect-ratio
@ -398,22 +398,22 @@ Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID
case CSS::MediaFeatureID::Height: case CSS::MediaFeatureID::Height:
return CSS::MediaFeatureValue(CSS::Length::make_px(inner_height())); return CSS::MediaFeatureValue(CSS::Length::make_px(inner_height()));
case CSS::MediaFeatureID::Hover: case CSS::MediaFeatureID::Hover:
return CSS::MediaFeatureValue("hover"); return CSS::MediaFeatureValue(CSS::ValueID::Hover);
case CSS::MediaFeatureID::Monochrome: case CSS::MediaFeatureID::Monochrome:
return CSS::MediaFeatureValue(0); return CSS::MediaFeatureValue(0);
case CSS::MediaFeatureID::Orientation: case CSS::MediaFeatureID::Orientation:
return CSS::MediaFeatureValue(inner_height() >= inner_width() ? "portrait" : "landscape"); return CSS::MediaFeatureValue(inner_height() >= inner_width() ? CSS::ValueID::Portrait : CSS::ValueID::Landscape);
case CSS::MediaFeatureID::OverflowBlock: case CSS::MediaFeatureID::OverflowBlock:
return CSS::MediaFeatureValue("scroll"); return CSS::MediaFeatureValue(CSS::ValueID::Scroll);
case CSS::MediaFeatureID::OverflowInline: case CSS::MediaFeatureID::OverflowInline:
return CSS::MediaFeatureValue("scroll"); return CSS::MediaFeatureValue(CSS::ValueID::Scroll);
case CSS::MediaFeatureID::Pointer: case CSS::MediaFeatureID::Pointer:
return CSS::MediaFeatureValue("fine"); return CSS::MediaFeatureValue(CSS::ValueID::Fine);
// FIXME: resolution // FIXME: resolution
case CSS::MediaFeatureID::Scan: case CSS::MediaFeatureID::Scan:
return CSS::MediaFeatureValue("progressive"); return CSS::MediaFeatureValue(CSS::ValueID::Progressive);
case CSS::MediaFeatureID::Update: case CSS::MediaFeatureID::Update:
return CSS::MediaFeatureValue("fast"); return CSS::MediaFeatureValue(CSS::ValueID::Fast);
case CSS::MediaFeatureID::Width: case CSS::MediaFeatureID::Width:
return CSS::MediaFeatureValue(CSS::Length::make_px(inner_width())); return CSS::MediaFeatureValue(CSS::Length::make_px(inner_width()));
@ -422,12 +422,12 @@ Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID
if (auto* page = this->page()) { if (auto* page = this->page()) {
switch (page->preferred_color_scheme()) { switch (page->preferred_color_scheme()) {
case CSS::PreferredColorScheme::Light: case CSS::PreferredColorScheme::Light:
return CSS::MediaFeatureValue("light"); return CSS::MediaFeatureValue(CSS::ValueID::Light);
case CSS::PreferredColorScheme::Dark: case CSS::PreferredColorScheme::Dark:
return CSS::MediaFeatureValue("dark"); return CSS::MediaFeatureValue(CSS::ValueID::Dark);
case CSS::PreferredColorScheme::Auto: case CSS::PreferredColorScheme::Auto:
default: default:
return CSS::MediaFeatureValue(page->palette().is_dark() ? "dark" : "light"); return CSS::MediaFeatureValue(page->palette().is_dark() ? CSS::ValueID::Dark : CSS::ValueID::Light);
} }
} }
return CSS::MediaFeatureValue(CSS::ValueID::Light); return CSS::MediaFeatureValue(CSS::ValueID::Light);