Skip to main content

meo_canvas_scene/style/
layout.rs

1//! What the solver reads: the flexbox, grid and block properties.
2//!
3//! The field set is the part of `canvas.type.ts`'s `BoxProps` that taffy
4//! answers to, so translation in `meo-canvas-core` is a move rather than an
5//! interpretation. Anything CSS defines that taffy does not solve is absent
6//! rather than accepted and ignored.
7
8use crate::{
9    geometry::Sides,
10    style::{Dimension, Length},
11    wire::wire_enum,
12};
13
14wire_enum! {
15    /// How a node's children are arranged.
16    pub enum Display {
17        /// Children participate in flex layout.
18        Flex = 0,
19        /// Children are placed on a grid.
20        Grid = 1,
21        /// Children stack as blocks.
22        Block = 2,
23        /// The node and its subtree are neither laid out nor drawn.
24        ///
25        /// Distinct from an opacity of zero, which still occupies space.
26        None = 3,
27    }
28}
29
30wire_enum! {
31    /// The axis children are placed along, and which end they start from.
32    pub enum FlexDirection {
33        /// Left to right, or right to left under [`Direction::Rtl`].
34        Row = 0,
35        /// The reverse of [`FlexDirection::Row`].
36        RowReverse = 1,
37        /// Top to bottom.
38        Column = 2,
39        /// Bottom to top.
40        ColumnReverse = 3,
41    }
42}
43
44wire_enum! {
45    /// Whether children overflow onto further lines.
46    pub enum FlexWrap {
47        /// One line, children shrink to fit.
48        NoWrap = 0,
49        /// Further lines are added below, or after, the first.
50        Wrap = 1,
51        /// Further lines are added before the first.
52        WrapReverse = 2,
53    }
54}
55
56wire_enum! {
57    /// Distribution of free space along the main axis.
58    pub enum Justify {
59        /// Packed at the start.
60        FlexStart = 0,
61        /// Packed at the end.
62        FlexEnd = 1,
63        /// Packed at the centre.
64        Center = 2,
65        /// Free space divided between the children.
66        SpaceBetween = 3,
67        /// Free space divided around each child, so edge gaps are half the
68        /// inner ones.
69        SpaceAround = 4,
70        /// Free space divided evenly, so every gap including the edges is
71        /// equal.
72        SpaceEvenly = 5,
73    }
74}
75
76wire_enum! {
77    /// Placement along the cross axis.
78    ///
79    /// One enum carrying the *union* of what `align-items`, `align-self` and
80    /// `align-content` accept, not their intersection. CSS does not give the
81    /// three the same set -- `align-items` has no `space-*` value at all, and
82    /// `align-content` has all four -- so each variant below names the
83    /// properties that cannot use it. One enum rather than three because the
84    /// overlap is most of the list and a caller reading a scene should not have
85    /// to learn which of three near-identical types a field takes.
86    ///
87    /// A property handed a value it has no meaning for is not an error here.
88    /// The solver resolves it the way CSS does, which for `align-items` given a
89    /// `space-*` value is to behave as [`Align::FlexStart`].
90    pub enum Align {
91        /// At the start of the cross axis.
92        FlexStart = 0,
93        /// At the end.
94        FlexEnd = 1,
95        /// At the centre.
96        Center = 2,
97        /// Filling the cross axis.
98        Stretch = 3,
99        /// Aligned so the children's first baselines coincide.
100        ///
101        /// A measured leaf reports its text's first baseline, offset by its
102        /// own top padding and border, since CSS measures a flex item's
103        /// baseline from its border box. A node reporting none aligns on its
104        /// bottom edge, which is how taffy reads a missing baseline. In a
105        /// column direction this is treated as [`Align::FlexStart`], because
106        /// taffy supports baseline alignment only across a row.
107        Baseline = 4,
108        /// Free space divided between the lines.
109        ///
110        /// `align-content` only; `align-items` and `align-self` align one item
111        /// and have no free space to divide.
112        SpaceBetween = 5,
113        /// Free space divided around each line, so the edge gaps are half the
114        /// inner ones.
115        ///
116        /// `align-content` only.
117        SpaceAround = 6,
118        /// Free space divided evenly, so every gap including the edges is
119        /// equal.
120        ///
121        /// `align-content` only.
122        SpaceEvenly = 7,
123    }
124}
125
126wire_enum! {
127    /// Whether a node is placed by the flow or by its own offsets.
128    ///
129    /// CSS's positioning schemes. `static` and `relative` differ in two observable
130    /// ways -- whether `inset` moves the node, and whether `z_index` gives it a
131    /// place in its parent's stack -- and both were measured in Chrome rather
132    /// than read off the specification; see [`LayoutStyle::inset`] and
133    /// `stacks_by_z_index` in `meo-canvas-core`.
134    #[derive(Default)]
135    pub enum PositionType {
136        /// Placed by the flow, with `inset` shifting it from where it landed.
137        Relative = 0,
138        /// Taken out of the flow and placed by `inset` against its container.
139        Absolute = 1,
140        /// Placed by the flow, and `inset` does not apply.
141        ///
142        /// CSS's initial value, so this is the variant a node that says nothing
143        /// about its position gets.
144        ///
145        /// Appended rather than given the zero it would have if the enum were
146        /// written today:
147        /// the discriminants are a published part of both wire formats, and
148        /// renumbering `Relative` and `Absolute` to put `static` first would
149        /// change what every already-encoded byte means to buy an ordering
150        /// nothing reads.
151        #[default]
152        Static = 2,
153        /// Placed by `inset` against the page rather than against an ancestor.
154        ///
155        /// CSS resolves `fixed` against the viewport, and a still render's
156        /// viewport is its page — there is nothing to scroll relative to, so
157        /// the page is the whole of it.
158        Fixed = 3,
159        /// Placed by the flow, and **identical to [`Relative`] here**.
160        ///
161        /// [`Relative`]: PositionType::Relative
162        ///
163        /// Not a gap: CSS's `sticky` is defined against a scroll position, and
164        /// a still image has none. Chrome draws a sticky element exactly where
165        /// it draws a relative one at scroll offset zero, which is the only
166        /// offset a page ever has here. Carried as its own variant so a scene
167        /// ported from a document keeps saying what it meant, and so a future
168        /// paged or scrolling surface has the word already on the wire.
169        Sticky = 4,
170    }
171}
172
173wire_enum! {
174    /// What happens to content larger than its box.
175    pub enum Overflow {
176        /// Drawn beyond the box.
177        Visible = 0,
178        /// Clipped to the box.
179        Hidden = 1,
180        /// Clipped, and the box reserves room for a scrollbar.
181        ///
182        /// Nothing scrolls in a still image. The variant exists because the
183        /// reserved gutter changes layout, which a caller porting a web design
184        /// depends on.
185        Scroll = 2,
186    }
187}
188
189wire_enum! {
190    /// Whether `width` and `height` include padding and border.
191    pub enum BoxSizing {
192        /// They do. This is the default here, as it is in Yoga.
193        BorderBox = 0,
194        /// They do not. This is CSS's initial value.
195        ContentBox = 1,
196    }
197}
198
199wire_enum! {
200    /// Inline direction, which decides which edge is the start.
201    pub enum Direction {
202        /// Left to right.
203        Ltr = 0,
204        /// Right to left.
205        Rtl = 1,
206    }
207}
208
209wire_enum! {
210    /// CSS's `<self-position>`, with `left` and `right`: where
211    /// [`JustifySelf::Position`] and [`JustifyItems::Position`] put a box on
212    /// the inline axis.
213    ///
214    /// `start` and `end` follow the alignment container's direction,
215    /// `self-start` and `self-end` the box's own, and `left` and `right`
216    /// neither. `flex-start` and `flex-end` are `start` and `end` outside a
217    /// flex container, which ignores both properties.
218    #[non_exhaustive]
219    pub enum SelfPosition {
220        /// Centred in the alignment container.
221        Center = 0,
222        /// At the container's inline start: the left edge under `ltr`, the
223        /// right under `rtl`.
224        Start = 1,
225        /// At the container's inline end.
226        End = 2,
227        /// At the inline start of the box's own direction.
228        SelfStart = 3,
229        /// At the inline end of the box's own direction.
230        SelfEnd = 4,
231        /// As [`SelfPosition::Start`] wherever these properties apply.
232        FlexStart = 5,
233        /// As [`SelfPosition::End`] wherever these properties apply.
234        FlexEnd = 6,
235        /// At the left edge, whatever the direction.
236        Left = 7,
237        /// At the right edge, whatever the direction.
238        Right = 8,
239    }
240}
241
242wire_enum! {
243    /// CSS's `<overflow-position>`: what a positioned box does when it is
244    /// wider than its alignment container.
245    ///
246    /// Stating neither is CSS's default, which for self-alignment places the
247    /// box as [`OverflowPosition::Unsafe`] does, as Chrome does for an
248    /// absolutely positioned box as well.
249    #[non_exhaustive]
250    pub enum OverflowPosition {
251        /// Falls back to `start` when the box overflows, so its start edge
252        /// stays inside the container.
253        Safe = 0,
254        /// Keeps the position asked for, overflowing on both sides if need be.
255        Unsafe = 1,
256    }
257}
258
259wire_enum! {
260    /// The keyword after `legacy` in `justify-items`.
261    #[non_exhaustive]
262    pub enum LegacyPosition {
263        /// At the left edge.
264        Left = 0,
265        /// At the right edge.
266        Right = 1,
267        /// Centred.
268        Center = 2,
269    }
270}
271
272/// CSS's `justify-self`: where this box sits on the inline axis of its
273/// alignment container, overriding the container's `justify-items`.
274///
275/// `auto` is the absence of a value, `LayoutStyle::justify_self` being
276/// `None`. A grid item and a block child take it, and a flex item ignores it,
277/// as in Chrome. `anchor-center` has no variant: anchor positioning has no
278/// counterpart in a scene.
279#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
280#[non_exhaustive]
281pub enum JustifySelf {
282    /// The layout mode's default: a box without a width stretches, and a box
283    /// with one or an image is placed at the start.
284    Normal,
285    /// Fills the alignment container, an image included, unless a width is
286    /// stated.
287    Stretch,
288    /// CSS's `baseline` and `first baseline`. A grid item aligns its left
289    /// border edge with the other items of its column that ask for it, the
290    /// group placed at the start; elsewhere it is `start`.
291    Baseline,
292    /// CSS's `last baseline`: as [`JustifySelf::Baseline`], the group placed at
293    /// the end. Outside a grid's flow it is `end` for an absolutely
294    /// positioned box and `start` for a block child.
295    LastBaseline,
296    /// A `<self-position>`, with or without an `<overflow-position>`.
297    Position {
298        /// Where the box goes.
299        position: SelfPosition,
300        /// What happens when it is wider than its container. `None` states
301        /// neither keyword.
302        overflow: Option<OverflowPosition>,
303    },
304}
305
306/// CSS's `justify-items`: where this box's grid items and block children sit
307/// on the inline axis, for each whose `justify-self` is `auto`.
308///
309/// `LayoutStyle::justify_items` being `None` is CSS's initial `legacy`: a
310/// box takes a `legacy left`, `legacy right` or `legacy center` from the
311/// nearest ancestor stating one, through ancestors that state nothing, and is
312/// otherwise [`JustifyItems::Normal`].
313#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
314#[non_exhaustive]
315pub enum JustifyItems {
316    /// As [`JustifySelf::Normal`] for each child.
317    Normal,
318    /// As [`JustifySelf::Stretch`] for each child.
319    Stretch,
320    /// As [`JustifySelf::Baseline`] for each child.
321    Baseline,
322    /// As [`JustifySelf::LastBaseline`] for each child.
323    LastBaseline,
324    /// As [`JustifySelf::Position`] for each child.
325    Position {
326        /// Where each child goes.
327        position: SelfPosition,
328        /// What happens when a child is wider than its container.
329        overflow: Option<OverflowPosition>,
330    },
331    /// CSS's `legacy left`, `legacy right` or `legacy center`: the position
332    /// for this box's children, handed on to descendants whose
333    /// `justify_items` is `None`.
334    Legacy(LegacyPosition),
335}
336
337impl JustifySelf {
338    /// `safe` with `position`: placed there unless the box overflows, then at
339    /// the start.
340    #[must_use]
341    pub const fn safe(position: SelfPosition) -> Self {
342        Self::Position {
343            position,
344            overflow: Some(OverflowPosition::Safe),
345        }
346    }
347
348    /// `unsafe` with `position`: placed there even when the box overflows.
349    #[must_use]
350    pub const fn unsafe_position(position: SelfPosition) -> Self {
351        Self::Position {
352            position,
353            overflow: Some(OverflowPosition::Unsafe),
354        }
355    }
356}
357
358impl JustifyItems {
359    /// `safe` with `position`: placed there unless a child overflows, then at
360    /// the start.
361    #[must_use]
362    pub const fn safe(position: SelfPosition) -> Self {
363        Self::Position {
364            position,
365            overflow: Some(OverflowPosition::Safe),
366        }
367    }
368
369    /// `unsafe` with `position`: placed there even when a child overflows.
370    #[must_use]
371    pub const fn unsafe_position(position: SelfPosition) -> Self {
372        Self::Position {
373            position,
374            overflow: Some(OverflowPosition::Unsafe),
375        }
376    }
377}
378
379impl From<SelfPosition> for JustifySelf {
380    fn from(position: SelfPosition) -> Self {
381        Self::Position {
382            position,
383            overflow: None,
384        }
385    }
386}
387
388impl From<SelfPosition> for JustifyItems {
389    fn from(position: SelfPosition) -> Self {
390        Self::Position {
391            position,
392            overflow: None,
393        }
394    }
395}
396
397wire_enum! {
398    /// The order the grid's auto-placement algorithm fills tracks in.
399    pub enum GridAutoFlow {
400        /// Fill a row, then move to the next.
401        Row = 0,
402        /// Fill a column, then move to the next.
403        Column = 1,
404        /// As [`GridAutoFlow::Row`], but backfill earlier holes.
405        RowDense = 2,
406        /// As [`GridAutoFlow::Column`], but backfill earlier holes.
407        ColumnDense = 3,
408    }
409}
410
411/// One track of a grid template.
412///
413/// `canvas.type.ts` spells this as ``number | 'auto' | `${number}px` |
414/// `${number}fr` | `${number}%` ``, and a bare number there means pixels.
415#[derive(Debug, Clone, Copy, PartialEq, Default)]
416#[non_exhaustive]
417pub enum TrackSize {
418    /// Sized to its content.
419    #[default]
420    Auto,
421    /// A fixed count of logical pixels.
422    Points(f32),
423    /// A fraction of the grid's extent, where `1.0` is 100%.
424    Percent(f32),
425    /// A share of the free space remaining after the fixed tracks are placed.
426    Fraction(f32),
427}
428
429/// Where a grid item sits, as a track span.
430///
431/// Numeric lines rather than the `'1 / 3'` strings `canvas.type.ts` accepts:
432/// the string is a surface convenience, and parsing it belongs to whichever
433/// surface offered it rather than to a crate every surface links.
434#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
435pub struct GridPlacement {
436    /// The line the item starts at, counting from one. `None` means the
437    /// auto-placement algorithm chooses.
438    pub start: Option<i16>,
439    /// How many tracks the item covers. `None` means one.
440    pub span: Option<u16>,
441}
442
443/// Everything the layout pass reads off a node.
444///
445/// Two of the three interesting defaults are CSS's rather than Yoga's:
446/// [`FlexDirection::Row`] and a `flex_shrink` of `1.0`, where Yoga's raw
447/// defaults are a column direction and a shrink of `0`, so a bare box would
448/// change meaning between the two. The TypeScript `Column` and `Row` factories
449/// already override the shrink to `1`, which makes CSS's value the one their
450/// own trees use; following CSS makes the bare case agree with the named ones
451/// instead of inheriting an exception.
452///
453/// The third, [`Display::Block`], is what Chrome gives a `div` rather than
454/// CSS's initial `inline`, which this crate has no node for: a scene is a tree
455/// of boxes, not a run of inline content. taffy's own default is `Flex`.
456///
457/// What that costs a reader is worth stating, because the shape of the tree
458/// depends on it: the factories on both surfaces name [`Display::Flex`] on
459/// every container, so a tree built through them is flex and one assembled
460/// from bare `Node` values is block. Under a flex parent a text child
461/// shrink-wraps to its content and its `text_align` moves nothing; under a
462/// block parent it fills the line and alignment places it.
463#[derive(Debug, Clone, PartialEq)]
464pub struct LayoutStyle {
465    /// Layout mode for this node's children.
466    pub display: Display,
467    /// Whether the node is placed by the flow or by `inset`.
468    pub position_type: PositionType,
469    /// Offsets from the container's edges.
470    ///
471    /// Read as a position for [`PositionType::Absolute`], as a shift that
472    /// moves the node without moving its siblings for
473    /// [`PositionType::Relative`], and **not at all** for
474    /// [`PositionType::Static`] -- which is the default, so an inset on a
475    /// node that was never positioned does nothing.
476    ///
477    /// Measured in Chrome, not assumed: a static child given
478    /// `top: 30px; left: 30px` sits at its flow position in a block, a flex
479    /// and a grid container alike, while the same child made relative
480    /// moves by 30 on both axes and leaves its sibling exactly where it
481    /// was.
482    pub inset: Sides<Option<Length>>,
483    /// Requested width and height.
484    pub size: (Dimension, Dimension),
485    /// Lower bound on the computed size.
486    pub min_size: (Dimension, Dimension),
487    /// Upper bound on the computed size.
488    pub max_size: (Dimension, Dimension),
489    /// Width divided by height, honoured when one axis is [`Dimension::Auto`].
490    pub aspect_ratio: Option<f32>,
491    /// Space outside the border. [`Dimension::Auto`] here is CSS's auto
492    /// margin, which absorbs free space.
493    pub margin: Sides<Dimension>,
494    /// Space inside the border.
495    pub padding: Sides<Length>,
496    /// Border thickness, which occupies space whether or not it is painted.
497    pub border: Sides<f32>,
498    /// The axis children run along.
499    pub flex_direction: FlexDirection,
500    /// Whether children overflow onto further lines.
501    pub flex_wrap: FlexWrap,
502    /// Share of free space this node absorbs.
503    pub flex_grow: f32,
504    /// Share of overflow this node gives up.
505    pub flex_shrink: f32,
506    /// Size along the main axis before growing or shrinking.
507    pub flex_basis: Dimension,
508    /// Main-axis distribution. `None` leaves the solver's default.
509    pub justify_content: Option<Justify>,
510    /// Cross-axis placement of children.
511    pub align_items: Option<Align>,
512    /// Cross-axis placement of this node, overriding its parent's
513    /// `align_items`.
514    pub align_self: Option<Align>,
515    /// Cross-axis distribution of wrapped lines.
516    pub align_content: Option<Align>,
517    /// Inline-axis placement of this node's grid items and block children,
518    /// or `None` for CSS's initial `legacy`, which takes an ancestor's
519    /// [`JustifyItems::Legacy`] and is otherwise `normal`.
520    pub justify_items: Option<JustifyItems>,
521    /// Inline-axis placement of this node, overriding its parent's
522    /// `justify_items`, or `None` for CSS's `auto`, which takes the parent's.
523    pub justify_self: Option<JustifySelf>,
524    /// Space between children as `(row, column)`.
525    pub gap: (Length, Length),
526    /// Clipping behaviour, per axis as `(x, y)`.
527    pub overflow: (Overflow, Overflow),
528    /// Whether `size` includes padding and border.
529    pub box_sizing: BoxSizing,
530    /// Inline direction, or `None` to take the parent's, as CSS's `direction`
531    /// inherits. A page root stating none is [`Direction::Ltr`].
532    pub direction: Option<Direction>,
533    /// Column tracks, when [`Display::Grid`].
534    pub grid_template_columns: Vec<TrackSize>,
535    /// Row tracks, when [`Display::Grid`].
536    pub grid_template_rows: Vec<TrackSize>,
537    /// Size given to rows the template does not name.
538    pub grid_auto_rows: Option<TrackSize>,
539    /// Size given to columns the template does not name.
540    pub grid_auto_columns: Option<TrackSize>,
541    /// The order auto-placement fills tracks in.
542    pub grid_auto_flow: GridAutoFlow,
543    /// This node's column span within its parent grid.
544    pub grid_column: GridPlacement,
545    /// This node's row span within its parent grid.
546    pub grid_row: GridPlacement,
547}
548
549impl Default for LayoutStyle {
550    fn default() -> Self {
551        Self {
552            display: Display::Block,
553            position_type: PositionType::Static,
554            inset: Sides::all(None),
555            size: (Dimension::Auto, Dimension::Auto),
556            min_size: (Dimension::Auto, Dimension::Auto),
557            max_size: (Dimension::Auto, Dimension::Auto),
558            aspect_ratio: None,
559            margin: Sides::all(Dimension::Points(0.0)),
560            padding: Sides::all(Length::ZERO),
561            border: Sides::all(0.0),
562            flex_direction: FlexDirection::Row,
563            flex_wrap: FlexWrap::NoWrap,
564            flex_grow: 0.0,
565            flex_shrink: 1.0,
566            flex_basis: Dimension::Auto,
567            justify_content: None,
568            align_items: None,
569            align_self: None,
570            align_content: None,
571            justify_items: None,
572            justify_self: None,
573            gap: (Length::ZERO, Length::ZERO),
574            overflow: (Overflow::Visible, Overflow::Visible),
575            box_sizing: BoxSizing::BorderBox,
576            direction: None,
577            grid_template_columns: Vec::new(),
578            grid_template_rows: Vec::new(),
579            grid_auto_rows: None,
580            grid_auto_columns: None,
581            grid_auto_flow: GridAutoFlow::Row,
582            grid_column: GridPlacement::AUTO,
583            grid_row: GridPlacement::AUTO,
584        }
585    }
586}
587
588impl GridPlacement {
589    /// Placement left entirely to the auto-placement algorithm.
590    pub const AUTO: Self = Self {
591        start: None,
592        span: None,
593    };
594
595    /// A span of `span` tracks beginning at `start`, counting from one.
596    #[must_use]
597    pub const fn spanning(start: i16, span: u16) -> Self {
598        Self {
599            start: Some(start),
600            span: Some(span),
601        }
602    }
603}
604
605#[cfg(test)]
606mod tests {
607
608    #[test]
609    fn every_track_size_is_named_here_so_a_new_one_cannot_be_ignored_elsewhere()
610    {
611        // No wildcard, in the crate that owns the type: `meo-canvas-core`'s
612        // wildcard arm would take a new variant and draw nothing, so it fails
613        // to compile here instead -- under `cargo test` rather than `cargo
614        // build`, and the gate runs both.
615        use super::TrackSize;
616        let witness = |value: &TrackSize| match value {
617            TrackSize::Auto => "auto",
618            TrackSize::Points { .. } => "points",
619            TrackSize::Percent { .. } => "percent",
620            TrackSize::Fraction { .. } => "fraction",
621        };
622        let _ = witness(&TrackSize::Auto);
623    }
624
625    #[test]
626    fn every_justify_value_is_named_here_so_a_new_one_cannot_be_ignored_elsewhere()
627     {
628        // As for `TrackSize`: `meo-canvas-core` maps these with a wildcard arm.
629        use super::{JustifyItems, JustifySelf};
630        let own = |value: &JustifySelf| match value {
631            JustifySelf::Normal => "normal",
632            JustifySelf::Stretch => "stretch",
633            JustifySelf::Baseline => "baseline",
634            JustifySelf::LastBaseline => "last baseline",
635            JustifySelf::Position { .. } => "position",
636        };
637        let children = |value: &JustifyItems| match value {
638            JustifyItems::Normal => "normal",
639            JustifyItems::Stretch => "stretch",
640            JustifyItems::Baseline => "baseline",
641            JustifyItems::LastBaseline => "last baseline",
642            JustifyItems::Position { .. } => "position",
643            JustifyItems::Legacy(_) => "legacy",
644        };
645        let _ = (own(&JustifySelf::Normal), children(&JustifyItems::Normal));
646    }
647    use super::{
648        Align, BoxSizing, Direction, Display, FlexDirection, FlexWrap,
649        GridAutoFlow, GridPlacement, Justify, LayoutStyle, LegacyPosition,
650        Overflow, OverflowPosition, PositionType, SelfPosition, TrackSize,
651    };
652    use crate::style::{Dimension, Length};
653
654    #[test]
655    fn defaults_follow_css_not_yoga() {
656        let style = LayoutStyle::default();
657        // `block`, which is what a browser gives a `<div>`, where taffy's own
658        // default is `Flex`. Both public surfaces name `flex` on every
659        // container, so nothing a caller writes depends on this.
660        assert_eq!(style.display, Display::Block);
661        assert_eq!(style.flex_direction, FlexDirection::Row);
662        assert!((style.flex_shrink - 1.0).abs() < f32::EPSILON);
663        assert!((style.flex_grow - 0.0).abs() < f32::EPSILON);
664        assert_eq!(style.box_sizing, BoxSizing::BorderBox);
665        // Absent, not `Ltr`: an unstated direction inherits, which an explicit
666        // `Ltr` under an `rtl` parent does not.
667        assert_eq!(style.direction, None);
668        assert_eq!(style.justify_items, None);
669        assert_eq!(style.justify_self, None);
670        assert_eq!(style.flex_basis, Dimension::Auto);
671        assert_eq!(style.gap, (Length::ZERO, Length::ZERO));
672        assert_eq!(style.overflow, (Overflow::Visible, Overflow::Visible));
673        // CSS's initial value, which is `static` and not `relative`: an
674        // `inset` on a node nobody positioned has to do nothing, and a
675        // `z_index` on one in a block container has to name nothing.
676        assert_eq!(style.position_type, PositionType::Static);
677        assert_eq!(PositionType::default(), PositionType::Static);
678        assert_eq!(style.grid_auto_flow, GridAutoFlow::Row);
679        assert_eq!(style.grid_column, GridPlacement::AUTO);
680        assert!(style.grid_template_rows.is_empty());
681    }
682
683    #[test]
684    fn grid_placement_spanning_is_start_plus_span() {
685        let placement = GridPlacement::spanning(2, 3);
686        assert_eq!(placement.start, Some(2));
687        assert_eq!(placement.span, Some(3));
688        assert_eq!(GridPlacement::default(), GridPlacement::AUTO);
689    }
690
691    #[test]
692    fn track_size_defaults_to_auto() {
693        assert_eq!(TrackSize::default(), TrackSize::Auto);
694    }
695
696    #[test]
697    fn every_layout_enum_lists_its_variants() {
698        assert_eq!(Display::ALL.len(), 4);
699        assert_eq!(FlexDirection::ALL.len(), 4);
700        assert_eq!(FlexWrap::ALL.len(), 3);
701        assert_eq!(Justify::ALL.len(), 6);
702        assert_eq!(Align::ALL.len(), 8);
703        assert_eq!(PositionType::ALL.len(), 5);
704        assert_eq!(Overflow::ALL.len(), 3);
705        assert_eq!(BoxSizing::ALL.len(), 2);
706        assert_eq!(Direction::ALL.len(), 2);
707        assert_eq!(GridAutoFlow::ALL.len(), 4);
708        assert_eq!(SelfPosition::ALL.len(), 9);
709        assert_eq!(OverflowPosition::ALL.len(), 2);
710        assert_eq!(LegacyPosition::ALL.len(), 3);
711    }
712}