From 65fce97f5114c97d52ea81504926b98cea3525a3 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 15 Aug 2014 16:29:39 -0700 Subject: [PATCH] Prevent margin collapse in the root flow According to the CSS specification, section 8.3.1, margins of the root element's box do not collapse. Ensure that root flow margins do not collapse during the assign heights phase. --- src/components/layout/block.rs | 8 ++++++-- src/test/ref/basic.list | 1 + src/test/ref/root_margin_collapse_a.html | 5 +++++ src/test/ref/root_margin_collapse_b.html | 5 +++++ 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/test/ref/root_margin_collapse_a.html create mode 100644 src/test/ref/root_margin_collapse_b.html diff --git a/src/components/layout/block.rs b/src/components/layout/block.rs index 50dfd7e3a3e..ea65756c7de 100644 --- a/src/components/layout/block.rs +++ b/src/components/layout/block.rs @@ -760,7 +760,7 @@ impl BlockFlow { } /// If this is the root flow, shifts all kids down and adjusts our size to account for - /// collapsed margins. + /// root flow margins, which should never be collapsed according to CSS § 8.3.1. /// /// TODO(#2017, pcwalton): This is somewhat inefficient (traverses kids twice); can we do /// better? @@ -770,7 +770,7 @@ impl BlockFlow { } let (block_start_margin_value, block_end_margin_value) = match self.base.collapsible_margins { - MarginsCollapseThrough(margin) => (Au(0), margin.collapse()), + MarginsCollapseThrough(_) => fail!("Margins unexpectedly collapsed through root flow."), MarginsCollapse(block_start_margin, block_end_margin) => { (block_start_margin.collapse(), block_end_margin.collapse()) } @@ -1581,6 +1581,10 @@ impl Flow for BlockFlow { } else if self.is_float() { debug!("assign_block_size_float: assigning block_size for float"); self.assign_block_size_float(ctx); + } else if self.is_root() { + // Root element margins should never be collapsed according to CSS § 8.3.1. + debug!("assign_block_size: assigning block_size for root flow"); + self.assign_block_size_block_base(ctx, MarginsMayNotCollapse); } else { debug!("assign_block_size: assigning block_size for block"); self.assign_block_size_block_base(ctx, MarginsMayCollapse); diff --git a/src/test/ref/basic.list b/src/test/ref/basic.list index 3a36b86aa4f..058e567068a 100644 --- a/src/test/ref/basic.list +++ b/src/test/ref/basic.list @@ -93,3 +93,4 @@ flaky_cpu == linebreak_simple_a.html linebreak_simple_b.html == line_height_a.html line_height_ref.html == block_replaced_content_a.html block_replaced_content_ref.html == block_replaced_content_b.html block_replaced_content_ref.html +== root_margin_collapse_a.html root_margin_collapse_b.html diff --git a/src/test/ref/root_margin_collapse_a.html b/src/test/ref/root_margin_collapse_a.html new file mode 100644 index 00000000000..a3c98252be0 --- /dev/null +++ b/src/test/ref/root_margin_collapse_a.html @@ -0,0 +1,5 @@ + + +
+ + diff --git a/src/test/ref/root_margin_collapse_b.html b/src/test/ref/root_margin_collapse_b.html new file mode 100644 index 00000000000..d7bdcf4e536 --- /dev/null +++ b/src/test/ref/root_margin_collapse_b.html @@ -0,0 +1,5 @@ + + +
+ +