Skip to content

Commit

Permalink
Store an OpaqueNode in boxes and fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Dec 13, 2019
1 parent 47944a3 commit abc2c15
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 25 deletions.
8 changes: 5 additions & 3 deletions components/layout_2020/dom_traversal.rs
Expand Up @@ -51,7 +51,7 @@ pub(super) trait TraversalHandler<'dom, Node>
where
Node: 'dom,
{
fn handle_text(&mut self, text: String, parent_style: &ServoArc<ComputedValues>);
fn handle_text(&mut self, node: Node, text: String, parent_style: &ServoArc<ComputedValues>);

/// Or pseudo-element
fn handle_element(
Expand All @@ -76,7 +76,7 @@ fn traverse_children_of<'dom, Node>(
let mut next = parent_element.first_child();
while let Some(child) = next {
if let Some(contents) = child.as_text() {
handler.handle_text(contents, &child.style(context));
handler.handle_text(child, contents, &child.style(context));
} else if child.is_element() {
traverse_element(child, context, handler);
}
Expand Down Expand Up @@ -157,7 +157,9 @@ fn traverse_pseudo_element_contents<'dom, Node>(
let mut anonymous_style = None;
for item in items {
match item {
PseudoElementContentItem::Text(text) => handler.handle_text(text, pseudo_element_style),
PseudoElementContentItem::Text(text) => {
handler.handle_text(node, text, pseudo_element_style)
},
PseudoElementContentItem::Replaced(contents) => {
let item_style = anonymous_style.get_or_insert_with(|| {
context
Expand Down
12 changes: 9 additions & 3 deletions components/layout_2020/flow/construct.rs
Expand Up @@ -273,7 +273,7 @@ where
}
}

fn handle_text(&mut self, input: String, parent_style: &Arc<ComputedValues>) {
fn handle_text(&mut self, node: Node, input: String, parent_style: &Arc<ComputedValues>) {
let (leading_whitespace, mut input) = self.handle_leading_whitespace(&input);
if leading_whitespace || !input.is_empty() {
// This text node should be pushed either to the next ongoing
Expand Down Expand Up @@ -330,6 +330,7 @@ where
if let Some(text) = new_text_run_contents {
let parent_style = parent_style.clone();
inlines.push(Arc::new(InlineLevelBox::TextRun(TextRun {
tag: node.as_opaque(),
parent_style,
text,
})))
Expand Down Expand Up @@ -389,6 +390,7 @@ where
// Whatever happened before, all we need to do before recurring
// is to remember this ongoing inline level box.
self.ongoing_inline_boxes_stack.push(InlineBox {
tag: node.as_opaque(),
style: style.clone(),
first_fragment: true,
last_fragment: false,
Expand Down Expand Up @@ -444,6 +446,7 @@ where
.rev()
.map(|ongoing| {
let fragmented = InlineBox {
tag: ongoing.tag,
style: ongoing.style.clone(),
first_fragment: ongoing.first_fragment,
// The fragmented boxes before the block level element
Expand Down Expand Up @@ -648,8 +651,11 @@ where
if let Some(to) = max_assign_in_flow_outer_content_sizes_to {
to.max_assign(&box_content_sizes.outer_inline(&style))
}
let block_level_box =
Arc::new(BlockLevelBox::SameFormattingContextBlock { contents, style });
let block_level_box = Arc::new(BlockLevelBox::SameFormattingContextBlock {
tag: node.as_opaque(),
contents,
style,
});
(block_level_box, contains_floats)
},
BlockLevelCreator::Independent {
Expand Down
9 changes: 9 additions & 0 deletions components/layout_2020/flow/inline.rs
Expand Up @@ -16,6 +16,7 @@ use crate::ContainingBlock;
use app_units::Au;
use gfx::text::text_run::GlyphRun;
use servo_arc::Arc;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::values::computed::{Length, LengthPercentage, Percentage};
use style::values::specified::text::TextAlignKeyword;
Expand All @@ -38,6 +39,7 @@ pub(crate) enum InlineLevelBox {

#[derive(Debug)]
pub(crate) struct InlineBox {
pub tag: OpaqueNode,
pub style: Arc<ComputedValues>,
pub first_fragment: bool,
pub last_fragment: bool,
Expand All @@ -47,6 +49,7 @@ pub(crate) struct InlineBox {
/// https://www.w3.org/TR/css-display-3/#css-text-run
#[derive(Debug)]
pub(crate) struct TextRun {
pub tag: OpaqueNode,
pub parent_style: Arc<ComputedValues>,
pub text: String,
}
Expand All @@ -59,6 +62,7 @@ struct InlineNestingLevelState<'box_tree> {
}

struct PartialInlineBoxFragment<'box_tree> {
tag: OpaqueNode,
style: Arc<ComputedValues>,
start_corner: Vec2<Length>,
padding: Sides<Length>,
Expand Down Expand Up @@ -371,6 +375,7 @@ impl InlineBox {
start_corner += &relative_adjustement(&style, ifc.containing_block)
}
PartialInlineBoxFragment {
tag: self.tag,
style,
start_corner,
padding,
Expand Down Expand Up @@ -398,6 +403,7 @@ impl<'box_tree> PartialInlineBoxFragment<'box_tree> {
at_line_break: bool,
) {
let mut fragment = BoxFragment {
tag: self.tag,
style: self.style.clone(),
children: std::mem::take(&mut nesting_level.fragments_so_far),
content_rect: Rect {
Expand Down Expand Up @@ -465,6 +471,7 @@ fn layout_atomic<'box_tree>(
let fragments = replaced.make_fragments(&atomic.style, size.clone());
let content_rect = Rect { start_corner, size };
BoxFragment {
tag: atomic.tag,
style: atomic.style.clone(),
children: fragments,
content_rect,
Expand Down Expand Up @@ -539,6 +546,7 @@ fn layout_atomic<'box_tree>(
},
};
BoxFragment {
tag: atomic.tag,
style: atomic.style.clone(),
children: independent_layout.fragments,
content_rect,
Expand Down Expand Up @@ -685,6 +693,7 @@ impl TextRun {
ifc.current_nesting_level
.fragments_so_far
.push(Fragment::Text(TextFragment {
tag: self.tag,
parent_style: self.parent_style.clone(),
rect,
ascent: font_ascent.into(),
Expand Down
47 changes: 29 additions & 18 deletions components/layout_2020/flow/mod.rs
Expand Up @@ -18,6 +18,7 @@ use crate::ContainingBlock;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use rayon_croissant::ParallelIteratorExt;
use servo_arc::Arc;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::values::computed::{Length, LengthOrAuto};
use style::Zero;
Expand All @@ -44,6 +45,7 @@ pub(crate) enum BlockContainer {
#[derive(Debug)]
pub(crate) enum BlockLevelBox {
SameFormattingContextBlock {
tag: OpaqueNode,
style: Arc<ComputedValues>,
contents: BlockContainer,
},
Expand Down Expand Up @@ -268,24 +270,27 @@ impl BlockLevelBox {
float_context: Option<&mut FloatContext>,
) -> Fragment {
match self {
BlockLevelBox::SameFormattingContextBlock { style, contents } => {
Fragment::Box(positioning_context.for_maybe_position_relative(
layout_context,
containing_block,
style,
|positioning_context| {
layout_in_flow_non_replaced_block_level(
layout_context,
positioning_context,
containing_block,
style,
NonReplacedContents::SameFormattingContextBlock(contents),
tree_rank,
float_context,
)
},
))
},
BlockLevelBox::SameFormattingContextBlock {
tag,
style,
contents,
} => Fragment::Box(positioning_context.for_maybe_position_relative(
layout_context,
containing_block,
style,
|positioning_context| {
layout_in_flow_non_replaced_block_level(
layout_context,
positioning_context,
containing_block,
*tag,
style,
NonReplacedContents::SameFormattingContextBlock(contents),
tree_rank,
float_context,
)
},
)),
BlockLevelBox::Independent(contents) => {
Fragment::Box(positioning_context.for_maybe_position_relative(
layout_context,
Expand All @@ -294,13 +299,15 @@ impl BlockLevelBox {
|positioning_context| match contents.as_replaced() {
Ok(replaced) => layout_in_flow_replaced_block_level(
containing_block,
contents.tag,
&contents.style,
replaced,
),
Err(non_replaced) => layout_in_flow_non_replaced_block_level(
layout_context,
positioning_context,
containing_block,
contents.tag,
&contents.style,
NonReplacedContents::EstablishesAnIndependentFormattingContext(
non_replaced,
Expand Down Expand Up @@ -339,6 +346,7 @@ fn layout_in_flow_non_replaced_block_level<'a>(
layout_context: &LayoutContext,
positioning_context: &mut PositioningContext<'a>,
containing_block: &ContainingBlock,
tag: OpaqueNode,
style: &Arc<ComputedValues>,
block_level_kind: NonReplacedContents<'a>,
tree_rank: usize,
Expand Down Expand Up @@ -488,6 +496,7 @@ fn layout_in_flow_non_replaced_block_level<'a>(
},
};
BoxFragment {
tag,
style: style.clone(),
children: fragments,
content_rect,
Expand All @@ -503,6 +512,7 @@ fn layout_in_flow_non_replaced_block_level<'a>(
/// https://drafts.csswg.org/css2/visudet.html#inline-replaced-height
fn layout_in_flow_replaced_block_level<'a>(
containing_block: &ContainingBlock,
tag: OpaqueNode,
style: &Arc<ComputedValues>,
replaced: &ReplacedContent,
) -> BoxFragment {
Expand Down Expand Up @@ -536,6 +546,7 @@ fn layout_in_flow_replaced_block_level<'a>(
size,
};
BoxFragment {
tag,
style: style.clone(),
children: fragments,
content_rect,
Expand Down
8 changes: 7 additions & 1 deletion components/layout_2020/flow/root.rs
Expand Up @@ -66,7 +66,13 @@ fn construct_for_root_element<'dom>(
(
ContainsFloats::No,
vec![Arc::new(BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(
AbsolutelyPositionedBox::construct(context, root_element, style, display_inside, contents),
AbsolutelyPositionedBox::construct(
context,
root_element,
style,
display_inside,
contents,
),
))],
)
} else if box_style.float.is_floating() {
Expand Down
4 changes: 4 additions & 0 deletions components/layout_2020/formatting_contexts.rs
Expand Up @@ -13,12 +13,14 @@ use crate::style_ext::DisplayInside;
use crate::ContainingBlock;
use servo_arc::Arc;
use std::convert::TryInto;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::values::computed::Length;

/// https://drafts.csswg.org/css-display/#independent-formatting-context
#[derive(Debug)]
pub(crate) struct IndependentFormattingContext {
pub tag: OpaqueNode,
pub style: Arc<ComputedValues>,

/// If it was requested during construction
Expand Down Expand Up @@ -71,6 +73,7 @@ impl IndependentFormattingContext {
content_sizes,
);
Self {
tag: node.as_opaque(),
style,
content_sizes,
contents: IndependentFormattingContextContents::Flow(bfc),
Expand All @@ -80,6 +83,7 @@ impl IndependentFormattingContext {
Err(replaced) => {
let content_sizes = content_sizes.compute(|| replaced.inline_content_sizes(&style));
Self {
tag: node.as_opaque(),
style,
content_sizes,
contents: IndependentFormattingContextContents::Replaced(replaced),
Expand Down
3 changes: 3 additions & 0 deletions components/layout_2020/fragments.rs
Expand Up @@ -6,6 +6,7 @@ use crate::geom::flow_relative::{Rect, Sides, Vec2};
use gfx::text::glyph::GlyphStore;
use servo_arc::Arc as ServoArc;
use std::sync::Arc;
use style::dom::OpaqueNode;
use style::logical_geometry::WritingMode;
use style::properties::ComputedValues;
use style::values::computed::Length;
Expand All @@ -20,6 +21,7 @@ pub(crate) enum Fragment {
}

pub(crate) struct BoxFragment {
pub tag: OpaqueNode,
pub style: ServoArc<ComputedValues>,
pub children: Vec<Fragment>,

Expand Down Expand Up @@ -55,6 +57,7 @@ pub(crate) struct AnonymousFragment {
}

pub(crate) struct TextFragment {
pub tag: OpaqueNode,
pub parent_style: ServoArc<ComputedValues>,
pub rect: Rect<Length>,
pub ascent: Length,
Expand Down
1 change: 1 addition & 0 deletions components/layout_2020/positioned.rs
Expand Up @@ -471,6 +471,7 @@ impl<'box_tree> HoistedAbsolutelyPositionedBox<'box_tree> {
};

BoxFragment {
tag: self.absolutely_positioned_box.contents.tag,
style: style.clone(),
children: fragments,
content_rect,
Expand Down

0 comments on commit abc2c15

Please sign in to comment.