mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
layout: Implement the correct hypothetical box behavior for
absolutely-positioned elements declared with `display: inline`. Although the computed `display` property of elements with `position: absolute` is `block`, `position: absolute; display: inline` can still behave differently from `position: absolute; display: block`. This is because the hypothetical box for `position: absolute` can be at the position it would have been if it had `display: inline`. CSS 2.1 § 10.3.7 describes this case in a parenthetical: "The static-position containing block is the containing block of a hypothetical box that would have been the first box of the element if its specified 'position' value had been 'static' and its specified 'float' had been 'none'. (Note that due to the rules in section 9.7 this hypothetical calculation might require also assuming a different computed value for 'display'.)" To handle this, I had to change both style computation and layout. For the former, I added an internal property `-servo-display-for-hypothetical-box`, which stores the `display` value supplied by the author, before the computed value is calculated. Flow construction now uses this value. As for layout, implementing the proper behavior is tricky because the position of an inline fragment in the inline direction cannot be determined until height assignment, which is a parallelism hazard because in parallel layout widths are computed before heights. However, in this particular case we can avoid the parallelism hazard because the inline direction of a hypothetical box only affects the layout if an absolutely-positioned element is unconstrained in the inline direction. Therefore, we can just lay out such absolutely-positioned elements with a bogus inline position and fix it up once the true inline position of the hypothetical box is computed. The name for this fix-up process is "late computation of inline position" (and the corresponding fix-up for the block position is called "late computation of block position"). This improves the header on /r/rust.
This commit is contained in:
parent
f800960695
commit
287fe3b3ab
17 changed files with 490 additions and 159 deletions
|
@ -279,7 +279,7 @@ pub trait Flow: fmt::Show + ToString + Sync {
|
|||
self.positioning() == position::absolute || self.is_fixed()
|
||||
}
|
||||
|
||||
/// Return true if this is the root of an Absolute flow tree.
|
||||
/// Return true if this is the root of an absolute flow tree.
|
||||
fn is_root_of_absolute_flow_tree(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
@ -289,6 +289,14 @@ pub trait Flow: fmt::Show + ToString + Sync {
|
|||
false
|
||||
}
|
||||
|
||||
/// Updates the inline position of a child flow during the assign-height traversal. At present,
|
||||
/// this is only used for absolutely-positioned inline-blocks.
|
||||
fn update_late_computed_inline_position_if_necessary(&mut self, inline_position: Au);
|
||||
|
||||
/// Updates the block position of a child flow during the assign-height traversal. At present,
|
||||
/// this is only used for absolutely-positioned inline-blocks.
|
||||
fn update_late_computed_block_position_if_necessary(&mut self, block_position: Au);
|
||||
|
||||
/// Return the dimensions of the containing block generated by this flow for absolutely-
|
||||
/// positioned descendants. For block flows, this is the padding box.
|
||||
fn generated_containing_block_rect(&self) -> LogicalRect<Au> {
|
||||
|
@ -426,6 +434,16 @@ pub trait MutableFlowUtils {
|
|||
|
||||
/// Builds the display lists for this flow.
|
||||
fn build_display_list(self, layout_context: &LayoutContext);
|
||||
|
||||
/// Gathers static block-offsets bubbled up by kids.
|
||||
///
|
||||
/// This essentially gives us offsets of all absolutely positioned direct descendants and all
|
||||
/// fixed descendants, in tree order.
|
||||
///
|
||||
/// This is called in a bottom-up traversal (specifically, the assign-block-size traversal).
|
||||
/// So, kids have their flow origin already set. In the case of absolute flow kids, they have
|
||||
/// their hypothetical box position already set.
|
||||
fn collect_static_block_offsets_from_children(&mut self);
|
||||
}
|
||||
|
||||
pub trait MutableOwnedFlowUtils {
|
||||
|
@ -580,15 +598,15 @@ pub struct Descendants {
|
|||
/// layout.
|
||||
descendant_links: Vec<FlowRef>,
|
||||
|
||||
/// Static y offsets of all descendants from the start of this flow box.
|
||||
pub static_b_offsets: Vec<Au>,
|
||||
/// Static block-direction offsets of all descendants from the start of this flow box.
|
||||
pub static_block_offsets: Vec<Au>,
|
||||
}
|
||||
|
||||
impl Descendants {
|
||||
pub fn new() -> Descendants {
|
||||
Descendants {
|
||||
descendant_links: Vec::new(),
|
||||
static_b_offsets: Vec::new(),
|
||||
static_block_offsets: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -621,7 +639,7 @@ impl Descendants {
|
|||
let descendant_iter = DescendantIter {
|
||||
iter: self.descendant_links.slice_from_mut(0).iter_mut(),
|
||||
};
|
||||
descendant_iter.zip(self.static_b_offsets.slice_from_mut(0).iter_mut())
|
||||
descendant_iter.zip(self.static_block_offsets.slice_from_mut(0).iter_mut())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -752,6 +770,16 @@ pub struct BaseFlow {
|
|||
pub writing_mode: WritingMode,
|
||||
}
|
||||
|
||||
impl fmt::Show for BaseFlow {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f,
|
||||
"CC {}, ADC {}, CADC {}",
|
||||
self.parallel.children_count.load(SeqCst),
|
||||
self.abs_descendants.len(),
|
||||
self.parallel.children_and_absolute_descendant_count.load(SeqCst))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, S: Encoder<E>> Encodable<S, E> for BaseFlow {
|
||||
fn encode(&self, e: &mut S) -> Result<(), E> {
|
||||
e.emit_struct("base", 0, |e| {
|
||||
|
@ -1103,6 +1131,52 @@ impl<'a> MutableFlowUtils for &'a mut Flow + 'a {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect and update static y-offsets bubbled up by kids.
|
||||
///
|
||||
/// This would essentially give us offsets of all absolutely positioned
|
||||
/// direct descendants and all fixed descendants, in tree order.
|
||||
///
|
||||
/// Assume that this is called in a bottom-up traversal (specifically, the
|
||||
/// assign-block-size traversal). So, kids have their flow origin already set.
|
||||
/// In the case of absolute flow kids, they have their hypothetical box
|
||||
/// position already set.
|
||||
fn collect_static_block_offsets_from_children(&mut self) {
|
||||
let mut absolute_descendant_block_offsets = Vec::new();
|
||||
for kid in mut_base(*self).child_iter() {
|
||||
let mut gives_absolute_offsets = true;
|
||||
if kid.is_block_like() {
|
||||
let kid_block = kid.as_block();
|
||||
if kid_block.is_fixed() || kid_block.is_absolutely_positioned() {
|
||||
// It won't contribute any offsets for descendants because it would be the
|
||||
// containing block for them.
|
||||
gives_absolute_offsets = false;
|
||||
// Give the offset for the current absolute flow alone.
|
||||
absolute_descendant_block_offsets.push(
|
||||
kid_block.get_hypothetical_block_start_edge());
|
||||
} else if kid_block.is_positioned() {
|
||||
// It won't contribute any offsets because it would be the containing block
|
||||
// for the descendants.
|
||||
gives_absolute_offsets = false;
|
||||
}
|
||||
}
|
||||
|
||||
if gives_absolute_offsets {
|
||||
let kid_base = mut_base(kid);
|
||||
// Avoid copying the offset vector.
|
||||
let offsets = mem::replace(&mut kid_base.abs_descendants.static_block_offsets,
|
||||
Vec::new());
|
||||
// Consume all the static block-offsets bubbled up by kids.
|
||||
for block_offset in offsets.into_iter() {
|
||||
// The offsets are with respect to the kid flow's fragment. Translate them to
|
||||
// that of the current flow.
|
||||
absolute_descendant_block_offsets.push(
|
||||
block_offset + kid_base.position.start.b);
|
||||
}
|
||||
}
|
||||
}
|
||||
mut_base(*self).abs_descendants.static_block_offsets = absolute_descendant_block_offsets
|
||||
}
|
||||
}
|
||||
|
||||
impl MutableOwnedFlowUtils for FlowRef {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue