[RenderTreeBuilder] Move finding-the-parent/creating-wrapper logic from RenderTableSe...
authorzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 Dec 2017 22:15:18 +0000 (22:15 +0000)
committerzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 19 Dec 2017 22:15:18 +0000 (22:15 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180994
<rdar://problem/36139222>

Reviewed by Antti Koivisto.

This is in preparation for removing all tree mutation from renderering code.

Covered by existing tests.

* rendering/RenderTableSection.cpp:
(WebCore::RenderTableSection::addChild):
* rendering/updating/RenderTreeBuilder.cpp:
(WebCore::RenderTreeBuilder::insertChild):
* rendering/updating/RenderTreeBuilderTable.cpp:
(WebCore::RenderTreeBuilder::Table::findOrCreateParentForChild):
* rendering/updating/RenderTreeBuilderTable.h:

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

Source/WebCore/ChangeLog
Source/WebCore/rendering/RenderTableSection.cpp
Source/WebCore/rendering/updating/RenderTreeBuilder.cpp
Source/WebCore/rendering/updating/RenderTreeBuilderTable.cpp
Source/WebCore/rendering/updating/RenderTreeBuilderTable.h

index 7373ede..0370a40 100644 (file)
@@ -1,3 +1,23 @@
+2017-12-19  Zalan Bujtas  <zalan@apple.com>
+
+        [RenderTreeBuilder] Move finding-the-parent/creating-wrapper logic from RenderTableSection::addChild to RenderTreeBuilder
+        https://bugs.webkit.org/show_bug.cgi?id=180994
+        <rdar://problem/36139222>
+
+        Reviewed by Antti Koivisto.
+
+        This is in preparation for removing all tree mutation from renderering code.
+
+        Covered by existing tests.
+
+        * rendering/RenderTableSection.cpp:
+        (WebCore::RenderTableSection::addChild):
+        * rendering/updating/RenderTreeBuilder.cpp:
+        (WebCore::RenderTreeBuilder::insertChild):
+        * rendering/updating/RenderTreeBuilderTable.cpp:
+        (WebCore::RenderTreeBuilder::Table::findOrCreateParentForChild):
+        * rendering/updating/RenderTreeBuilderTable.h:
+
 2017-12-19  Daniel Bates  <dabates@apple.com>
 
         Implement InlineTextBox painting using marker subranges
index 3a465f8..fe2b904 100644 (file)
@@ -123,43 +123,6 @@ void RenderTableSection::willBeRemovedFromTree()
 
 void RenderTableSection::addChild(RenderTreeBuilder& builder, RenderPtr<RenderObject> child, RenderObject* beforeChild)
 {
-    if (!is<RenderTableRow>(*child)) {
-        RenderObject* last = beforeChild;
-        if (!last)
-            last = lastRow();
-        if (is<RenderTableRow>(last) && last->isAnonymous() && !last->isBeforeOrAfterContent()) {
-            RenderTableRow& row = downcast<RenderTableRow>(*last);
-            if (beforeChild == &row)
-                beforeChild = row.firstCell();
-            builder.insertChild(row, WTFMove(child), beforeChild);
-            return;
-        }
-
-        if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
-            RenderObject* row = beforeChild->previousSibling();
-            if (is<RenderTableRow>(row) && row->isAnonymous()) {
-                builder.insertChild(downcast<RenderTableRow>(*row), WTFMove(child));
-                return;
-            }
-        }
-
-        // If beforeChild is inside an anonymous cell/row, insert into the cell or into
-        // the anonymous row containing it, if there is one.
-        RenderObject* lastBox = last;
-        while (lastBox && lastBox->parent()->isAnonymous() && !is<RenderTableRow>(*lastBox))
-            lastBox = lastBox->parent();
-        if (is<RenderTableRow>(lastBox) && lastBox->isAnonymous() && !lastBox->isBeforeOrAfterContent()) {
-            builder.insertChild(downcast<RenderTableRow>(*lastBox), WTFMove(child), beforeChild);
-            return;
-        }
-
-        auto newRow = RenderTableRow::createAnonymousWithParentRenderer(*this);
-        auto& row = *newRow;
-        builder.insertChild(*this, WTFMove(newRow), beforeChild);
-        builder.insertChild(row, WTFMove(child));
-        return;
-    }
-
     if (beforeChild)
         setNeedsCellRecalc();
 
index d5dd281..e1842d8 100644 (file)
@@ -67,6 +67,16 @@ void RenderTreeBuilder::insertChild(RenderElement& parent, RenderPtr<RenderObjec
         return;
     }
 
+    if (is<RenderTableSection>(parent)) {
+        auto& parentCandidate = m_tableBuilder->findOrCreateParentForChild(downcast<RenderTableSection>(parent), *child, beforeChild);
+        if (&parent != &parentCandidate) {
+            insertChild(parentCandidate, WTFMove(child), beforeChild);
+            return;
+        }
+        parent.addChild(*this, WTFMove(child), beforeChild);
+        return;
+    }
+
     if (is<RenderRubyRun>(parent)) {
         rubyRunInsertChild(downcast<RenderRubyRun>(parent), WTFMove(child), beforeChild);
         return;
index d79ffff..60a8db4 100644 (file)
@@ -82,4 +82,39 @@ RenderElement& RenderTreeBuilder::Table::findOrCreateParentForChild(RenderTableR
     return cell;
 }
 
+RenderElement& RenderTreeBuilder::Table::findOrCreateParentForChild(RenderTableSection& parent, const RenderObject& child, RenderObject*& beforeChild)
+{
+    if (is<RenderTableRow>(child))
+        return parent;
+
+    auto* lastChild = beforeChild ? beforeChild : parent.lastRow();
+    if (is<RenderTableRow>(lastChild) && lastChild->isAnonymous() && !lastChild->isBeforeOrAfterContent()) {
+        if (beforeChild == lastChild)
+            beforeChild = downcast<RenderTableRow>(*lastChild).firstCell();
+        return downcast<RenderElement>(*lastChild);
+    }
+
+    if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == &parent) {
+        auto* row = beforeChild->previousSibling();
+        if (is<RenderTableRow>(row) && row->isAnonymous()) {
+            beforeChild = nullptr;
+            return downcast<RenderElement>(*row);
+        }
+    }
+
+    // If beforeChild is inside an anonymous cell/row, insert into the cell or into
+    // the anonymous row containing it, if there is one.
+    auto* parentCandidate = lastChild;
+    while (parentCandidate && parentCandidate->parent()->isAnonymous() && !is<RenderTableRow>(*parentCandidate))
+        parentCandidate = parentCandidate->parent();
+    if (is<RenderTableRow>(parentCandidate) && parentCandidate->isAnonymous() && !parentCandidate->isBeforeOrAfterContent())
+        return downcast<RenderElement>(*parentCandidate);
+
+    auto newRow = RenderTableRow::createAnonymousWithParentRenderer(parent);
+    auto& row = *newRow;
+    m_builder.insertChild(parent, WTFMove(newRow), beforeChild);
+    beforeChild = nullptr;
+    return row;
+}
+
 }
index b3f6ce6..d7c505c 100644 (file)
@@ -39,6 +39,7 @@ public:
     Table(RenderTreeBuilder&);
 
     RenderElement& findOrCreateParentForChild(RenderTableRow& parent, const RenderObject& child, RenderObject*& beforeChild);
+    RenderElement& findOrCreateParentForChild(RenderTableSection& parent, const RenderObject& child, RenderObject*& beforeChild);
 
 private:
     RenderTreeBuilder& m_builder;