servo-layout 0.5.0

A component of the servo web-engine.
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* 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 std::borrow::Cow;
use std::cell::LazyCell;
use std::ops::{ControlFlow, Range};

use icu_properties::BidiClass;
use layout_api::{LayoutNode, SharedSelection};
use servo_base::text::{RangeAny, Utf32CodeUnits};
use style::computed_values::direction::T as Direction;
use style::computed_values::white_space_collapse::T as WhiteSpaceCollapse;
use style::dom::NodeInfo;
use style::selector_parser::PseudoElement;
use unicode_bidi::Level;
use unicode_categories::UnicodeCategories;

use super::text_run::TextRun;
use super::{
    InlineBox, InlineBoxIdentifier, InlineBoxes, InlineFormattingContext, InlineItem,
    SharedInlineStyles,
};
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::{LayoutBox, NodeExt};
use crate::dom_traversal::{BoxTreeString, NodeAndStyleInfo};
use crate::flow::BlockLevelBox;
use crate::flow::float::FloatBox;
use crate::flow::inline::text_transform::{OffsetMap, TextTransformationIterator};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::positioned::AbsolutelyPositionedBox;
use crate::style_ext::ComputedValuesExt;

#[derive(Default)]
pub(crate) struct InlineFormattingContextBuilder {
    /// A stack of [`SharedInlineStyles`] including one for the root, one for each inline box on the
    /// inline box stack, and importantly, one for every `display: contents` element that we are
    /// currently processing. Normally `display: contents` elements don't affect the structure of
    /// the [`InlineFormattingContext`], but the styles they provide do style their children.
    pub shared_inline_styles_stack: Vec<SharedInlineStyles>,

    /// The collection of text strings that make up this [`InlineFormattingContext`] under
    /// construction.
    pub text_segments: Vec<String>,

    /// The current offset in the final text string of this [`InlineFormattingContext`],
    /// used to properly set the text range of new [`InlineItem::TextRun`]s.
    current_text_offset: usize,

    /// The current character offset in the final text string of this [`InlineFormattingContext`],
    /// used to properly set the text range of new [`InlineItem::TextRun`]s. Note that this is
    /// different from the UTF-8 code point offset.
    current_character_offset: usize,

    /// If the [`InlineFormattingContext`] that we are building has a selection shared with its
    /// originating node in the DOM, this will not be `None`.
    pub shared_selection: Option<SharedSelection>,

    /// Whether the last processed node ended with whitespace. This is used to
    /// implement rule 4 of <https://www.w3.org/TR/css-text-3/#collapse>:
    ///
    /// > Any collapsible space immediately following another collapsible space—even one
    /// > outside the boundary of the inline containing that space, provided both spaces are
    /// > within the same inline formatting context—is collapsed to have zero advance width.
    /// > (It is invisible, but retains its soft wrap opportunity, if any.)
    last_inline_box_ended_with_collapsible_white_space: bool,

    /// Whether or not the current state of the inline formatting context is on a word boundary
    /// for the purposes of `text-transform: capitalize`.
    on_word_boundary: bool,

    /// Whether or not this inline formatting context will contain floats.
    pub contains_floats: bool,

    /// The current list of [`InlineItem`]s in this [`InlineFormattingContext`] under
    /// construction. This is stored in a flat list to make it easy to access the last
    /// item.
    pub inline_items: Vec<InlineItem>,

    /// The current [`InlineBox`] tree of this [`InlineFormattingContext`] under construction.
    pub inline_boxes: InlineBoxes,

    /// The ongoing stack of inline boxes stack of the builder.
    ///
    /// Contains all the currently ongoing inline boxes we entered so far.
    /// The traversal is at all times as deep in the tree as this stack is,
    /// which is why the code doesn't need to keep track of the actual
    /// container root (see `handle_inline_level_element`).
    ///
    /// When an inline box ends, it's removed from this stack.
    inline_box_stack: Vec<InlineBoxIdentifier>,

