[New Multicolumn] Don't destroy all the renderers when a multi-column block stops being multi-column (and vice versa)
https://bugs.webkit.org/show_bug.cgi?id=127584

Make the logic for when you need columns and when you don't shared between the
old multi-column code and the new multi-column code. Make sure that the flow thread
and sets get created lazily and destroyed on-demand when whether or not we should
have multiple columns changes.

Reviewed by Beth Dakin.

* rendering/RenderBlock.cpp:
(WebCore::RenderBlock::updateLogicalWidthAndColumnWidth):
No longer virtual. The new column code now uses this function too.

(WebCore::RenderBlock::availableLogicalWidth):
Renamed desiredColumnWidth() to computedColumnWidth().

(WebCore::RenderBlock::computeColumnCountAndWidth):
Renamed calcColumnWidth to computeColumnCountAndWidth.

(WebCore::RenderBlock::setComputedColumnCountAndWidth):
Rename setDesiredColumnCountAndWidth to computed.

(WebCore::RenderBlock::computedColumnWidth):
(WebCore::RenderBlock::computedColumnCount):
Renamed desiredColumnWidth/Count to computedColumnWidth/Count and made them virtual.

(WebCore::RenderBlock::updateFirstLetterStyle):
desired -> computed rename.

* rendering/RenderBlock.h:
Renames and made a few functions virtual so that RenderBlockFlow can override.

* rendering/RenderBlockFlow.cpp:
(WebCore::RenderBlockFlow::RenderBlockFlow):
Don't create the flow thread at construction time any longer.

(WebCore::RenderBlockFlow::createMultiColumnFlowThread):
(WebCore::RenderBlockFlow::destroyMultiColumnFlowThread):
The methods to create and destroy flow threads. These work at any time now and will
fix up the render tree accordingly.

(WebCore::RenderBlockFlow::setComputedColumnCountAndWidth):
Virtual override that creates/destroys the new multi-column information as needed.

(WebCore::RenderBlockFlow::computedColumnWidth):
(WebCore::RenderBlockFlow::computedColumnCount):
Overrides to return the cached column width and count from the flow thread.

* rendering/RenderBlockFlow.h:
Has overrides of the virtual functions needed to turn multi-column state on/off and
to hand back computed count/width information.

* rendering/RenderMultiColumnFlowThread.cpp:
* rendering/RenderMultiColumnFlowThread.h:
Removed the algorithm to compute column count and width, since this has been combined
with the old multi-column layout code.

* rendering/RenderView.cpp:
(WebCore::RenderView::computeColumnCountAndWidth):
Renamed desired -> computed.

* rendering/RenderView.h:
Renamed desired -> computed.

* style/StyleResolveTree.cpp:
(WebCore::Style::determineChange):
(WebCore::Style::resolveLocal):
(WebCore::Style::resolveTree):
* style/StyleResolveTree.h:
The Settings argument is no longer needed now that we don't destroy and re-create
the renderer for a block flow if it stops being (or becomes) multi-column.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@162726 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/rendering/RenderBlockFlow.cpp b/Source/WebCore/rendering/RenderBlockFlow.cpp
index fe4e52f..f4f7137 100644
--- a/Source/WebCore/rendering/RenderBlockFlow.cpp
+++ b/Source/WebCore/rendering/RenderBlockFlow.cpp
@@ -103,7 +103,6 @@
 #endif
 {
     setChildrenInline(true);
-    createMultiColumnFlowThreadIfNeeded();
 }
 
 RenderBlockFlow::RenderBlockFlow(Document& document, PassRef<RenderStyle> style)
@@ -114,25 +113,39 @@
 #endif
 {
     setChildrenInline(true);
-    createMultiColumnFlowThreadIfNeeded();
 }
 
 RenderBlockFlow::~RenderBlockFlow()
 {
 }
 
-void RenderBlockFlow::createMultiColumnFlowThreadIfNeeded()
+void RenderBlockFlow::createMultiColumnFlowThread()
 {
-    if ((style().hasAutoColumnCount() && style().hasAutoColumnWidth()) || !document().regionBasedColumnsEnabled())
-        return;
-    
-    setChildrenInline(false);
     RenderMultiColumnFlowThread* flowThread = new RenderMultiColumnFlowThread(document(), RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
     flowThread->initializeStyle();
+    moveAllChildrenTo(flowThread, true);
     RenderBlock::addChild(flowThread);
     setMultiColumnFlowThread(flowThread);
 }
 
+void RenderBlockFlow::destroyMultiColumnFlowThread()
+{
+    // Get the flow thread out of our list.
+    multiColumnFlowThread()->removeFromParent();
+    
+    // Destroy all the multicolumn sets.
+    destroyLeftoverChildren();
+    
+    // Move all the children of the flow thread into our block.
+    multiColumnFlowThread()->moveAllChildrenTo(this, true);
+    
+    // Now destroy the flow thread.
+    multiColumnFlowThread()->destroy();
+    
+    // Clear the multi-column flow thread pointer.
+    setMultiColumnFlowThread(nullptr);
+}
+
 void RenderBlockFlow::insertedIntoTree()
 {
     RenderBlock::insertedIntoTree();
@@ -3519,16 +3532,6 @@
     return multiColumnFlowThread();
 }
 
-
-bool RenderBlockFlow::updateLogicalWidthAndColumnWidth()
-{
-    bool relayoutChildren = RenderBlock::updateLogicalWidthAndColumnWidth();
-    if (multiColumnFlowThread() && multiColumnFlowThread()->computeColumnCountAndWidth())
-        relayoutChildren = true;
-    return relayoutChildren;
-}
-
-
 void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
 {
     if (multiColumnFlowThread())
@@ -3536,7 +3539,6 @@
     RenderBlock::addChild(newChild, beforeChild);
 }
 
-
 void RenderBlockFlow::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
 {
     // If we don't use either of the two column implementations or a flow thread, then bail.
@@ -3578,5 +3580,42 @@
     }
 }
 
+void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
+{
+    if (!document().regionBasedColumnsEnabled())
+        return RenderBlock::setComputedColumnCountAndWidth(count, width);
+    
+    bool destroyColumns = !requiresColumns(count);
+    if (destroyColumns) {
+        if (multiColumnFlowThread())
+            destroyMultiColumnFlowThread();
+    } else {
+        if (!multiColumnFlowThread())
+            createMultiColumnFlowThread();
+        multiColumnFlowThread()->setColumnCountAndWidth(count, width);
+    }
+}
+
+LayoutUnit RenderBlockFlow::computedColumnWidth() const
+{
+    if (!document().regionBasedColumnsEnabled())
+        return RenderBlock::computedColumnWidth();
+    
+    if (multiColumnFlowThread())
+        return multiColumnFlowThread()->computedColumnWidth();
+    return contentLogicalWidth();
+}
+
+unsigned RenderBlockFlow::computedColumnCount() const
+{
+    if (!document().regionBasedColumnsEnabled())
+        return RenderBlock::computedColumnCount();
+    
+    if (multiColumnFlowThread())
+        return multiColumnFlowThread()->computedColumnCount();
+    
+    return 1;
+}
+
 }
 // namespace WebCore