style: Fix container query evaluation on unsupported axis

We were falling back to viewport size, which is not what the spec says.

Differential Revision: https://phabricator.services.mozilla.com/D161132
This commit is contained in:
Emilio Cobos Álvarez 2022-11-03 14:07:02 +00:00 committed by Martin Robinson
parent aba0a4bce0
commit 81ae588ec9
3 changed files with 47 additions and 35 deletions

View file

@ -34,11 +34,13 @@ pub type KeywordParser = for<'a, 'i, 't> fn(
#[allow(missing_docs)]
pub enum Evaluator {
Length(QueryFeatureGetter<CSSPixelLength>),
OptionalLength(QueryFeatureGetter<Option<CSSPixelLength>>),
Integer(QueryFeatureGetter<u32>),
Float(QueryFeatureGetter<f32>),
BoolInteger(QueryFeatureGetter<bool>),
/// A non-negative number ratio, such as the one from device-pixel-ratio.
NumberRatio(QueryFeatureGetter<Ratio>),
OptionalNumberRatio(QueryFeatureGetter<Option<Ratio>>),
/// A resolution.
Resolution(QueryFeatureGetter<Resolution>),
/// A keyword value.

View file

@ -591,6 +591,14 @@ impl QueryFeatureExpression {
self.kind
.evaluate(v, |v| expect!(Length, v).to_computed_value(context))
},
Evaluator::OptionalLength(eval) => {
let v = match eval(context) {
Some(v) => v,
None => return false,
};
self.kind
.evaluate(v, |v| expect!(Length, v).to_computed_value(context))
},
Evaluator::Integer(eval) => {
let v = eval(context);
self.kind.evaluate(v, |v| *expect!(Integer, v))
@ -608,6 +616,15 @@ impl QueryFeatureExpression {
self.kind
.evaluate(ratio, |v| expect!(NumberRatio, v).used_value())
},
Evaluator::OptionalNumberRatio(eval) => {
let ratio = match eval(context) {
Some(v) => v,
None => return false,
};
// See above for subtleties here.
self.kind
.evaluate(ratio, |v| expect!(NumberRatio, v).used_value())
},
Evaluator::Resolution(eval) => {
let v = eval(context).dppx();
self.kind.evaluate(v, |v| {
@ -686,7 +703,7 @@ impl QueryExpressionValue {
input: &mut Parser<'i, 't>,
) -> Result<QueryExpressionValue, ParseError<'i>> {
Ok(match for_feature.evaluator {
Evaluator::Length(..) => {
Evaluator::OptionalLength(..) | Evaluator::Length(..) => {
let length = Length::parse_non_negative(context, input)?;
QueryExpressionValue::Length(length)
},
@ -706,7 +723,7 @@ impl QueryExpressionValue {
let number = Number::parse(context, input)?;
QueryExpressionValue::Float(number.get())
},
Evaluator::NumberRatio(..) => {
Evaluator::OptionalNumberRatio(..) | Evaluator::NumberRatio(..) => {
use crate::values::specified::Ratio as SpecifiedRatio;
let ratio = SpecifiedRatio::parse(context, input)?;
QueryExpressionValue::NumberRatio(Ratio::new(ratio.0.get(), ratio.1.get()))

View file

@ -261,51 +261,44 @@ pub struct ContainerInfo {
wm: WritingMode,
}
fn get_container(context: &Context) -> ContainerInfo {
if let Some(ref info) = context.container_info {
return info.clone();
}
ContainerInfo {
size: context.device().au_viewport_size(),
wm: WritingMode::horizontal_tb(),
}
fn eval_width(context: &Context) -> Option<CSSPixelLength> {
let info = context.container_info.as_ref()?;
Some(CSSPixelLength::new(info.size.width.to_f32_px()))
}
fn eval_width(context: &Context) -> CSSPixelLength {
let info = get_container(context);
CSSPixelLength::new(info.size.width.to_f32_px())
fn eval_height(context: &Context) -> Option<CSSPixelLength> {
let info = context.container_info.as_ref()?;
Some(CSSPixelLength::new(info.size.height.to_f32_px()))
}
fn eval_height(context: &Context) -> CSSPixelLength {
let info = get_container(context);
CSSPixelLength::new(info.size.height.to_f32_px())
}
fn eval_inline_size(context: &Context) -> CSSPixelLength {
let info = get_container(context);
CSSPixelLength::new(
fn eval_inline_size(context: &Context) -> Option<CSSPixelLength> {
let info = context.container_info.as_ref()?;
Some(CSSPixelLength::new(
LogicalSize::from_physical(info.wm, info.size)
.inline
.to_f32_px(),
)
))
}
fn eval_block_size(context: &Context) -> CSSPixelLength {
let info = get_container(context);
CSSPixelLength::new(
fn eval_block_size(context: &Context) -> Option<CSSPixelLength> {
let info = context.container_info.as_ref()?;
Some(CSSPixelLength::new(
LogicalSize::from_physical(info.wm, info.size)
.block
.to_f32_px(),
)
))
}
fn eval_aspect_ratio(context: &Context) -> Ratio {
let info = get_container(context);
Ratio::new(info.size.width.0 as f32, info.size.height.0 as f32)
fn eval_aspect_ratio(context: &Context) -> Option<Ratio> {
let info = context.container_info.as_ref()?;
Some(Ratio::new(info.size.width.0 as f32, info.size.height.0 as f32))
}
fn eval_orientation(context: &Context, value: Option<Orientation>) -> bool {
let info = get_container(context);
let info = match context.container_info.as_ref() {
Some(info) => info,
None => return false,
};
Orientation::eval(info.size, value)
}
@ -316,31 +309,31 @@ pub static CONTAINER_FEATURES: [QueryFeatureDescription; 6] = [
feature!(
atom!("width"),
AllowsRanges::Yes,
Evaluator::Length(eval_width),
Evaluator::OptionalLength(eval_width),
FeatureFlags::CONTAINER_REQUIRES_WIDTH_AXIS,
),
feature!(
atom!("height"),
AllowsRanges::Yes,
Evaluator::Length(eval_height),
Evaluator::OptionalLength(eval_height),
FeatureFlags::CONTAINER_REQUIRES_HEIGHT_AXIS,
),
feature!(
atom!("inline-size"),
AllowsRanges::Yes,
Evaluator::Length(eval_inline_size),
Evaluator::OptionalLength(eval_inline_size),
FeatureFlags::CONTAINER_REQUIRES_INLINE_AXIS,
),
feature!(
atom!("block-size"),
AllowsRanges::Yes,
Evaluator::Length(eval_block_size),
Evaluator::OptionalLength(eval_block_size),
FeatureFlags::CONTAINER_REQUIRES_BLOCK_AXIS,
),
feature!(
atom!("aspect-ratio"),
AllowsRanges::Yes,
Evaluator::NumberRatio(eval_aspect_ratio),
Evaluator::OptionalNumberRatio(eval_aspect_ratio),
// XXX from_bits_truncate is const, but the pipe operator isn't, so this
// works around it.
FeatureFlags::from_bits_truncate(