    /// Whether this [`InlineFormattingContextBuilder`] is empty for the purposes of ignoring
    /// during box tree construction. An IFC is empty if it only contains TextRuns with
    /// completely collapsible whitespace. When that happens it can be ignored completely.
    pub is_empty: bool,

    /// Whether or not the `::first-letter` pseudo-element of this inline formatting context
    /// has been processed yet.
    has_processed_first_letter: bool,

    /// Whether or not the inline formatting context under construction has any kind of
    /// right-to-left content such as a character with an RTL character class or a `dir`
    /// attribute specifying right-to-left content.
    pub has_right_to_left_content: bool,

    /// An [`OffsetMap`] used to map selections from their offset before inline formatting
    /// context text transformation to their offsets after transformation.
    pub offset_map: OffsetMap,
}

impl InlineFormattingContextBuilder {
    /// <https://drafts.csswg.org/css-text/#white-space>:
    /// > Except where specified otherwise, white space processing in CSS affects only the document
    /// > white space characters: spaces (U+0020), tabs (U+0009), and segment breaks.
    ///
    /// From <https://github.com/w3c/csswg-drafts/issues/5147#issuecomment-637816669>:
    /// > HTML clearly treats CR, LF, and CRLF as segment breaks.
    ///
    /// Other browsers also consider the form feed character (0x0c) to be document white space, it
    /// seems.
    ///
    /// Taken all together, this is equivalent to the WhatWG Infra Standard's definition of ASCII
    /// white space.
    pub(crate) fn is_document_white_space(character: char) -> bool {
        character.is_ascii_whitespace()
    }

    pub(crate) fn new(info: &NodeAndStyleInfo, context: &LayoutContext) -> Self {
        let has_right_to_left_content = info.style.get_inherited_box().direction == Direction::Rtl;
        Self {
            // For the purposes of `text-transform: capitalize` the start of the IFC is a word boundary.
            on_word_boundary: true,
            is_empty: true,
            shared_inline_styles_stack: vec![SharedInlineStyles::from_info_and_context(
                info, context,
            )],
            shared_selection: info.node.selection(),
            has_right_to_left_content,
            ..Default::default()
        }
    }

    pub(crate) fn currently_processing_inline_box(&self) -> bool {
        !self.inline_box_stack.is_empty()
    }

    fn push_control_character_string(&mut self, string_to_push: &str) {
        self.text_segments.push(string_to_push.to_owned());
        self.current_text_offset += string_to_push.len();

        let new_characters = Utf32CodeUnits::length_of(string_to_push);
        self.current_character_offset += new_characters.0;
        self.offset_map
            .push_range(Utf32CodeUnits(0), new_characters);
    }

    fn shared_inline_styles(&self) -> SharedInlineStyles {
        self.shared_inline_styles_stack
            .last()
            .expect("Should always have at least one SharedInlineStyles")
            .clone()
    }

    pub(crate) fn push_atomic(
        &mut self,
        independent_formatting_context_creator: impl FnOnce()
            -> ArcRefCell<IndependentFormattingContext>,
        old_layout_box: Option<LayoutBox>,
    ) -> InlineItem {
        // If there is an existing undamaged layout box that's compatible, use that.
        let independent_formatting_context = old_layout_box
            .and_then(|layout_box| match layout_box {
                LayoutBox::InlineLevel(InlineItem::Atomic(atomic, ..)) => Some(atomic),
                _ => None,
            })
            .unwrap_or_else(independent_formatting_context_creator);

        let inline_level_box = InlineItem::Atomic(
            independent_formatting_context,
            self.current_text_offset,
            Level::ltr(), /* This will be assigned later if necessary. */
        );
        self.inline_items.push(inline_level_box.clone());
        self.is_empty = false;

        // Push an object replacement character for this atomic, which will ensure that the line breaker
        // inserts a line breaking opportunity here.
        self.push_control_character_string("\u{fffc}");

        self.last_inline_box_ended_with_collapsible_white_space = false;
        self.on_word_boundary = true;

        // Atomics such as images should prevent any following text as being interpreted as the first letter.
        self.has_processed_first_letter = true;

        inline_level_box
    }

