Skip to content

Commit

Permalink
Implement position: absolute for non-replaced elements.
Browse files Browse the repository at this point in the history
+ Re-implement fixed positioning using the absolute positioning code.
+ Add reftests for absolute positioning and fixed positioning.
+ Refactor assign_widths in BlockFlow to isolate the calculation of
widths and margins.
+ Pass down details of the Containing Block for absolute and fixed flows
during layout. Use it to calculate the static position of absolute flows.
+ Defer calculation of absolute flow dimensions till we build the
display list.
  • Loading branch information
pradeep90 committed Mar 3, 2014
1 parent 478c9bf commit c4d177a
Show file tree
Hide file tree
Showing 15 changed files with 1,204 additions and 195 deletions.
1,008 changes: 864 additions & 144 deletions src/components/main/layout/block.rs

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions src/components/main/layout/box_.rs
Expand Up @@ -605,6 +605,16 @@ impl Box {
specified(padding, content_box_width)
}

pub fn border_and_padding_horiz(&self) -> Au {
self.border.get().left + self.border.get().right + self.padding.get().left
+ self.padding.get().right
}

pub fn border_and_padding_vertical(&self) -> Au {
self.border.get().top + self.border.get().bottom + self.padding.get().top
+ self.padding.get().bottom
}

pub fn noncontent_width(&self) -> Au {
self.noncontent_left() + self.noncontent_right()
}
Expand All @@ -613,6 +623,7 @@ impl Box {
self.noncontent_top() + self.noncontent_bottom()
}

