aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout_2020/flexbox.rs
blob: c0d852ff47c5dff3980ebfecb536e1b65d516c7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom_traversal::{
    BoxSlot, Contents, NodeAndStyleInfo, NodeExt, NonReplacedContents, TraversalHandler,
};
use crate::element_data::LayoutBox;
use crate::formatting_contexts::{IndependentFormattingContext, IndependentLayout};
use crate::fragments::Tag;
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
use crate::sizing::{BoxContentSizes, ContentSizes, ContentSizesRequest};
use crate::style_ext::DisplayGeneratingBox;
use crate::ContainingBlock;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use servo_arc::Arc;
use std::borrow::Cow;
use style::values::computed::Length;
use style::values::specified::text::TextDecorationLine;
use style::Zero;

// FIXME: `min-width: auto` is not zero: https://drafts.csswg.org/css-flexbox/#min-size-auto

#[derive(Debug, Serialize)]
pub(crate) struct FlexContainer {
    children: Vec<ArcRefCell<FlexLevelBox>>,
}

#[derive(Debug, Serialize)]
pub(crate) enum FlexLevelBox {
    FlexItem(IndependentFormattingContext),
    OutOfFlowAbsolutelyPositionedBox(Arc<AbsolutelyPositionedBox>),
}

impl FlexContainer {
    pub fn construct<'dom>(
        context: &LayoutContext,
        info: &NodeAndStyleInfo<impl NodeExt<'dom>>,
        contents: NonReplacedContents,
        content_sizes: ContentSizesRequest,
        propagated_text_decoration_line: TextDecorationLine,
    ) -> (Self, BoxContentSizes) {
        let text_decoration_line =
            propagated_text_decoration_line | info.style.clone_text_decoration_line();
        let mut builder = FlexContainerBuilder {
            context,
            info,
            text_decoration_line,
            contiguous_text_runs: Vec::new(),
            jobs: Vec::new(),
            has_text_runs: false,
        };
        contents.traverse(context, info, &mut builder);
        let content_sizes = content_sizes.compute(|| {
            // FIXME
            ContentSizes::zero()
        });
        (builder.finish(), content_sizes)
    }
}

/// https://drafts.csswg.org/css-flexbox/#flex-items
struct FlexContainerBuilder<'a, 'dom, Node> {
    context: &'a LayoutContext<'a>,
    info: &'a NodeAndStyleInfo<Node>,
    text_decoration_line: TextDecorationLine,
    contiguous_text_runs: Vec<TextRun<'dom, Node>>,
    /// To be run in parallel with rayon in `finish`
    jobs: Vec<FlexLevelJob<'dom, Node>>,
    has_text_runs: bool,
}

enum FlexLevelJob<'dom, Node> {
    /// Or pseudo-element
    Element {
        info: NodeAndStyleInfo<Node>,
        display: DisplayGeneratingBox,
        contents: Contents,
        box_slot: BoxSlot<'dom>,
    },
    TextRuns(Vec<TextRun<'dom, Node>>),
}

struct TextRun<'dom, Node> {
    info: NodeAndStyleInfo<Node>,
    text: Cow<'dom, str>,
}

impl<'a, 'dom, Node: 'dom> TraversalHandler<'dom, Node> for FlexContainerBuilder<'a, 'dom, Node>
where
    Node: NodeExt<'dom>,
{
    fn handle_text(&mut self, info: &NodeAndStyleInfo<Node>, text: Cow<'dom, str>) {
        self.contiguous_text_runs.push(TextRun {
            info: info.clone(),
            text,
        })
    }

    /// Or pseudo-element
    fn handle_element(
        &mut self,
        info: &NodeAndStyleInfo<Node>,
        display: DisplayGeneratingBox,
        contents: Contents,
        box_slot: BoxSlot<'dom>,
    ) {
        // FIXME: are text runs considered "contiguous" if they are only separated
        // by an out-of-flow abspos element?
        // (That is, are they wrapped in the same anonymous flex item, or each its own?)
        self.wrap_any_text_in_anonymous_block_container();

        self.jobs.push(FlexLevelJob::Element {
            info: info.clone(),
            display,
            contents,
            box_slot,
        })
    }
}