    pub(crate) fn push_absolutely_positioned_box(
        &mut self,
        absolutely_positioned_box_creator: impl FnOnce() -> ArcRefCell<AbsolutelyPositionedBox>,
        old_layout_box: Option<LayoutBox>,
    ) -> InlineItem {
        let absolutely_positioned_box = old_layout_box
            .and_then(|layout_box| match layout_box {
                LayoutBox::InlineLevel(InlineItem::OutOfFlowAbsolutelyPositionedBox(
                    positioned_box,
                    ..,
                )) => Some(positioned_box),
                _ => None,
            })
            .unwrap_or_else(absolutely_positioned_box_creator);

        // We cannot just reuse the old inline item, because the `current_text_offset` may have changed.
        let inline_level_box = InlineItem::OutOfFlowAbsolutelyPositionedBox(
            absolutely_positioned_box,
            self.current_text_offset,
        );

        self.inline_items.push(inline_level_box.clone());
        self.is_empty = false;
        inline_level_box
    }

    pub(crate) fn push_float_box(
        &mut self,
        float_box_creator: impl FnOnce() -> ArcRefCell<FloatBox>,
        old_layout_box: Option<LayoutBox>,
    ) -> InlineItem {
        let inline_level_box = old_layout_box
            .and_then(|layout_box| match layout_box {
                LayoutBox::InlineLevel(inline_item) => Some(inline_item),
                _ => None,
            })
            .unwrap_or_else(|| InlineItem::OutOfFlowFloatBox(float_box_creator()));

        debug_assert!(
            matches!(inline_level_box, InlineItem::OutOfFlowFloatBox(..),),
            "Created float box with incompatible `old_layout_box`"
        );

        self.inline_items.push(inline_level_box.clone());
        self.is_empty = false;
        self.contains_floats = true;
        inline_level_box
    }

    pub(crate) fn push_block_level_box(&mut self, block_level: ArcRefCell<BlockLevelBox>) {
        assert!(self.currently_processing_inline_box());
        self.contains_floats = self.contains_floats || block_level.borrow().contains_floats();
        self.inline_items.push(InlineItem::BlockLevel(block_level));
    }

    pub(crate) fn start_inline_box(
        &mut self,
        inline_box_creator: impl FnOnce() -> ArcRefCell<InlineBox>,
        old_layout_box: Option<LayoutBox>,
    ) -> InlineItem {
        // If there is an existing undamaged layout box that's compatible, use the `InlineBox` within it.
        let inline_box = old_layout_box
            .and_then(|layout_box| match layout_box {
                LayoutBox::InlineLevel(InlineItem::StartInlineBox(inline_box)) => Some(inline_box),
                _ => None,
            })
            .unwrap_or_else(inline_box_creator);

        let borrowed_inline_box = inline_box.borrow();

        let style = &borrowed_inline_box.base.style;
        self.push_control_character_string(style.bidi_control_chars().0);
        self.has_right_to_left_content =
            self.has_right_to_left_content || style.get_inherited_box().direction == Direction::Rtl;

        self.shared_inline_styles_stack
            .push(borrowed_inline_box.shared_inline_styles.clone());
        std::mem::drop(borrowed_inline_box);

        let identifier = self.inline_boxes.start_inline_box(inline_box.clone());
        let inline_item = InlineItem::StartInlineBox(inline_box);
        self.inline_items.push(inline_item.clone());
        self.inline_box_stack.push(identifier);
        self.is_empty = false;
        inline_item
    }