// Return offset from original position because of `position: relative`.
pub fn relative_position(&self, container_block_size: &Size2D<Au>) -> Point2D<Au> {
fn left_right(style: &ComputedValues, block_width: Au) -> Au {
// TODO(ksh8281) : consider RTL(right-to-left) culture
Expand Down Expand Up @@ -651,6 +662,7 @@ impl Box {
rel_pos.y = rel_pos.y + top_bottom(self.style(), container_block_size.height);
}

// Go over the ancestor boxes and add all relative offsets (if any).
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
Expand Down Expand Up @@ -1122,6 +1134,7 @@ impl Box {
// FIXME(pcwalton): This is a bit of an abuse of the logging infrastructure. We
// should have a real `SERVO_DEBUG` system.
debug!("{:?}", {
// This prints a debug border around the border of this box.
let debug_border = SideOffsets2D::new_all_same(Au::from_px(1));

lists.with_mut(|lists| {
Expand Down Expand Up @@ -1432,8 +1445,10 @@ impl Box {
}
}

/// Assigns the appropriate width to this box.
pub fn assign_width(&self,container_width: Au) {
/// Assigns replaced width for this box only if it is replaced content.
///
/// CSS 2.1 § 10.3.2.
pub fn assign_replaced_width_if_necessary(&self,container_width: Au) {
match self.specific {
GenericBox | IframeBox(_) => {
}
Expand Down Expand Up @@ -1469,7 +1484,8 @@ impl Box {
image_box_info.computed_width.set(Some(width));
}
ScannedTextBox(_) => {
// Scanned text boxes will have already had their content_widths assigned by this point.
// Scanned text boxes will have already had their
// content_widths assigned by this point.
let mut position = self.border_box.borrow_mut();
position.get().size.width = position.get().size.width + self.noncontent_width() +
self.noncontent_inline_left() + self.noncontent_inline_right();
Expand All @@ -1478,6 +1494,7 @@ impl Box {
}
}

/// Assign height for image and scanned text boxes.
pub fn assign_height(&self) {
match self.specific {
GenericBox | IframeBox(_) => {
Expand Down Expand Up @@ -1514,6 +1531,8 @@ impl Box {
ScannedTextBox(_) => {
// Scanned text boxes will have already had their widths assigned by this point
let mut position = self.border_box.borrow_mut();
// Scanned text boxes' content heights are calculated by the
// text run scanner during Flow construction.
position.get().size.height
= position.get().size.height + self.noncontent_height()
}
Expand Down
22 changes: 11 additions & 11 deletions src/components/main/layout/construct.rs
Expand Up @@ -410,8 +410,9 @@ impl<'a> FlowConstructor<'a> {
/// Builds a flow for a node with `display: block`. This yields a `BlockFlow` with possibly
/// other `BlockFlow`s or `InlineFlow`s underneath it, depending on whether {ib} splits needed
/// to happen.
fn build_flow_for_block(&mut self, node: &ThreadSafeLayoutNode, is_fixed: bool) -> ~Flow {
let mut flow = ~BlockFlow::from_node(self, node, is_fixed) as ~Flow;
fn build_flow_for_block(&mut self, node: &ThreadSafeLayoutNode, positioning: position::T)
-> ~Flow {
let mut flow = ~BlockFlow::from_node(self, node, positioning) as ~Flow;
self.build_children_of_block_flow(&mut flow, node);
flow
}
Expand All @@ -433,7 +434,7 @@ impl<'a> FlowConstructor<'a> {
-> ConstructionResult {
let mut opt_inline_block_splits = None;
let mut opt_box_accumulator = None;

// Concatenate all the boxes of our kids, creating {ib} splits as necessary.
for kid in node.children() {
match kid.swap_out_construction_result() {
Expand Down Expand Up @@ -636,7 +637,7 @@ impl<'a> PostorderNodeMutTraversal for FlowConstructor<'a> {
#[inline(always)]
fn process(&mut self, node: &ThreadSafeLayoutNode) -> bool {
// Get the `display` property for this node, and determine whether this node is floated.
let (display, float, position) = match node.type_id() {
let (display, float, positioning) = match node.type_id() {
ElementNodeTypeId(_) => {
let style = node.style().get();
(style.Box.get().display, style.Box.get().float, style.Box.get().position)
Expand All @@ -652,7 +653,7 @@ impl<'a> PostorderNodeMutTraversal for FlowConstructor<'a> {
debug!("building flow for node: {:?} {:?}", display, float);

// Switch on display and floatedness.
match (display, float, position) {
match (display, float, positioning) {
// `display: none` contributes no flow construction result. Nuke the flow construction
// results of children.
(display::none, _, _) => {
Expand All @@ -673,12 +674,12 @@ impl<'a> PostorderNodeMutTraversal for FlowConstructor<'a> {
// TODO(pcwalton): Make this only trigger for blocks and handle the other `display`
// properties separately.

(_, _, position::fixed) => {
let flow = self.build_flow_for_block(node, true);
(_, _, position::absolute) | (_, _, position::fixed) => {
let flow = self.build_flow_for_block(node, positioning);
node.set_flow_construction_result(FlowConstructionResult(flow))
}
(_, float::none, _) => {
let flow = self.build_flow_for_block(node, false);
let flow = self.build_flow_for_block(node, positioning);
node.set_flow_construction_result(FlowConstructionResult(flow))
}

Expand Down Expand Up @@ -779,7 +780,7 @@ trait ObjectElement {
/// Returns true if this node has object data that is correct uri.
fn has_object_data(&self) -> bool;

/// Returns the "data" attribute value parsed as a URL
/// Returns the "data" attribute value parsed as a URL
fn get_object_data(&self, base_url: &Url) -> Option<Url>;
}

Expand All @@ -793,7 +794,7 @@ impl<'ln> ObjectElement for ThreadSafeLayoutNode<'ln> {
match self.get_type_and_data() {
(None, Some(uri)) => is_image_data(uri),
_ => false
}
}
}

fn get_object_data(&self, base_url: &Url) -> Option<Url> {
Expand Down Expand Up @@ -854,4 +855,3 @@ fn strip_ignorable_whitespace_from_end(opt_boxes: &mut Option<~[Box]>) {
*opt_boxes = None
}
}

76 changes: 58 additions & 18 deletions src/components/main/layout/flow.rs
Expand Up @@ -16,7 +16,7 @@
///
/// * `BlockFlow`: A flow that establishes a block context. It has several child flows, each of
/// which are positioned according to block formatting context rules (CSS block boxes). Block
/// flows also contain a single `GenericBox` to represent their rendered borders, padding, etc.
/// flows also contain a single box to represent their rendered borders, padding, etc.
/// The BlockFlow at the root of the tree has special behavior: it stretches to the boundaries of
/// the viewport.
///
Expand All @@ -26,7 +26,7 @@
/// similar methods.

use css::node_style::StyledNode;
use layout::block::BlockFlow;
use layout::block::{BlockFlow};
use layout::box_::Box;
use layout::context::LayoutContext;
use layout::display_list_builder::{DisplayListBuilder, ExtraDisplayListData};
Expand Down Expand Up @@ -208,6 +208,7 @@ pub trait MutableFlowUtils {
self,
builder: &DisplayListBuilder,
container_block_size: &Size2D<Au>,
absolute_cb_abs_position: Point2D<Au>,
dirty: &Rect<Au>,
index: uint,
mut list: &RefCell<DisplayListCollection<E>>)
Expand Down Expand Up @@ -498,8 +499,9 @@ pub struct BaseFlow {
min_width: Au,
pref_width: Au,

/// The position of the upper left corner of the border box of this flow, relative to the
/// containing block.
/// The upper left corner of the box representing this flow, relative to
/// the box representing its parent flow.
/// For absolute flows, this represents the position wrt to its Containing Block.
position: Rect<Au>,

/// The amount of overflow of this flow, relative to the containing block. Must include all the
Expand All @@ -514,7 +516,11 @@ pub struct BaseFlow {
/// The floats next to this flow.
floats: Floats,

/// The number of floated descendants of this flow (including this flow, if it's floated).
/// For normal flows, this is the number of floated descendants that are
/// not contained within any other floated descendant of this flow. For
/// floats, it is 1.
/// It is used to allocate float data if necessary and to
/// decide whether to do an in-order traversal for assign_height.
num_floats: uint,

/// The position of this flow in page coordinates, computed during display list construction.
Expand Down Expand Up @@ -707,6 +713,19 @@ impl<'a> MutableFlowUtils for &'a mut Flow {
f(mut_base(self).children.back_mut())
}

/// Calculate and set overflow for current flow.
///
/// CSS Section 11.1
/// This is ideally the union of all flows for which we define the
/// Containing Block.
///
/// Assumption: This is called in a bottom-up traversal, so kids' overflows have
/// already been set.
/// So, currently this is a union of the overflows of all kids and our own
/// flow rectangle.
/// FIXME: Handle the overflow of absolute flow descendants, because their
/// assign-heights happen after the normal
/// assign-height-and-store-overflow traversal
fn store_overflow(self, _: &mut LayoutContext) {
let my_position = mut_base(self).position;
let mut overflow = my_position;
Expand All @@ -723,17 +742,29 @@ impl<'a> MutableFlowUtils for &'a mut Flow {
/// For InlineFlow, add display items for all its boxes onto list`.
/// For BlockFlow, add a ClipDisplayItemClass for itself and its children,
/// plus any other display items like border.
///
/// `container_block_size`: Size of the Containing Block for the current
/// flow. This is used for relative positioning (which resolves percentage
/// values for 'top', etc. after all Containing Block heights have been computed.)
/// `absolute_cb_abs_position`: Absolute position of the Containing Block
/// for the flow if it is absolutely positioned.
fn build_display_lists<E:ExtraDisplayListData>(
self,
builder: &DisplayListBuilder,
container_block_size: &Size2D<Au>,
absolute_cb_abs_position: Point2D<Au>,
dirty: &Rect<Au>,
mut index: uint,
lists: &RefCell<DisplayListCollection<E>>)
-> bool {
debug!("Flow: building display list");
index = match self.class() {
BlockFlowClass => self.as_block().build_display_list_block(builder, container_block_size, dirty, index, lists),
BlockFlowClass => self.as_block().build_display_list_block(builder,
container_block_size,
absolute_cb_abs_position,
dirty,
index,
lists),
InlineFlowClass => self.as_inline().build_display_list_inline(builder, container_block_size, dirty, index, lists),
};

Expand All @@ -745,21 +776,31 @@ impl<'a> MutableFlowUtils for &'a mut Flow {
let mut child_lists = DisplayListCollection::new();
child_lists.add_list(DisplayList::new());
let child_lists = RefCell::new(child_lists);
let container_block_size = match self.class() {
BlockFlowClass => {
if self.as_block().box_.is_some() {
self.as_block().box_.get_ref().border_box.get().size
let is_positioned = self.as_block().is_positioned();
let container_block_size;
let abs_cb_position;
let flow_pos = base(self).abs_position;
match self.as_block().box_ {
Some(ref box_) => {
// FIXME: This should be the size of the content box (which is the
// Containing Block formed by a BlockFlow), not the border box.
container_block_size = box_.border_box.get().size;

abs_cb_position = if is_positioned {
let padding_box_pos = flow_pos + box_.border_box.get().origin
+ Point2D(box_.border.get().left, box_.border.get().top);
padding_box_pos
} else {
base(self).position.size
}
},
_ => {
base(self).position.size
absolute_cb_abs_position
};
}
};
None => fail!("Flow: block container should have a box_")
}

for kid in child_iter(self) {
kid.build_display_lists(builder, &container_block_size, dirty, 0u, &child_lists);
kid.build_display_lists(builder, &container_block_size,
abs_cb_position,
dirty, 0u, &child_lists);
}

let mut child_lists = Some(child_lists.unwrap());
Expand Down Expand Up @@ -828,4 +869,3 @@ impl MutableOwnedFlowUtils for ~Flow {
self_borrowed.destroy();
}
}

17 changes: 7 additions & 10 deletions src/components/main/layout/inline.rs
Expand Up @@ -657,18 +657,13 @@ impl Flow for InlineFlow {
{
let this = &mut *self;
for box_ in this.boxes.iter() {
box_.assign_width(self.base.position.size.width);
box_.assign_replaced_width_if_necessary(self.base.position.size.width);
}
}

// FIXME(ksh8281) avoid copy
let flags_info = self.base.flags_info.clone();
for kid in self.base.child_iter() {
let child_base = flow::mut_base(kid);
child_base.position.size.width = self.base.position.size.width;
child_base.flags_info.flags.set_inorder(self.base.flags_info.flags.inorder());
child_base.flags_info.propagate_text_alignment_from_parent(&flags_info)
}
assert!(self.base.children.len() == 0,
"InlineFlow: should not have children flows in the current layout implementation.");

// There are no child contexts, so stop here.

// TODO(Issue #225): once there are 'inline-block' elements, this won't be
Expand All @@ -685,6 +680,9 @@ impl Flow for InlineFlow {
self.assign_height(ctx);
}

/// Calculate and set the height of this Flow.
///
/// CSS Section 10.6.1
fn assign_height(&mut self, _: &mut LayoutContext) {
debug!("assign_height_inline: assigning height for flow");

Expand Down Expand Up @@ -901,4 +899,3 @@ impl Flow for InlineFlow {
~"InlineFlow: " + self.boxes.map(|s| s.debug_str()).connect(", ")
}
}

0 comments on commit c4d177a

Please sign in to comment.