/// https://drafts.csswg.org/css-text/#white-space
fn is_only_document_white_space<Node>(run: &TextRun<'_, Node>) -> bool {
    // FIXME: is this the right definition? See
    // https://github.com/w3c/csswg-drafts/issues/5146
    // https://github.com/w3c/csswg-drafts/issues/5147
    run.text
        .bytes()
        .all(|byte| matches!(byte, b' ' | b'\n' | b'\t'))
}

impl<'a, 'dom, Node: 'dom> FlexContainerBuilder<'a, 'dom, Node>
where
    Node: NodeExt<'dom>,
{
    fn wrap_any_text_in_anonymous_block_container(&mut self) {
        let runs = std::mem::take(&mut self.contiguous_text_runs);
        if runs.iter().all(is_only_document_white_space) {
            // There is no text run, or they all only contain document white space characters
        } else {
            self.jobs.push(FlexLevelJob::TextRuns(runs));
            self.has_text_runs = true;
        }
    }

    fn finish(mut self) -> FlexContainer {
        self.wrap_any_text_in_anonymous_block_container();

        let anonymous_style = if self.has_text_runs {
            Some(
                self.context
                    .shared_context()
                    .stylist
                    .style_for_anonymous::<Node::ConcreteElement>(
                        &self.context.shared_context().guards,
                        &style::selector_parser::PseudoElement::ServoText,
                        &self.info.style,
                    ),
            )
        } else {
            None
        };

        let mut children = std::mem::take(&mut self.jobs)
            .into_par_iter()
            .map(|job| match job {
                FlexLevelJob::TextRuns(runs) => ArcRefCell::new(FlexLevelBox::FlexItem(
                    IndependentFormattingContext::construct_for_text_runs(
                        self.context,
                        &self
                            .info
                            .new_replacing_style(anonymous_style.clone().unwrap()),
                        runs.into_iter().map(|run| crate::flow::inline::TextRun {
                            tag: Tag::from_node_and_style_info(&run.info),
                            text: run.text.into(),
                            parent_style: run.info.style,
                        }),
                        ContentSizesRequest::None, // FIXME: request sizes when we start using them
                        self.text_decoration_line,
                    ),
                )),
                FlexLevelJob::Element {
                    info,
                    display,
                    contents,
                    box_slot,
                } => {
                    let display_inside = match display {
                        DisplayGeneratingBox::OutsideInside { inside, .. } => inside,
                    };
                    let box_ = if info.style.get_box().position.is_absolutely_positioned() {
                        // https://drafts.csswg.org/css-flexbox/#abspos-items
                        ArcRefCell::new(FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(Arc::new(
                            AbsolutelyPositionedBox::construct(
                                self.context,
                                &info,
                                display_inside,
                                contents,
                            ),
                        )))
                    } else {
                        ArcRefCell::new(FlexLevelBox::FlexItem(
                            IndependentFormattingContext::construct(
                                self.context,
                                &info,
                                display_inside,
                                contents,
                                ContentSizesRequest::None, // FIXME: request sizes when we start using them
                                self.text_decoration_line,
                            ),
                        ))
                    };
                    box_slot.set(LayoutBox::FlexLevel(box_.clone()));
                    box_
                },
            })
            .collect::<Vec<_>>();

        // https://drafts.csswg.org/css-flexbox/#order-modified-document-order
        children.sort_by_key(|child| match &*child.borrow() {
            FlexLevelBox::FlexItem(item) => item.style.clone_order(),

            // “Absolutely-positioned children of a flex container are treated
            //  as having order: 0 for the purpose of determining their painting order
            //  relative to flex items.”
            FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(_) => 0,
        });

        FlexContainer { children }
    }
}

impl FlexContainer {
    pub(crate) fn layout(
        &self,
        layout_context: &LayoutContext,
        positioning_context: &mut PositioningContext,
        containing_block: &ContainingBlock,
        tree_rank: usize,
    ) -> IndependentLayout {
        // FIXME
        let _ = layout_context;
        let _ = positioning_context;
        let _ = containing_block;
        let _ = tree_rank;
        IndependentLayout {
            fragments: Vec::new(),
            content_block_size: Length::zero(),
        }
    }
}