    /// End the ongoing inline box in this [`InlineFormattingContextBuilder`], returning
    /// shared references to all of the box tree items that were created for it. More than
    /// a single box tree items may be produced for a single inline box when that inline
    /// box is split around a block-level element.
    pub(crate) fn end_inline_box(&mut self) {
        let identifier = self
            .inline_box_stack
            .pop()
            .expect("Ended non-existent inline box");
        let inline_level_box = self.inline_boxes.get(&identifier);

        self.shared_inline_styles_stack.pop();
        self.inline_items
            .push(InlineItem::EndInlineBox(inline_level_box.clone()));
        self.inline_boxes.end_inline_box(identifier);
        let bidi_control_chars = inline_level_box.borrow().base.style.bidi_control_chars();
        self.push_control_character_string(bidi_control_chars.1);
    }

    /// This is like [`Self::push_text`], except that it might possibly add an anonymous box if
    ///
    ///  - This inline formatting context has a `::first-letter` style.
    ///  - No anonymous box for `::first-letter` has been added yet.
    ///  - First letter content is detected in this text.
    ///
    /// Note that this should only be used when processing text in block containers.
    pub(crate) fn push_text_with_possible_first_letter<'dom>(
        &mut self,
        text: BoxTreeString<'dom>,
        info: &NodeAndStyleInfo<'dom>,
        container_info: &NodeAndStyleInfo<'dom>,
        layout_context: &LayoutContext,
    ) -> bool {
        let document_selection = info.node.document_selection_in_text_node();
        if self.has_processed_first_letter || !container_info.pseudo_element_chain().is_empty() {
            self.push_text(text, info, document_selection);
            return false;
        }

        let Some(first_letter_info) =
            container_info.with_pseudo_element(layout_context, PseudoElement::FirstLetter)
        else {
            self.push_text(text, info, document_selection);
            return false;
        };

        let first_letter_range = first_letter_range(&text[..]);
        if first_letter_range.is_empty() {
            return false;
        }

        // Push any leading white space first.
        let first_letter_range_u32 = LazyCell::new(|| {
            Utf32CodeUnits::length_of(&text[..first_letter_range.start])..
                Utf32CodeUnits::length_of(&text[..first_letter_range.end])
        });
        if first_letter_range.start != 0 {
            let leading_whitespace_range = 0..first_letter_range.start;
            let leading_whitespace_selection_range =
                document_selection.and_then(|document_selection| {
                    let leading_whitespace_range_u32 = RangeAny {
                        start: None,
                        end: Some(first_letter_range_u32.start),
                    };
                    document_selection.intersect(leading_whitespace_range_u32)
                });

            self.push_text(
                Cow::Borrowed(&text[leading_whitespace_range]).into(),
                info,
                leading_whitespace_selection_range,
            );
        }

        // Push the first-letter text into an anonymous box with the `::first-letter` style.
        let box_slot = first_letter_info.node.box_slot();
        let inline_item = self.start_inline_box(
            || ArcRefCell::new(InlineBox::new(&first_letter_info, layout_context)),
            None,
        );
        box_slot.set(LayoutBox::InlineLevel(inline_item));

        let first_letter_text = Cow::Borrowed(&text[first_letter_range.clone()]);
        let first_letter_selection_range = document_selection.and_then(|document_selection| {
            document_selection
                .intersect((*first_letter_range_u32).clone().into())
                .map(|range| range.map(|offset| offset - first_letter_range_u32.start))
        });
        self.push_text(
            first_letter_text.into(),
            &first_letter_info,
            first_letter_selection_range,
        );
        self.end_inline_box();
        self.has_processed_first_letter = true;

        // Now push the non-first-letter text.
        let remaining_selection_range = document_selection.and_then(|document_selection| {
            let remaining_text_range_u32 = RangeAny {
                start: Some(first_letter_range_u32.end),
                end: document_selection.end,
            };
            document_selection
                .intersect(remaining_text_range_u32)
                .map(|range| range.map(|offset| offset - first_letter_range_u32.end))
        });
        self.push_text(
            Cow::Borrowed(&text[first_letter_range.end..]).into(),
            info,
            remaining_selection_range,
        );

        true
    }

    pub(crate) fn push_text<'dom>(
        &mut self,
        text: BoxTreeString<'dom>,
        info: &NodeAndStyleInfo<'dom>,
        document_selection: Option<RangeAny<Utf32CodeUnits>>,
    ) {
        let bidi_class_map = icu_properties::maps::bidi_class();
        let white_space_collapse = info.style.clone_white_space_collapse();
        let original_size_before = self.offset_map.total_original_size();
        let mut character_count = 0;
        let mut new_text = String::with_capacity(text.len());
        for iteration in TextTransformationIterator::new(
            &text,
            &info.style,
            self.last_inline_box_ended_with_collapsible_white_space,
            self.on_word_boundary,
        ) {
            self.offset_map.push_iteration(&iteration);
            for &character in iteration.characters() {
                character_count += 1;

                // If this character has a strong right-to-left class the new inline formatting context will
                // need to be BiDi-aware. This match is derived from the list of strong right-to-left classes
                // at https://www.unicode.org/reports/tr44/#Bidi_Class_Values.
                self.has_right_to_left_content = self.has_right_to_left_content ||
                    matches!(
                        bidi_class_map.get(character),
                        BidiClass::RightToLeft |
                            BidiClass::ArabicLetter |
                            BidiClass::RightToLeftEmbedding |
                            BidiClass::RightToLeftIsolate |
                            BidiClass::RightToLeftOverride
                    );

                self.is_empty = self.is_empty &&
                    match white_space_collapse {
                        WhiteSpaceCollapse::Collapse => Self::is_document_white_space(character),
                        WhiteSpaceCollapse::PreserveBreaks => {
                            Self::is_document_white_space(character) && character != '\n'
                        },
                        WhiteSpaceCollapse::Preserve | WhiteSpaceCollapse::BreakSpaces => false,
                    };

                new_text.push(character)
            }
        }

        if new_text.is_empty() {
            return;
        }

        let document_selection = document_selection.map(|document_selection| {
            let start = document_selection
                .start
                .map(|offset| self.offset_map.map(offset))
                // Range unbounded at the start: the concrete start is offset zero
                .unwrap_or(Utf32CodeUnits(0));
            let end = document_selection
                .end
                .map(|offset| self.offset_map.map(offset))
                // Range unbounded at the end: the concrete end is the full length
                .unwrap_or(Utf32CodeUnits(character_count));
            original_size_before + start..original_size_before + end
        });

        if let Some(last_character) = new_text.chars().next_back() {
            self.on_word_boundary = last_character.is_whitespace();
            self.last_inline_box_ended_with_collapsible_white_space =
                self.on_word_boundary && white_space_collapse != WhiteSpaceCollapse::Preserve;
        }

        let new_utf8_range = self.current_text_offset..self.current_text_offset + new_text.len();
        self.current_text_offset = new_utf8_range.end;

        let new_character_range =
            self.current_character_offset..self.current_character_offset + character_count;
        self.current_character_offset = new_character_range.end;

        self.text_segments.push(new_text);

        if self
            .try_to_push_text_range_to_previous_text_run(
                info,
                &document_selection,
                &new_utf8_range,
                &new_character_range,
            )
            .is_break()
        {
            return;
        }

        let current_inline_styles = self.shared_inline_styles();
        let box_slot = info.node.is_text_node().then(|| info.node.box_slot());
        let text_run = ArcRefCell::new(TextRun::new(
            info.into(),
            current_inline_styles,
            new_utf8_range,
            new_character_range,
            document_selection.unwrap_or_default(),
            box_slot
                .as_ref()
                .and_then(|box_slot| box_slot.take_layout_box_as_text_run()),
        ));
        self.inline_items
            .push(InlineItem::TextRun(text_run.clone()));

        if let Some(box_slot) = box_slot {
            box_slot.set(LayoutBox::Text(text_run));
        }
    }

    fn try_to_push_text_range_to_previous_text_run(
        &mut self,
        info: &NodeAndStyleInfo,
        new_text_selection: &Option<Range<Utf32CodeUnits>>,
        new_range: &Range<usize>,
        new_character_range: &Range<usize>,
    ) -> ControlFlow<()> {
        // First check to see if the last item was actually a text run.
        let Some(InlineItem::TextRun(text_run_arc)) = self.inline_items.last() else {
            return ControlFlow::Continue(());
        };

        // Currently to merge two text runs the styles need to be the same.
        if !text_run_arc
            .borrow()
            .inline_styles
            .ptr_eq(&self.shared_inline_styles())
        {
            return ControlFlow::Continue(());
        }

        let mut text_run = text_run_arc.borrow_mut();
        if let Some(next_text_selection) = new_text_selection {
            if !text_run.document_selection.is_empty() {
                // If both the new and old text had selections, they are only compatible
                // if the old selection extends to the start of the new selection
                if text_run.document_selection.end.0 == next_text_selection.start.0 {
                    text_run.document_selection.end = next_text_selection.end;
                } else {
                    return ControlFlow::Continue(());
                }
            } else {
                // If only the new part of the text run has a selection, we can use it directly.
                text_run.document_selection = next_text_selection.start..next_text_selection.end;
            }
        }

        text_run.text_range.end = new_range.end;
        text_run.character_range.end = new_character_range.end;

        // If this text node does not have a `TextRun` in the box slot, this means that
        // it is either new or dirty, which means that the entire `TextRun` just extended
        // is dirty as well. In this case, never reuse existing shaping results. Clear
        // all old items to ensure this.
        let box_slot = info.node.box_slot();
        let old_text_run = box_slot.take_layout_box_as_text_run();
        if old_text_run.is_none() {
            text_run.items.clear();
        }

        box_slot.set(LayoutBox::Text(text_run_arc.clone()));
        ControlFlow::Break(())
    }

    pub(crate) fn enter_display_contents(&mut self, shared_inline_styles: SharedInlineStyles) {
        self.shared_inline_styles_stack.push(shared_inline_styles);
    }

    pub(crate) fn leave_display_contents(&mut self) {
        self.shared_inline_styles_stack.pop();
    }

    /// Finish the current inline formatting context, returning [`None`] if the context was empty.
    pub(crate) fn finish(
        self,
        layout_context: &LayoutContext,
        has_first_formatted_line: bool,
        is_single_line_text_input: bool,
        default_bidi_level: Level,
    ) -> Option<InlineFormattingContext> {
        if self.is_empty {
            return None;
        }

        assert!(self.inline_box_stack.is_empty());
        assert_eq!(
            self.offset_map.total_final_size().0,
            self.current_character_offset
        );

        Some(InlineFormattingContext::new_with_builder(
            self,
            layout_context,
            has_first_formatted_line,
            is_single_line_text_input,
            default_bidi_level,
        ))
    }
}

