[LayoutReloaded] FloatingContext does not need a parent formatting context.
[WebKit-https.git] / Tools / LayoutReloaded / FormattingContext / FloatingContext.js
index 960e942..cfa97ee 100644 (file)
@@ -32,10 +32,10 @@ class FloatingContext {
     computePosition(layoutBox) {
         if (layoutBox.isOutOfFlowPositioned())
             return;
-        let displayBox = this._formattingContext().displayBox(layoutBox);
+        let displayBox = this._formattingState().displayBox(layoutBox);
         if (layoutBox.isFloatingPositioned()) {
             displayBox.setTopLeft(this._positionForFloating(layoutBox));
-            this._floatingState().addFloating(layoutBox);
+            this._addFloatingBox(layoutBox);
             return;
         }
         if (Utils.hasClear(layoutBox))
@@ -57,10 +57,10 @@ class FloatingContext {
     }
 
     _positionForFloating(floatingBox) {
-        let absoluteFloatingBox = this._formattingContext().absoluteMarginBox(floatingBox);
+        let absoluteFloatingBox = this._mapMarginBoxToFormattingRoot(floatingBox);
         if (this._isEmpty())
             return this._adjustedFloatingPosition(floatingBox, absoluteFloatingBox.top());
-        let verticalPosition = Math.max(absoluteFloatingBox.top(), this._lastFloating().top());
+        let verticalPosition = Math.max(absoluteFloatingBox.top(), this._mapDisplayMarginBoxToFormattingRoot(this._lastFloating()).top());
         let spaceNeeded = absoluteFloatingBox.width();
         while (true) {
             let floatingPair = this._findInnerMostLeftAndRight(verticalPosition);
@@ -73,7 +73,7 @@ class FloatingContext {
 
     _positionForClear(layoutBox) {
         ASSERT(Utils.hasClear(layoutBox));
-        let displayBox = this._formattingContext().displayBox(layoutBox);
+        let displayBox = this._formattingState().displayBox(layoutBox);
         if (this._isEmpty())
             return displayBox.topLeft();
 
@@ -95,7 +95,7 @@ class FloatingContext {
 
     _computePositionToAvoidIntrudingFloats(layoutBox) {
         if (!layoutBox.establishesBlockFormattingContext() || this._isEmpty())
-            return this._formattingContext().displayBox(layoutBox).topLeft();
+            return this._formattingState().displayBox(layoutBox).topLeft();
         // The border box of a table, a block-level replaced element, or an element in the normal flow that establishes
         // a new block formatting context (such as an element with 'overflow' other than 'visible') must not overlap the
         // margin box of any floats in the same block formatting context as the element itself.
@@ -115,26 +115,27 @@ class FloatingContext {
         let leftBottom = Number.POSITIVE_INFINITY;
         let rightBottom = Number.POSITIVE_INFINITY;
         if (floatingPair.left)
-            leftBottom = floatingPair.left.bottom();
+            leftBottom = this._mapDisplayMarginBoxToFormattingRoot(floatingPair.left).bottom();
         if (floatingPair.right)
-            rightBottom = floatingPair.right.bottom();
+            rightBottom = this._mapDisplayMarginBoxToFormattingRoot(floatingPair.right).bottom();
         return Math.min(leftBottom, rightBottom);
     }
 
     _availableSpace(containingBlock, floatingPair) {
-        let containingBlockContentBox = this._formattingContext().displayBox(containingBlock);
+        let containingBlockContentBox = this._formattingState().displayBox(containingBlock);
         if (floatingPair.left && floatingPair.right)
             return floatingPair.right.left() - floatingPair.left.right();
-        if (floatingPair.left)
-            return containingBlockContentBox.width() - (floatingPair.left.right() - this._formattingContext().absoluteBorderBox(containingBlock).left());
+        if (floatingPair.left) {
+            return containingBlockContentBox.width() - (this._mapDisplayMarginBoxToFormattingRoot(floatingPair.left).right() - this._mapBorderBoxToFormattingRoot(containingBlock).left());
+        }
         if (floatingPair.right)
-            return floatingPair.right.left();
+            return this._mapDisplayMarginBoxToFormattingRoot(floatingPair.right).left();
         return containingBlockContentBox.width();
     }
 
     _findFloatingAtVerticalPosition(verticalPosition, floatingStack) {
         let index = floatingStack.length;
-        while (--index >= 0 && floatingStack[index].bottom() <= verticalPosition);
+        while (--index >= 0 && this._mapDisplayMarginBoxToFormattingRoot(floatingStack[index]).bottom() <= verticalPosition);
         return index >= 0 ? floatingStack[index] : null;
     }
 
@@ -145,34 +146,35 @@ class FloatingContext {
     _adjustedFloatingPosition(floatingBox, verticalPosition, leftRightFloatings) {
         let containingBlock = floatingBox.containingBlock();
         // Convert all coordinates relative to formatting context's root.
-        let left = this._formattingContext().absoluteContentBox(containingBlock).left();
-        let right = this._formattingContext().absoluteContentBox(containingBlock).right();
+        let left = this._mapContentBoxToFormattingRoot(containingBlock).left();
+        let right = this._mapContentBoxToFormattingRoot(containingBlock).right();
         if (leftRightFloatings) {
             if (leftRightFloatings.left) {
-                let floatingBoxRight = leftRightFloatings.left.right();
+                let floatingBoxRight = this._mapDisplayMarginBoxToFormattingRoot(leftRightFloatings.left).right();
                 if (floatingBoxRight > left)
                     left = floatingBoxRight;
             }
 
             if (leftRightFloatings.right) {
-                let floatingBoxLeft = leftRightFloatings.right.left();
+                let floatingBoxLeft = this._mapDisplayMarginBoxToFormattingRoot(leftRightFloatings.right).left();
                 if (floatingBoxLeft < right)
                     right = floatingBoxLeft;
             }
         }
-        left += this._formattingContext().marginLeft(floatingBox);
-        right -= this._formattingContext().marginRight(floatingBox);
-        verticalPosition += this._formattingContext().marginTop(floatingBox);
+        let floatingDisplayBox = this._formattingState().displayBox(floatingBox);
+        left += floatingDisplayBox.marginLeft();
+        right -= floatingDisplayBox.marginRight();
+        verticalPosition += floatingDisplayBox.marginTop();
         // No convert them back relative to the floatingBox's containing block.
-        let containingBlockLeft = this._formattingContext().absoluteBorderBox(containingBlock).left();
-        let containingBlockTop = this._formattingContext().absoluteBorderBox(containingBlock).top();
+        let containingBlockLeft = this._mapBorderBoxToFormattingRoot(containingBlock).left();
+        let containingBlockTop = this._mapBorderBoxToFormattingRoot(containingBlock).top();
         left -= containingBlockLeft;
         right -= containingBlockLeft;
         verticalPosition -= containingBlockTop;
 
         if (Utils.isFloatingLeft(floatingBox) || !Utils.isFloatingPositioned(floatingBox))
             return new LayoutPoint(verticalPosition, left);
-        return new LayoutPoint(verticalPosition, right - this._formattingContext().displayBox(floatingBox).rect().width());
+        return new LayoutPoint(verticalPosition, right - floatingDisplayBox.rect().width());
     }
 
     _bottom(floatingStack) {
@@ -180,16 +182,46 @@ class FloatingContext {
             return Number.NaN;
         let max = Number.NEGATIVE_INFINITY;
         for (let i = 0; i < floatingStack.length; ++i)
-            max = Math.max(floatingStack[i].bottom(), max);
+            max = Math.max(this._mapDisplayMarginBoxToFormattingRoot(floatingStack[i]).bottom(), max);
         return max;
     }
 
+    _addFloatingBox(layoutBox) {
+        this._floatingState().addFloating(this._formattingState().displayBox(layoutBox), Utils.isFloatingLeft(layoutBox));
+    }
+
+    _mapMarginBoxToFormattingRoot(layoutBox) {
+        ASSERT(layoutBox instanceof Layout.Box);
+        return this._mapDisplayMarginBoxToFormattingRoot(this._formattingState().displayBox(layoutBox));
+    }
+
+    _mapDisplayMarginBoxToFormattingRoot(displayBox) {
+        ASSERT(displayBox instanceof Display.Box);
+        return Utils.marginBox(displayBox, this._formattingState().displayBox(this._formattingRoot()));
+    }
+
+    _mapBorderBoxToFormattingRoot(layoutBox) {
+        let displayBox = this._formattingState().displayBox(layoutBox);
+        let rootDisplayBox = this._formattingState().displayBox(this._formattingRoot());
+        return Utils.borderBox(displayBox, rootDisplayBox);
+    }
+
+    _mapContentBoxToFormattingRoot(layoutBox) {
+        let displayBox = this._formattingState().displayBox(layoutBox);
+        let rootDisplayBox = this._formattingState().displayBox(this._formattingRoot());
+        return Utils.contentBox(displayBox, rootDisplayBox);
+    }
+
     _floatingState() {
         return this.m_floatingState;
     }
 
-    _formattingContext() {
-        return this._floatingState().formattingContext();
+    _formattingRoot() {
+        return this._formattingState().formattingRoot();
+    }
+
+    _formattingState() {
+        return this._floatingState().formattingState();
     }
 
     _lastFloating() {