Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110//! The `width` generated function's place in the engine: CSS 2.1 10.3.3 block width//! resolution, isolated behind one function with plain-data inputs and outputs//! (`roadmap.md` -> "Phase 1 — Engine bootstrap" step 4, "Generated algorithms, one//! category at a time"). The types a candidate implements against are re-exported from//! [`crate::width_types`] so `layout::width::...` names them; this module says how the//! engine hands them over, and holds the baseline the engine runs when the committed//! function does not build against a new types file. A candidate crate compiles//! independently of the DOM/style layer, and the runner swaps resolvers without touching//! anything else in `fragment.rs`.//!//! **Written against one list of inputs.** The inputs are the list in//! `generated/width.inputs.json`; [`crate::width_engine`] holds a value per input and//! [`crate::width_sources`] reads each from its listed source. The containing block's width//! and `direction` come through the `containing_block` function's answer for the box, which//! `fragment.rs` `layout_box_styled` asks for first. The engine builds a//! [`WidthEngineValues`] and calls through [`call`], which fills the inputs with//! [`inputs_from`] (written by `build.rs` from the types file in place).//!//! [`baseline`] is this function's deliberately wrong starting candidate: every box is zero//! wide with zero margins, whatever its `width`, margins or containing block. This module//! holds no rule of CSS 2.1 10.3 or 10.4.
pub use crate::width_engine::{WidthEngineAnswer, WidthEngineValues};pub use crate::width_types::{WidthInputs, WidthOutputs, WidthResolver};
use dom::node::NodeId;
include!(concat!(env!("OUT_DIR"), "/width_inputs_from.rs"));
/// A box's `direction`, as the absolutely positioned and relatively positioned box/// functions read it (`crate::absolute`, `crate::relpos`). The current corpus is/// exclusively left-to-right (`crates/layout/src/fragment.rs`'s module doc), so `Ltr` is the/// only value any case exercises today.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Direction { Ltr, Rtl,}
/// One call the engine made to the function for a box's own width, as `fragment.rs`/// `layout_box_styled` records it (`crate::call_recording`): the box and the engine's values/// for it. The call a ratio box makes again for its margins, the float path (which does not/// call) and a box whose width a table or a ratio replaces are not recorded, because their/// answer is not the width the browsers measure for the box.#[derive(Debug, Clone, PartialEq)]pub struct WidthCall { pub node: NodeId, /// The engine's values at the call (`inputs_from` turns them into what the resolver /// was handed). pub values: WidthEngineValues,}
/// Asks `resolver` about the box `values` describes: fills the types file's inputs from/// `values` ([`inputs_from`]) and returns the answer. How the engine calls every `width`/// resolver.pub fn call(resolver: WidthResolver, values: &WidthEngineValues) -> WidthOutputs { resolver(&inputs_from(values))}
/// Deliberately wrong starting candidate: a box is zero wide with zero margins.pub fn baseline(_values: &WidthEngineValues) -> WidthOutputs { WidthOutputs { used_width: 0.0, used_margin_left: 0.0, used_margin_right: 0.0 }}
/// [`baseline`] as a resolver, for the engine's tables of resolvers/// (`fragment::Resolvers::baselines`).pub fn resolve_block_width_baseline(_inputs: &WidthInputs) -> WidthOutputs { WidthOutputs { used_width: 0.0, used_margin_left: 0.0, used_margin_right: 0.0 }}
#[cfg(test)]mod tests { use super::*; use dom::computed::ComputedLength;
fn values(containing_block_width: f64, width: ComputedLength) -> WidthEngineValues { WidthEngineValues { width, margin_left: ComputedLength::Length(0.0), margin_right: ComputedLength::Length(0.0), border_left_width: 0.0, padding_left: 0.0, padding_right: 0.0, border_right_width: 0.0, containing_block_width, containing_block_direction: "ltr".to_string(), min_width: ComputedLength::Auto, max_width: ComputedLength::None, } }
#[test] fn the_starting_candidate_makes_every_box_zero_wide() { let v = values(800.0, ComputedLength::Length(220.0)); assert_eq!( call(resolve_block_width_baseline, &v), WidthOutputs { used_width: 0.0, used_margin_left: 0.0, used_margin_right: 0.0 } ); assert_eq!(call(resolve_block_width_baseline, &values(800.0, ComputedLength::Auto)), baseline(&v)); }
#[test] fn the_inputs_are_the_engines_values_one_for_one() { let inputs = inputs_from(&values(640.0, ComputedLength::Percentage(0.5))); assert_eq!(inputs.containing_block_width, 640.0); assert_eq!(inputs.width, ComputedLength::Percentage(0.5)); assert_eq!(inputs.containing_block_direction, "ltr"); }}