/// Computes the range of the first letter.
///
/// The range includes any preceding punctuation and white space, and any trailing punctuation. Any
/// non-punctuation following the letter/number/symbol of first-letter ends the range. Intervening
/// spaces within trailing punctuation are not supported yet.
///
/// If the resulting range is empty, no compatible first-letter text was found.
///
/// <https://drafts.csswg.org/css-pseudo/#first-letter-pattern>
fn first_letter_range(text: &str) -> Range<usize> {
    enum State {
        /// All characters that precede the `PrecedingWhitespaceAndPunctuation` state.
        Start,
        /// All preceding punctuation and intervening whitepace that precedes the `Lns` state.
        PrecedingPunctuation,
        /// Unicode general category L: letter, N: number and S: symbol
        Lns,
        /// All punctuation (but no whitespace or other characters), that
        /// come after the `Lns` state.
        TrailingPunctuation,
    }

    let mut start = 0;
    let mut state = State::Start;
    for (index, character) in text.char_indices() {
        match &mut state {
            State::Start => {
                if character.is_letter() || character.is_number() || character.is_symbol() {
                    start = index;
                    state = State::Lns;
                } else if character.is_punctuation() {
                    start = index;
                    state = State::PrecedingPunctuation
                }
            },
            State::PrecedingPunctuation => {
                if character.is_letter() || character.is_number() || character.is_symbol() {
                    state = State::Lns;
                } else if !character.is_separator_space() && !character.is_punctuation() {
                    return 0..0;
                }
            },
            State::Lns => {
                // TODO: Implement support for intervening spaces
                // <https://drafts.csswg.org/css-pseudo/#first-letter-pattern>
                if character.is_punctuation() &&
                    !character.is_punctuation_open() &&
                    !character.is_punctuation_dash()
                {
                    state = State::TrailingPunctuation;
                } else {
                    return start..index;
                }
            },
            State::TrailingPunctuation => {
                // TODO: Implement support for intervening spaces
                // <https://drafts.csswg.org/css-pseudo/#first-letter-pattern>
                if character.is_punctuation() &&
                    !character.is_punctuation_open() &&
                    !character.is_punctuation_dash()
                {
                    continue;
                } else {
                    return start..index;
                }
            },
        }
    }

    match state {
        State::Start | State::PrecedingPunctuation => 0..0,
        State::Lns | State::TrailingPunctuation => start..text.len(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn assert_first_letter_eq(text: &str, expected: &str) {
        let range = first_letter_range(text);
        assert_eq!(&text[range], expected);
    }

    #[test]
    fn test_first_letter_range() {
        // All spaces
        assert_first_letter_eq("", "");
        assert_first_letter_eq("  ", "");

        // Spaces and punctuation only
        assert_first_letter_eq("(", "");
        assert_first_letter_eq(" (", "");
        assert_first_letter_eq("( ", "");
        assert_first_letter_eq("()", "");

        // Invalid chars
        assert_first_letter_eq("\u{0903}", "");

        // First letter only
        assert_first_letter_eq("A", "A");
        assert_first_letter_eq(" A", "A");
        assert_first_letter_eq("A ", "A");
        assert_first_letter_eq(" A ", "A");

        // Word
        assert_first_letter_eq("App", "A");
        assert_first_letter_eq(" App", "A");
        assert_first_letter_eq("App ", "A");

        // Preceding punctuation(s), intervening spaces and first letter
        assert_first_letter_eq(r#""A"#, r#""A"#);
        assert_first_letter_eq(r#" "A"#, r#""A"#);
        assert_first_letter_eq(r#""A "#, r#""A"#);
        assert_first_letter_eq(r#"" A"#, r#"" A"#);
        assert_first_letter_eq(r#" "A "#, r#""A"#);
        assert_first_letter_eq(r#"("A"#, r#"("A"#);
        assert_first_letter_eq(r#" ("A"#, r#"("A"#);
        assert_first_letter_eq(r#"( "A"#, r#"( "A"#);
        assert_first_letter_eq(r#"[ ( "A"#, r#"[ ( "A"#);

        // First letter and succeeding punctuation(s)
        // TODO: modify test cases when intervening spaces in succeeding puntuations is supported
        assert_first_letter_eq(r#"A""#, r#"A""#);
        assert_first_letter_eq(r#"A" "#, r#"A""#);
        assert_first_letter_eq(r#"A)]"#, r#"A)]"#);
        assert_first_letter_eq(r#"A" )]"#, r#"A""#);
        assert_first_letter_eq(r#"A)] >"#, r#"A)]"#);

        // All
        assert_first_letter_eq(r#" ("A" )]"#, r#"("A""#);
        assert_first_letter_eq(r#" ("A")] >"#, r#"("A")]"#);

        // Non ASCII chars
        assert_first_letter_eq("", "");
        assert_first_letter_eq("", "");
        assert_first_letter_eq("一二三", "");
        assert_first_letter_eq(" 一二三 ", "");
        assert_first_letter_eq("(一二三)", "(一");
        assert_first_letter_eq(" (一二三) ", "(一");
        assert_first_letter_eq("((一", "((一");
        assert_first_letter_eq(" ( (一", "( (一");
        assert_first_letter_eq("一)", "一)");
        assert_first_letter_eq("一))", "一))");
        assert_first_letter_eq("一) )", "一)");
    }
}