mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Anonymous flex item for text directly in a flex container
This commit is contained in:
parent
ec548e849c
commit
67d8aa84d2
3 changed files with 139 additions and 21 deletions
|
@ -6,6 +6,7 @@ use crate::cell::ArcRefCell;
|
||||||
use crate::context::LayoutContext;
|
use crate::context::LayoutContext;
|
||||||
use crate::dom_traversal::{BoxSlot, Contents, NodeExt, NonReplacedContents, TraversalHandler};
|
use crate::dom_traversal::{BoxSlot, Contents, NodeExt, NonReplacedContents, TraversalHandler};
|
||||||
use crate::element_data::LayoutBox;
|
use crate::element_data::LayoutBox;
|
||||||
|
use crate::flow::inline::TextRun;
|
||||||
use crate::formatting_contexts::{IndependentFormattingContext, IndependentLayout};
|
use crate::formatting_contexts::{IndependentFormattingContext, IndependentLayout};
|
||||||
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
|
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
|
||||||
use crate::sizing::{BoxContentSizes, ContentSizes, ContentSizesRequest};
|
use crate::sizing::{BoxContentSizes, ContentSizes, ContentSizesRequest};
|
||||||
|
@ -44,27 +45,34 @@ impl FlexContainer {
|
||||||
propagated_text_decoration_line | style.clone_text_decoration_line();
|
propagated_text_decoration_line | style.clone_text_decoration_line();
|
||||||
let mut builder = FlexContainerBuilder {
|
let mut builder = FlexContainerBuilder {
|
||||||
context,
|
context,
|
||||||
|
node,
|
||||||
|
style,
|
||||||
|
anonymous_style: None,
|
||||||
text_decoration_line,
|
text_decoration_line,
|
||||||
flex_container: Self {
|
contiguous_text_runs: Vec::new(),
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
},
|
|
||||||
};
|
};
|
||||||
contents.traverse(context, node, style, &mut builder);
|
contents.traverse(context, node, style, &mut builder);
|
||||||
let content_sizes = content_sizes.compute(|| {
|
let content_sizes = content_sizes.compute(|| {
|
||||||
// FIXME
|
// FIXME
|
||||||
ContentSizes::zero()
|
ContentSizes::zero()
|
||||||
});
|
});
|
||||||
(builder.flex_container, content_sizes)
|
(builder.finish(), content_sizes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FlexContainerBuilder<'context> {
|
/// https://drafts.csswg.org/css-flexbox/#flex-items
|
||||||
context: &'context LayoutContext<'context>,
|
struct FlexContainerBuilder<'a, Node> {
|
||||||
|
context: &'a LayoutContext<'a>,
|
||||||
|
node: Node,
|
||||||
|
style: &'a Arc<ComputedValues>,
|
||||||
|
anonymous_style: Option<Arc<ComputedValues>>,
|
||||||
text_decoration_line: TextDecorationLine,
|
text_decoration_line: TextDecorationLine,
|
||||||
flex_container: FlexContainer,
|
contiguous_text_runs: Vec<TextRun>,
|
||||||
|
children: Vec<ArcRefCell<FlexLevelBox>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'context, 'dom, Node: 'dom> TraversalHandler<'dom, Node> for FlexContainerBuilder<'context>
|
impl<'a, 'dom, Node: 'dom> TraversalHandler<'dom, Node> for FlexContainerBuilder<'a, Node>
|
||||||
where
|
where
|
||||||
Node: NodeExt<'dom>,
|
Node: NodeExt<'dom>,
|
||||||
{
|
{
|
||||||
|
@ -74,10 +82,11 @@ where
|
||||||
text: Cow<'dom, str>,
|
text: Cow<'dom, str>,
|
||||||
parent_style: &Arc<ComputedValues>,
|
parent_style: &Arc<ComputedValues>,
|
||||||
) {
|
) {
|
||||||
// FIXME
|
self.contiguous_text_runs.push(TextRun {
|
||||||
let _ = node;
|
tag: node.as_opaque(),
|
||||||
let _ = text;
|
parent_style: parent_style.clone(),
|
||||||
let _ = parent_style;
|
text: text.into(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Or pseudo-element
|
/// Or pseudo-element
|
||||||
|
@ -89,6 +98,11 @@ where
|
||||||
contents: Contents,
|
contents: Contents,
|
||||||
box_slot: BoxSlot<'dom>,
|
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();
|
||||||
|
|
||||||
let display_inside = match display {
|
let display_inside = match display {
|
||||||
DisplayGeneratingBox::OutsideInside { inside, .. } => inside,
|
DisplayGeneratingBox::OutsideInside { inside, .. } => inside,
|
||||||
};
|
};
|
||||||
|
@ -104,18 +118,76 @@ where
|
||||||
),
|
),
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
ArcRefCell::new(FlexLevelBox::FlexItem(IndependentFormattingContext::construct(
|
ArcRefCell::new(FlexLevelBox::FlexItem(
|
||||||
|
IndependentFormattingContext::construct(
|
||||||
|
self.context,
|
||||||
|
node,
|
||||||
|
style.clone(),
|
||||||
|
display_inside,
|
||||||
|
contents,
|
||||||
|
ContentSizesRequest::None, // FIXME: request sizes when we start using them
|
||||||
|
self.text_decoration_line,
|
||||||
|
),
|
||||||
|
))
|
||||||
|
};
|
||||||
|
self.children.push(box_.clone());
|
||||||
|
box_slot.set(LayoutBox::FlexLevel(box_))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// https://drafts.csswg.org/css-text/#white-space
|
||||||
|
fn is_only_document_white_space(string: &str) -> bool {
|
||||||
|
// FIXME: is this the right definition? See
|
||||||
|
// https://github.com/w3c/csswg-drafts/issues/5146
|
||||||
|
// https://github.com/w3c/csswg-drafts/issues/5147
|
||||||
|
string
|
||||||
|
.bytes()
|
||||||
|
.all(|byte| matches!(byte, b' ' | b'\n' | b'\t'))
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'dom, Node: 'dom> FlexContainerBuilder<'a, Node>
|
||||||
|
where
|
||||||
|
Node: NodeExt<'dom>,
|
||||||
|
{
|
||||||
|
fn wrap_any_text_in_anonymous_block_container(&mut self) {
|
||||||
|
if self
|
||||||
|
.contiguous_text_runs
|
||||||
|
.iter()
|
||||||
|
.all(|run| is_only_document_white_space(&run.text))
|
||||||
|
{
|
||||||
|
// There is no text run, or they all only contain document white space characters
|
||||||
|
self.contiguous_text_runs.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let context = self.context;
|
||||||
|
let style = self.style;
|
||||||
|
let anonymous_style = self.anonymous_style.get_or_insert_with(|| {
|
||||||
|
context
|
||||||
|
.shared_context()
|
||||||
|
.stylist
|
||||||
|
.style_for_anonymous::<Node::ConcreteElement>(
|
||||||
|
&context.shared_context().guards,
|
||||||
|
&style::selector_parser::PseudoElement::ServoText,
|
||||||
|
style,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
self.children.push(ArcRefCell::new(FlexLevelBox::FlexItem(
|
||||||
|
IndependentFormattingContext::construct_for_text_runs(
|
||||||
self.context,
|
self.context,
|
||||||
node,
|
self.node,
|
||||||
style.clone(),
|
anonymous_style.clone(),
|
||||||
display_inside,
|
self.contiguous_text_runs.drain(..),
|
||||||
contents,
|
|
||||||
ContentSizesRequest::None, // FIXME: request sizes when we start using them
|
ContentSizesRequest::None, // FIXME: request sizes when we start using them
|
||||||
self.text_decoration_line,
|
self.text_decoration_line,
|
||||||
)))
|
),
|
||||||
};
|
)))
|
||||||
self.flex_container.children.push(box_.clone());
|
}
|
||||||
box_slot.set(LayoutBox::FlexLevel(box_))
|
|
||||||
|
fn finish(mut self) -> FlexContainer {
|
||||||
|
self.wrap_any_text_in_anonymous_block_container();
|
||||||
|
FlexContainer {
|
||||||
|
children: self.children,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,30 @@ impl BlockFormattingContext {
|
||||||
};
|
};
|
||||||
(bfc, inline_content_sizes)
|
(bfc, inline_content_sizes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn construct_for_text_runs<'dom>(
|
||||||
|
context: &LayoutContext,
|
||||||
|
runs: impl Iterator<Item = TextRun>,
|
||||||
|
content_sizes: ContentSizesRequest,
|
||||||
|
text_decoration_line: TextDecorationLine,
|
||||||
|
) -> (Self, BoxContentSizes) {
|
||||||
|
// FIXME: do white space collapsing
|
||||||
|
let inline_level_boxes = runs
|
||||||
|
.map(|run| ArcRefCell::new(InlineLevelBox::TextRun(run)))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let ifc = InlineFormattingContext {
|
||||||
|
inline_level_boxes,
|
||||||
|
text_decoration_line,
|
||||||
|
};
|
||||||
|
let content_sizes = content_sizes.compute(|| ifc.inline_content_sizes(context));
|
||||||
|
let contents = BlockContainer::InlineFormattingContext(ifc);
|
||||||
|
let bfc = Self {
|
||||||
|
contents,
|
||||||
|
contains_floats: false,
|
||||||
|
};
|
||||||
|
(bfc, content_sizes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BlockLevelJob<'dom, Node> {
|
struct BlockLevelJob<'dom, Node> {
|
||||||
|
|
|
@ -115,6 +115,28 @@ impl IndependentFormattingContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn construct_for_text_runs<'dom>(
|
||||||
|
context: &LayoutContext,
|
||||||
|
node: impl NodeExt<'dom>,
|
||||||
|
style: Arc<ComputedValues>,
|
||||||
|
runs: impl Iterator<Item = crate::flow::inline::TextRun>,
|
||||||
|
content_sizes: ContentSizesRequest,
|
||||||
|
propagated_text_decoration_line: TextDecorationLine,
|
||||||
|
) -> Self {
|
||||||
|
let (bfc, content_sizes) = BlockFormattingContext::construct_for_text_runs(
|
||||||
|
context,
|
||||||
|
runs,
|
||||||
|
content_sizes,
|
||||||
|
propagated_text_decoration_line,
|
||||||
|
);
|
||||||
|
Self {
|
||||||
|
tag: node.as_opaque(),
|
||||||
|
style,
|
||||||
|
content_sizes,
|
||||||
|
contents: IndependentFormattingContextContents::Flow(bfc),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_replaced(&self) -> Result<&ReplacedContent, NonReplacedIFC> {
|
pub fn as_replaced(&self) -> Result<&ReplacedContent, NonReplacedIFC> {
|
||||||
use self::IndependentFormattingContextContents as Contents;
|
use self::IndependentFormattingContextContents as Contents;
|
||||||
use self::NonReplacedIFC as NR;
|
use self::NonReplacedIFC as NR;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue