[LayoutReloaded] Decouple formatting state and context lifetime.
authorzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Mar 2018 15:38:54 +0000 (15:38 +0000)
committerzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Mar 2018 15:38:54 +0000 (15:38 +0000)
https://bugs.webkit.org/show_bug.cgi?id=184136

Reviewed by Antti Koivisto.

FormattingContext is about the layout logic. We don't need to hold on to it.

* LayoutReloaded/FormattingContext/FloatingContext.js:
(FloatingContext):
(FloatingContext.prototype.computePosition):
(FloatingContext.prototype._positionForClear):
(FloatingContext.prototype._computePositionToAvoidIntrudingFloats):
(FloatingContext.prototype._addFloatingBox):
(FloatingContext.prototype._formattingContext):
(FloatingContext.prototype._formattingState):
* LayoutReloaded/FormattingContext/FormattingContext.js:
(FormattingContext):
* LayoutReloaded/FormattingState/BlockFormattingState.js:
(BlockFormattingState):
* LayoutReloaded/FormattingState/FloatingState.js:
(FloatingState):
(FloatingState.prototype.addFloating):
(FloatingState.prototype.formattingState):
(FloatingState.prototype.formattingContext): Deleted.
* LayoutReloaded/FormattingState/FormattingState.js:
(FormattingState):
(FormattingState.prototype.formattingContext): Deleted.
* LayoutReloaded/FormattingState/InlineFormattingState.js:
(InlineFormattingState):
* LayoutReloaded/LayoutState.js:
(LayoutState.prototype.layout):
(LayoutState.prototype.formattingContext):

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

Tools/ChangeLog
Tools/LayoutReloaded/FormattingContext/FloatingContext.js
Tools/LayoutReloaded/FormattingContext/FormattingContext.js
Tools/LayoutReloaded/FormattingState/BlockFormattingState.js
Tools/LayoutReloaded/FormattingState/FloatingState.js
Tools/LayoutReloaded/FormattingState/FormattingState.js
Tools/LayoutReloaded/FormattingState/InlineFormattingState.js
Tools/LayoutReloaded/LayoutState.js

index 00b8317..3c56c12 100644 (file)
@@ -1,3 +1,38 @@
+2018-03-29  Zalan Bujtas  <zalan@apple.com>
+
+        [LayoutReloaded] Decouple formatting state and context lifetime.
+        https://bugs.webkit.org/show_bug.cgi?id=184136
+
+        Reviewed by Antti Koivisto.
+
+        FormattingContext is about the layout logic. We don't need to hold on to it.
+
+        * LayoutReloaded/FormattingContext/FloatingContext.js:
+        (FloatingContext):
+        (FloatingContext.prototype.computePosition):
+        (FloatingContext.prototype._positionForClear):
+        (FloatingContext.prototype._computePositionToAvoidIntrudingFloats):
+        (FloatingContext.prototype._addFloatingBox):
+        (FloatingContext.prototype._formattingContext):
+        (FloatingContext.prototype._formattingState):
+        * LayoutReloaded/FormattingContext/FormattingContext.js:
+        (FormattingContext):
+        * LayoutReloaded/FormattingState/BlockFormattingState.js:
+        (BlockFormattingState):
+        * LayoutReloaded/FormattingState/FloatingState.js:
+        (FloatingState):
+        (FloatingState.prototype.addFloating):
+        (FloatingState.prototype.formattingState):
+        (FloatingState.prototype.formattingContext): Deleted.
+        * LayoutReloaded/FormattingState/FormattingState.js:
+        (FormattingState):
+        (FormattingState.prototype.formattingContext): Deleted.
+        * LayoutReloaded/FormattingState/InlineFormattingState.js:
+        (InlineFormattingState):
+        * LayoutReloaded/LayoutState.js:
+        (LayoutState.prototype.layout):
+        (LayoutState.prototype.formattingContext):
+
 2018-03-28  Zalan Bujtas  <zalan@apple.com>
 
         [LayoutReloaded] Introduce FloatingState.
index 960e942..f434d0e 100644 (file)
 
 // All geometry here is absolute to the formatting context's root.
 class FloatingContext {
-    constructor(floatingState) {
+    constructor(floatingState, parentFormattingContext) {
         this.m_floatingState = floatingState;
+        this.m_parentFormattingContext = parentFormattingContext;
     }
 
     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))
@@ -73,7 +74,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 +96,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.
@@ -184,12 +185,23 @@ class FloatingContext {
         return max;
     }
 
+    _addFloatingBox(layoutBox) {
+        // Convert floating box to absolute.
+        let clonedDisplayBox = this._formattingContext().displayBox(layoutBox).clone();
+        clonedDisplayBox.setRect(this._formattingContext().absoluteMarginBox(layoutBox));
+        this._floatingState().addFloating(layoutBox, clonedDisplayBox);
+    }
+
     _floatingState() {
         return this.m_floatingState;
     }
 
     _formattingContext() {
-        return this._floatingState().formattingContext();
+        return this.m_parentFormattingContext;
+    }
+
+    _formattingState() {
+        return this._floatingState().formattingState();
     }
 
     _lastFloating() {
index 732c288..420e339 100644 (file)
@@ -26,7 +26,7 @@
 class FormattingContext {
     constructor(formattingState) {
         this.m_formattingState = formattingState;
-        this.m_floatingContext = new FloatingContext(formattingState.floatingState());
+        this.m_floatingContext = new FloatingContext(formattingState.floatingState(), this);
         this.m_layoutStack = new Array();
     }
 
index df60dd1..07bd90d 100644 (file)
@@ -26,8 +26,7 @@
 class BlockFormattingState extends FormattingState {
     constructor(formattingRoot, layoutState) {
         super(layoutState, formattingRoot);
-        // New block formatting context always establishes a new floating context.
+        // New block formatting state always establishes a new floating state.
         this.m_floatingState = new FloatingState(this);
-        this.m_formattingContext = new BlockFormattingContext(this);
     }
 }
index 17ad1a2..e75498a 100644 (file)
  */
 
 class FloatingState {
-    constructor(formattingState) {
-        this.m_formattingState = formattingState;
+    constructor(parentFormattingState) {
+        this.m_parentFormattingState = parentFormattingState;
         this.m_leftFloatingBoxStack = new Array();
         this.m_rightFloatingBoxStack = new Array();
         this.m_lastFloating = null;
     }
 
-    addFloating(floatingBox) {
-        // Convert floating box to absolute.
-        let floatingDisplayBox = this.formattingContext().displayBox(floatingBox).clone();
-        floatingDisplayBox.setRect(this.formattingContext().absoluteMarginBox(floatingBox));
-        this.m_lastFloating = floatingDisplayBox;
-        if (Utils.isFloatingLeft(floatingBox)) {
-            this.m_leftFloatingBoxStack.push(floatingDisplayBox);
+    addFloating(layoutBox, displayBox) {
+        this.m_lastFloating = displayBox;
+        if (Utils.isFloatingLeft(layoutBox)) {
+            this.m_leftFloatingBoxStack.push(displayBox);
             return;
         }
-        this.m_rightFloatingBoxStack.push(floatingDisplayBox);
+        this.m_rightFloatingBoxStack.push(displayBox);
     }
 
     leftFloatingStack() {
@@ -55,7 +52,7 @@ class FloatingState {
         return this.m_lastFloating;
     }
 
-    formattingContext() {
-        return this.m_formattingState.formattingContext();
+    formattingState() {
+        return this.m_parentFormattingState;
     }
 }
index 2a9eb86..2c696de 100644 (file)
 class FormattingState {
     constructor(layoutState, formattingRoot) {
         this.m_layoutState = layoutState;
-        this.m_formattingContext = null;
         this.m_formattingRoot = formattingRoot;
         this.m_floatingState = null;
         this.m_displayToLayout = new Map();
     }
 
-    formattingContext() {
-        return this.m_formattingContext;
-    }
-
     formattingRoot() {
         return this.m_formattingRoot;
     }
index 25aafff..b9a874e 100644 (file)
@@ -32,7 +32,6 @@ class InlineFormattingState extends FormattingState {
         else {
             // TODO: use parent formatting context's floating state.
         }
-        this.m_formattingContext = new InlineFormattingContext(this);
         this.m_lines = new Array();
     }
 
index 4f05333..b241c1d 100644 (file)
@@ -32,7 +32,7 @@ class LayoutState {
     layout(formattingRoot) {
         let formattingState = this._createFormattingState(formattingRoot);
         this.m_formattingStates.set(formattingRoot, formattingState);
-        formattingState.formattingContext().layout();
+        this.formattingContext(formattingState).layout();
     }
 
     _createFormattingState(formattingRoot) {
@@ -45,6 +45,15 @@ class LayoutState {
         return null;
     }
 
+    formattingContext(formattingState) {
+        if (formattingState instanceof BlockFormattingState)
+            return new BlockFormattingContext(formattingState);
+        if (formattingState instanceof InlineFormattingState)
+            return new InlineFormattingContext(formattingState);
+        ASSERT_NOT_REACHED();
+        return null;
+    }
+
     formattingStates() {
         return this.m_formattingStates;
     }