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

Code before:
        info: &'a NodeAndStyleInfo<'dom>,
        propagated_data: PropagatedBoxTreeData,
    ) -> Self {
        ModernContainerBuilder {
            context,
            info,
            anonymous_info: Default::default(),
            propagated_data: propagated_data.disallowing_percentage_table_columns(),
            contiguous_text_runs: Vec::new(),
            jobs: Vec::new(),
            has_text_runs: false,
        }
    }

    fn anonymous_info(&self) -> &NodeAndStyleInfo<'dom> {
        self.anonymous_info.get_or_init(|| {
            self.info
                .with_pseudo_element(self.context, PseudoElement::ServoAnonymousBox)
                .expect("Should always be able to construct info for anonymous boxes.")
        })
    }

    fn wrap_any_text_in_anonymous_block_container(&mut self) {
        let runs = std::mem::take(&mut self.contiguous_text_runs);

        // There is no text run or they all only contain document white space characters, do nothing.
        if runs
            .iter()
            .all(ModernContainerTextRun::is_only_document_white_space)
        {
            return;
        }

        let box_slot = self.anonymous_info().node.box_slot();
        self.jobs.push(ModernContainerJob::TextRuns(runs, box_slot));
        self.has_text_runs = true;
    }

    pub(crate) fn finish(mut self) -> Vec<ModernItem<'dom>> {
        self.wrap_any_text_in_anonymous_block_container();

        let jobs = std::mem::take(&mut self.jobs);
        let mut children: Vec<_> = if self.context.use_rayon {
            jobs.into_par_iter()
                .filter_map(|job| job.finish(&self))
                .collect()
        } else {
            jobs.into_iter()
                .filter_map(|job| job.finish(&self))
                .collect()
        };

Review comment (suggestion):
If you are moving this outside, i would add "If":

```suggestion
        // If there is no text run or they all only contain document white space characters, do nothing.
```

Code after:
        info: &'a NodeAndStyleInfo<'dom>,
        propagated_data: PropagatedBoxTreeData,
    ) -> Self {
        ModernContainerBuilder {
            context,
            info,
            anonymous_info: Default::default(),
            propagated_data: propagated_data.disallowing_percentage_table_columns(),
            contiguous_text_runs: Vec::new(),
            jobs: Vec::new(),
            has_text_runs: false,
        }
    }

    fn anonymous_info(&self) -> &NodeAndStyleInfo<'dom> {
        self.anonymous_info.get_or_init(|| {
            self.info
                .with_pseudo_element(self.context, PseudoElement::ServoAnonymousBox)
                .expect("Should always be able to construct info for anonymous boxes.")
        })
    }

    fn wrap_any_text_in_anonymous_block_container(&mut self) {
        let runs = std::mem::take(&mut self.contiguous_text_runs);

        // If there is no text run or they all only contain document white space
        // characters, do nothing.
        if runs
            .iter()
            .all(ModernContainerTextRun::is_only_document_white_space)
        {
            return;
        }

        let box_slot = self.anonymous_info().node.box_slot();
        self.jobs.push(ModernContainerJob::TextRuns(runs, box_slot));
        self.has_text_runs = true;
    }

    pub(crate) fn finish(mut self) -> Vec<ModernItem<'dom>> {
        self.wrap_any_text_in_anonymous_block_container();

        let jobs = std::mem::take(&mut self.jobs);
        let mut children: Vec<_> = if self.context.use_rayon {
            jobs.into_par_iter()
                .filter_map(|job| job.finish(&self))
                .collect()
        } else {
            jobs.into_iter()
                .filter_map(|job| job.finish(&self))
                .collect()