Repository: servo/servo
File: components/layout/positioned.rs (Rust)

Code before:
        // and distributed alignment values (when both insets are auto).
        // The latter are treated as their fallback alignment.
        let alignment = match self.alignment.value() {
            // https://drafts.csswg.org/css-align/#valdef-self-position-center
            // https://drafts.csswg.org/css-align/#valdef-align-content-space-around
            // https://drafts.csswg.org/css-align/#valdef-align-content-space-evenly
            AlignFlags::CENTER | AlignFlags::SPACE_AROUND | AlignFlags::SPACE_EVENLY => {
                AlignFlags::CENTER
            },
            // https://drafts.csswg.org/css-align/#valdef-self-position-self-start
            AlignFlags::SELF_START if self_value_matches_container() => AlignFlags::START,
            AlignFlags::SELF_START => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-self-position-self-end
            AlignFlags::SELF_END if self_value_matches_container() => AlignFlags::END,
            AlignFlags::SELF_END => AlignFlags::START,
            // https://drafts.csswg.org/css-align/#valdef-justify-content-left
            AlignFlags::LEFT if alignment_container_writing_mode.is_bidi_ltr() => AlignFlags::START,
            AlignFlags::LEFT => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-justify-content-right
            AlignFlags::RIGHT if alignment_container_writing_mode.is_bidi_ltr() => AlignFlags::END,
            AlignFlags::RIGHT => AlignFlags::START,
            // https://drafts.csswg.org/css-align/#valdef-self-position-end
            // https://drafts.csswg.org/css-align/#valdef-self-position-flex-end
            AlignFlags::END | AlignFlags::FLEX_END => AlignFlags::END,
            // https://drafts.csswg.org/css-align-3/#baseline-values
            AlignFlags::LAST_BASELINE => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-self-position-start
            // https://drafts.csswg.org/css-align/#valdef-self-position-flex-start
            _ => AlignFlags::START,
        };

        let alignment = match alignment {
            AlignFlags::START if flip_anchor => AlignFlags::END,
            AlignFlags::END if flip_anchor => AlignFlags::START,
            alignment => alignment,
        };

        let free_space = alignment_container.length - size;
        let flags = self.alignment.flags();
        let alignment = if flags == AlignFlags::SAFE && free_space < Au::zero() {
            AlignFlags::START
        } else {
            alignment
        };

        let origin = match alignment {
            AlignFlags::START => alignment_container.origin,
            AlignFlags::CENTER => alignment_container.origin + free_space / 2,
            AlignFlags::END => alignment_container.origin + free_space,
            _ => unreachable!(),
        };

Review comment (suggestion):
But the spec says `self-end` (and `self-start` for `first baseline`). It should matter for `direction: rtl`.

> The [fallback alignment](https://drafts.csswg.org/css-align-3/#fallback-alignment) for [`last baseline`](https://drafts.csswg.org/css-align-3/#valdef-justify-self-last-baseline) is `safe self-end` (for [self-alignment](https://drafts.csswg.org/css-align-3/#self-align))

Also this isn't using `safe`, though `space-around` and such are also affected, so it's fine for now I guess.

Code after:
        // and distributed alignment values (when both insets are auto).
        // The latter are treated as their fallback alignment.
        let alignment = match self.alignment.value() {
            // https://drafts.csswg.org/css-align/#valdef-self-position-center
            // https://drafts.csswg.org/css-align/#valdef-align-content-space-around
            // https://drafts.csswg.org/css-align/#valdef-align-content-space-evenly
            AlignFlags::CENTER | AlignFlags::SPACE_AROUND | AlignFlags::SPACE_EVENLY => {
                AlignFlags::CENTER
            },
            // https://drafts.csswg.org/css-align/#valdef-self-position-self-start
            AlignFlags::SELF_START if self_value_matches_container() => AlignFlags::START,
            AlignFlags::SELF_START => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-self-position-self-end
            AlignFlags::SELF_END if self_value_matches_container() => AlignFlags::END,
            AlignFlags::SELF_END => AlignFlags::START,
            // https://drafts.csswg.org/css-align/#valdef-justify-content-left
            AlignFlags::LEFT if alignment_container_writing_mode.is_bidi_ltr() => AlignFlags::START,
            AlignFlags::LEFT => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-justify-content-right
            AlignFlags::RIGHT if alignment_container_writing_mode.is_bidi_ltr() => AlignFlags::END,
            AlignFlags::RIGHT => AlignFlags::START,
            // https://drafts.csswg.org/css-align/#valdef-self-position-end
            // https://drafts.csswg.org/css-align/#valdef-self-position-flex-end
            // https://drafts.csswg.org/css-align/#valdef-justify-self-last-baseline
            AlignFlags::END | AlignFlags::FLEX_END | AlignFlags::LAST_BASELINE => AlignFlags::END,
            // https://drafts.csswg.org/css-align/#valdef-self-position-start
            // https://drafts.csswg.org/css-align/#valdef-self-position-flex-start
            // https://drafts.csswg.org/css-align/#valdef-justify-self-first-baseline
            _ => AlignFlags::START,
        };

        let alignment = match alignment {
            AlignFlags::START if flip_anchor => AlignFlags::END,
            AlignFlags::END if flip_anchor => AlignFlags::START,
            alignment => alignment,
        };

        let free_space = alignment_container.length - size;
        let flags = self.alignment.flags();
        let alignment = if flags == AlignFlags::SAFE && free_space < Au::zero() {
            AlignFlags::START
        } else {
            alignment
        };

        let origin = match alignment {
            AlignFlags::START => alignment_container.origin,
            AlignFlags::CENTER => alignment_container.origin + free_space / 2,
            AlignFlags::END => alignment_container.origin + free_space,
            _ => unreachable!(),
        };