1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

LibWeb: Add Ratio type to MediaFeatureValue

As noted, the Parser can't handle the `<number>` syntax for this - it
gets parsed instead by the `<number>` branch. We can't actually resolve
the ambiguity without making the Parser aware of what type each
media-feature is, but I will get to that soon. :^)
This commit is contained in:
Sam Atkins 2022-03-06 17:50:56 +00:00 committed by Andreas Kling
parent 5f93f1c161
commit deea129b8c
4 changed files with 50 additions and 7 deletions

View file

@ -25,6 +25,7 @@ String MediaFeatureValue::to_string() const
return m_value.visit(
[](String const& ident) { return serialize_an_identifier(ident); },
[](Length const& length) { return length.to_string(); },
[](Ratio const& ratio) { return ratio.to_string(); },
[](Resolution const& resolution) { return resolution.to_string(); },
[](double number) { return String::number(number); });
}
@ -34,6 +35,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
return m_value.visit(
[&](String const&) { return other.is_ident(); },
[&](Length const&) { return other.is_length(); },
[&](Ratio const&) { return other.is_ratio(); },
[&](Resolution const&) { return other.is_resolution(); },
[&](double) { return other.is_number(); });
}
@ -88,6 +90,9 @@ bool MediaFeature::evaluate(DOM::Window const& window) const
return queried_value.number() != 0;
if (queried_value.is_length())
return queried_value.length().raw_value() != 0;
// FIXME: I couldn't figure out from the spec how ratios should be evaluated in a boolean context.
if (queried_value.is_ratio())
return !queried_value.ratio().is_degenerate();
if (queried_value.is_resolution())
return queried_value.resolution().to_dots_per_pixel() != 0;
if (queried_value.is_ident())
@ -181,6 +186,25 @@ bool MediaFeature::compare(DOM::Window const& window, MediaFeatureValue left, Co
VERIFY_NOT_REACHED();
}
if (left.is_ratio()) {
auto left_decimal = left.ratio().value();
auto right_decimal = right.ratio().value();
switch (comparison) {
case Comparison::Equal:
return left_decimal == right_decimal;
case Comparison::LessThan:
return left_decimal < right_decimal;
case Comparison::LessThanOrEqual:
return left_decimal <= right_decimal;
case Comparison::GreaterThan:
return left_decimal > right_decimal;
case Comparison::GreaterThanOrEqual:
return left_decimal >= right_decimal;
}
VERIFY_NOT_REACHED();
}
if (left.is_resolution()) {
auto left_dppx = left.resolution().to_dots_per_pixel();
auto right_dppx = right.resolution().to_dots_per_pixel();