[LFC][Floating] Add support for negative clearance.
authorzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Aug 2018 02:59:51 +0000 (02:59 +0000)
committerzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Aug 2018 02:59:51 +0000 (02:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=188555

Reviewed by Simon Fraser.

Source/WebCore:

1. Compute clearance to avoid float(s) (border box needs to avoid floats)
2. Reset vertical margins to non-collapsed values.
4. Adjust clearance with the new margins.
5. Take the adjusted clearance and move the box vertically if needed.

Test: fast/block/block-only/margin-collapse-with-clearance.html

* layout/FloatingContext.cpp:
(WebCore::Layout::FloatingContext::verticalPositionWithClearance const):
* layout/FloatingState.cpp:
(WebCore::Layout::FloatingState::FloatItem::FloatItem):
* layout/FormattingContext.cpp:
(WebCore::Layout::FormattingContext::mapBoxToAncestor):
(WebCore::Layout::FormattingContext::mapTopLeftToAncestor):
(WebCore::Layout::FormattingContext::mapCoordinateToAncestor):
(WebCore::Layout::FormattingContext::mapToAncestor): Deleted.
* layout/FormattingContext.h:
* layout/LayoutUnits.h:
(WebCore::Layout::Position::Position):
(WebCore::Layout::Position::moveBy):
* layout/displaytree/DisplayBox.h:

Tools:

* LayoutReloaded/misc/LFC-passing-tests.txt:

LayoutTests:

* fast/block/block-only/margin-collapse-with-clearance-expected.txt: Added.
* fast/block/block-only/margin-collapse-with-clearance.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@234878 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/block/block-only/margin-collapse-with-clearance-expected.txt [new file with mode: 0644]
LayoutTests/fast/block/block-only/margin-collapse-with-clearance.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/layout/FloatingContext.cpp
Source/WebCore/layout/FloatingState.cpp
Source/WebCore/layout/FormattingContext.cpp
Source/WebCore/layout/FormattingContext.h
Source/WebCore/layout/LayoutUnits.h
Source/WebCore/layout/displaytree/DisplayBox.h
Tools/ChangeLog
Tools/LayoutReloaded/misc/LFC-passing-tests.txt

index 796d433..822ac2e 100644 (file)
@@ -1,3 +1,13 @@
+2018-08-14  Zalan Bujtas  <zalan@apple.com>
+
+        [LFC][Floating] Add support for negative clearance.
+        https://bugs.webkit.org/show_bug.cgi?id=188555
+
+        Reviewed by Simon Fraser.
+
+        * fast/block/block-only/margin-collapse-with-clearance-expected.txt: Added.
+        * fast/block/block-only/margin-collapse-with-clearance.html: Added.
+
 2018-08-14  Ansh Shukla  <ansh_shukla@apple.com>
 
         NSURLAuthenticationMethodOAuth challenges are surfaced to clients in -didReceiveAuthenticationChallenge as NSURLAuthenticationMethodDefault
diff --git a/LayoutTests/fast/block/block-only/margin-collapse-with-clearance-expected.txt b/LayoutTests/fast/block/block-only/margin-collapse-with-clearance-expected.txt
new file mode 100644 (file)
index 0000000..767ec80
--- /dev/null
@@ -0,0 +1,33 @@
+layer at (0,0) size 800x600
+  RenderView at (0,0) size 800x600
+layer at (0,0) size 800x450
+  RenderBlock {HTML} at (0,0) size 800x450
+    RenderBody {BODY} at (8,8) size 784x434
+      RenderBlock {DIV} at (0,0) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (1,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,45) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,62) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (449,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,45) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,124) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,43) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,186) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (1,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (449,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,45) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,248) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (1,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,45) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,310) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (449,23) size 52x22 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,45) size 12x12 [border: (1px solid #000000)]
+      RenderBlock {DIV} at (0,372) size 502x62 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,1) size 12x12 [border: (1px solid #000000)]
+        RenderBlock (floating) {DIV} at (1,23) size 52x7 [border: (1px solid #000000)]
+        RenderBlock {DIV} at (1,30) size 12x12 [border: (1px solid #000000)]
diff --git a/LayoutTests/fast/block/block-only/margin-collapse-with-clearance.html b/LayoutTests/fast/block/block-only/margin-collapse-with-clearance.html
new file mode 100644 (file)
index 0000000..baf64cb
--- /dev/null
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html>
+<head>
+<style>
+div {
+       border: 1px solid black;
+}
+</style>
+</head>
+<body>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: left; width: 50px; height: 20px;"></div>
+       <div style="clear: left; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: right; width: 50px; height: 20px;"></div>
+       <div style="clear: right; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="clear: both; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: left; width: 50px; height: 20px;"></div>
+       <div style="float: right; width: 50px; height: 20px;"></div>
+       <div style="clear: both; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: left; width: 50px; height: 20px;"></div>
+       <div style="clear: both; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: right; width: 50px; height: 20px;"></div>
+       <div style="clear: both; width: 10px; height: 10px; margin-top: 30px"></div>
+</div>
+<div style="width: 500px; height: 60px;">
+       <div style="width: 10px; height: 10px; margin-bottom: 10px"></div>
+       <div style="float: left; width: 50px; height: 5px;"></div>
+       <div style="clear: left; width: 10px; height: 10px; margin-top: 10px"></div>
+</div>
+</body>
+</html>
\ No newline at end of file
index 1f2a5d2..f0ef389 100644 (file)
@@ -1,3 +1,32 @@
+2018-08-14  Zalan Bujtas  <zalan@apple.com>
+
+        [LFC][Floating] Add support for negative clearance.
+        https://bugs.webkit.org/show_bug.cgi?id=188555
+
+        Reviewed by Simon Fraser.
+
+        1. Compute clearance to avoid float(s) (border box needs to avoid floats)
+        2. Reset vertical margins to non-collapsed values.
+        4. Adjust clearance with the new margins.
+        5. Take the adjusted clearance and move the box vertically if needed.
+
+        Test: fast/block/block-only/margin-collapse-with-clearance.html
+
+        * layout/FloatingContext.cpp:
+        (WebCore::Layout::FloatingContext::verticalPositionWithClearance const):
+        * layout/FloatingState.cpp:
+        (WebCore::Layout::FloatingState::FloatItem::FloatItem):
+        * layout/FormattingContext.cpp:
+        (WebCore::Layout::FormattingContext::mapBoxToAncestor):
+        (WebCore::Layout::FormattingContext::mapTopLeftToAncestor):
+        (WebCore::Layout::FormattingContext::mapCoordinateToAncestor):
+        (WebCore::Layout::FormattingContext::mapToAncestor): Deleted.
+        * layout/FormattingContext.h:
+        * layout/LayoutUnits.h:
+        (WebCore::Layout::Position::Position):
+        (WebCore::Layout::Position::moveBy):
+        * layout/displaytree/DisplayBox.h:
+
 2018-08-14  Fujii Hironori  <Hironori.Fujii@sony.com>
 
         Unreviewed, rolling out r234859.
index 340a7e1..4feaea2 100644 (file)
@@ -150,13 +150,40 @@ std::optional<LayoutUnit> FloatingContext::verticalPositionWithClearance(const B
         // 1. The amount necessary to place the border edge of the block even with the bottom outer edge of the lowest float that is to be cleared.
         // 2. The amount necessary to place the top border edge of the block at its hypothetical position.
 
-        auto absoluteDisplayBox = FormattingContext::mapToAncestor(layoutContext(), layoutBox, downcast<Container>(m_floatingState.root()));
-        if (floatBottom <= absoluteDisplayBox.rectWithMargin().top())
+        auto& layoutContext = this->layoutContext();
+        auto& displayBox = *layoutContext.displayBoxForLayoutBox(layoutBox);
+        auto rootRelativeTop = FormattingContext::mapTopLeftToAncestor(layoutContext, layoutBox, downcast<Container>(m_floatingState.root())).y;
+        auto clearance = *floatBottom - rootRelativeTop;
+        if (clearance <= 0)
             return { };
 
+        // Clearance inhibits margin collapsing. Let's reset the relevant adjoining margins.
+        if (auto* previousInFlowSibling = layoutBox.previousInFlowSibling()) {
+            auto& previousInFlowDisplayBox = *layoutContext.displayBoxForLayoutBox(*previousInFlowSibling);
+
+            // Since the previous inflow sibling has already been laid out, its margin is collapsed by now.
+            ASSERT(!previousInFlowDisplayBox.marginBottom());
+            auto collapsedMargin = displayBox.marginTop();
+
+            // Reset previous bottom and current top margins to non-collapsing.
+            previousInFlowDisplayBox.setVerticalMargin({ previousInFlowDisplayBox.marginTop(), previousInFlowDisplayBox.nonCollapsedMarginBottom() });
+            displayBox.setVerticalMargin({ displayBox.nonCollapsedMarginTop(), displayBox.marginBottom() });
+
+            auto nonCollapsedMargin = previousInFlowDisplayBox.marginBottom() + displayBox.marginTop();
+            auto marginOffset = nonCollapsedMargin - collapsedMargin;
+            // Move the box to the position where it would be with non-collapsed margins.
+            rootRelativeTop += marginOffset;
+
+            // Having negative clearance is also normal. It just means that the box with the non-collapsed margins is now lower than it needs to be.
+            clearance -= marginOffset;
+        }
+        // Now adjust the box's position with the clearance.
+        rootRelativeTop += clearance;
+        ASSERT(*floatBottom == rootRelativeTop);
+
         // The return vertical position is in the containing block's coordinate system.
-        auto* containingBlockDisplayBox = layoutContext().displayBoxForLayoutBox(*layoutBox.containingBlock());
-        return *floatBottom - containingBlockDisplayBox->top();
+        auto containingBlockRootRelativeTop = FormattingContext::mapTopLeftToAncestor(layoutContext, *layoutBox.containingBlock(), downcast<Container>(m_floatingState.root())).y;
+        return rootRelativeTop - containingBlockRootRelativeTop;
     };
 
     auto clear = layoutBox.style().clear();
index 91d7c2e..41d3ece 100644 (file)
@@ -41,8 +41,8 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(FloatingState);
 FloatingState::FloatItem::FloatItem(const Box& layoutBox, const FloatingState& floatingState)
     : m_layoutBox(makeWeakPtr(const_cast<Box&>(layoutBox)))
     , m_containingBlock(makeWeakPtr(const_cast<Container&>(*layoutBox.containingBlock())))
-    , m_absoluteDisplayBox(FormattingContext::mapToAncestor(floatingState.layoutContext(), layoutBox, downcast<Container>(floatingState.root())))
-    , m_containingBlockAbsoluteDisplayBox(FormattingContext::mapToAncestor(floatingState.layoutContext(), *m_containingBlock, downcast<Container>(floatingState.root())))
+    , m_absoluteDisplayBox(FormattingContext::mapBoxToAncestor(floatingState.layoutContext(), layoutBox, downcast<Container>(floatingState.root())))
+    , m_containingBlockAbsoluteDisplayBox(FormattingContext::mapBoxToAncestor(floatingState.layoutContext(), *m_containingBlock, downcast<Container>(floatingState.root())))
 {
 }
 
index 1a05b7a..4163e76 100644 (file)
@@ -142,7 +142,7 @@ void FormattingContext::layoutOutOfFlowDescendants(LayoutContext& layoutContext,
     LOG_WITH_STREAM(FormattingContextLayout, stream << "End: layout out-of-flow descendants -> context: " << &layoutContext << " root: " << &root());
 }
 
-Display::Box FormattingContext::mapToAncestor(const LayoutContext& layoutContext, const Box& layoutBox, const Container& ancestor)
+Display::Box FormattingContext::mapBoxToAncestor(const LayoutContext& layoutContext, const Box& layoutBox, const Container& ancestor)
 {
     ASSERT(layoutBox.isDescendantOf(ancestor));
 
@@ -164,6 +164,27 @@ Display::Box FormattingContext::mapToAncestor(const LayoutContext& layoutContext
     return mappedDisplayBox;
 }
 
+Position FormattingContext::mapTopLeftToAncestor(const LayoutContext& layoutContext, const Box& layoutBox, const Container& ancestor)
+{
+    ASSERT(layoutBox.isDescendantOf(ancestor));
+    return mapCoordinateToAncestor(layoutContext, layoutContext.displayBoxForLayoutBox(layoutBox)->topLeft(), *layoutBox.containingBlock(), ancestor);
+}
+
+Position FormattingContext::mapCoordinateToAncestor(const LayoutContext& layoutContext, Position position, const Container& containingBlock, const Container& ancestor)
+{
+    auto mappedPosition = position;
+    auto* container = &containingBlock;
+    for (; container && container != &ancestor; container = container->containingBlock())
+        mappedPosition.moveBy(layoutContext.displayBoxForLayoutBox(*container)->topLeft());
+
+    if (!container) {
+        ASSERT_NOT_REACHED();
+        return position;
+    }
+
+    return mappedPosition;
+}
+
 #ifndef NDEBUG
 void FormattingContext::validateGeometryConstraintsAfterLayout(const LayoutContext& layoutContext) const
 {
index 57139a5..aeb0e2f 100644 (file)
@@ -59,7 +59,9 @@ public:
     };
     virtual InstrinsicWidthConstraints instrinsicWidthConstraints(LayoutContext&, const Box&) const = 0;
 
-    static Display::Box mapToAncestor(const LayoutContext&, const Box&, const Container& ancestor);
+    static Display::Box mapBoxToAncestor(const LayoutContext&, const Box&, const Container& ancestor);
+    static Position mapTopLeftToAncestor(const LayoutContext&, const Box&, const Container& ancestor);
+    static Position mapCoordinateToAncestor(const LayoutContext&, Position, const Container& containingBlock, const Container& ancestor);
 
 protected:
     struct LayoutPair {
index e2a8375..d39e50a 100644 (file)
@@ -38,9 +38,32 @@ struct Position {
     // FIXME: Use LayoutUnit<Horizontal> to avoid top/left vs. x/y confusion.
     LayoutUnit x; // left
     LayoutUnit y; // top
+
+    Position() = default;
+    Position(LayoutUnit, LayoutUnit);
+    Position(LayoutPoint);
+    void moveBy(LayoutPoint);
     operator LayoutPoint() const { return { x, y }; }
 };
 
+inline Position::Position(LayoutPoint point)
+    : x(point.x())
+    , y(point.y())
+{
+}
+
+inline Position::Position(LayoutUnit x, LayoutUnit y)
+    : x(x)
+    , y(y)
+{
+}
+
+inline void Position::moveBy(LayoutPoint offset)
+{
+    x += offset.x();
+    y += offset.y();
+}
+
 // Margin, border, padding
 struct HorizontalEdges {
     LayoutUnit left;
index f7e0c6b..fd096d3 100644 (file)
@@ -38,6 +38,7 @@ class RenderStyle;
 namespace Layout {
 class BlockFormattingContext;
 class FormattingContext;
+class FloatingContext;
 class LayoutContext;
 }
 
@@ -48,6 +49,7 @@ class Box {
 public:
     friend class Layout::BlockFormattingContext;
     friend class Layout::FormattingContext;
+    friend class Layout::FloatingContext;
     friend class Layout::LayoutContext;
 
     Box(const Box&);
index c5ee2a2..d763dc2 100644 (file)
@@ -1,3 +1,12 @@
+2018-08-14  Zalan Bujtas  <zalan@apple.com>
+
+        [LFC][Floating] Add support for negative clearance.
+        https://bugs.webkit.org/show_bug.cgi?id=188555
+
+        Reviewed by Simon Fraser.
+
+        * LayoutReloaded/misc/LFC-passing-tests.txt:
+
 2018-08-14 Sihui Liu <sihui_liu@apple.com>
 
         Crash in WebKit::filterPreloadHSTSEntry via NetworkProcess::getHostNamesWithHSTSCache
index 51d16c2..5d294a0 100644 (file)
@@ -49,3 +49,4 @@ fast/block/block-only/floating-multiple-lefts.html
 fast/block/block-only/floating-and-next-previous-inflow-with-margin.html
 fast/block/block-only/floating-left-and-right-with-clearance.html
 fast/block/block-only/float-and-siblings-with-margins.html
+fast/block/block-only/margin-collapse-with-clearance.html