Render tree teardown should be iterative
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Apr 2016 15:04:02 +0000 (15:04 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Apr 2016 15:04:02 +0000 (15:04 +0000)
https://bugs.webkit.org/show_bug.cgi?id=156233

Reviewed by Andreas Kling.

* dom/ContainerNode.cpp:
(WebCore::destroyRenderTreeIfNeeded):
(WebCore::ContainerNode::takeAllChildrenFrom):
* dom/Document.cpp:
(WebCore::Document::destroyRenderTree):
* dom/Element.cpp:
(WebCore::disconnectPseudoElement):
* html/HTMLPlugInImageElement.cpp:
(WebCore::HTMLPlugInImageElement::prepareForDocumentSuspension):
* mathml/MathMLSelectElement.cpp:
(WebCore::MathMLSelectElement::updateSelectedChild):
* style/RenderTreeUpdater.cpp:
(WebCore::RenderTreeUpdater::updateElementRenderer):
(WebCore::RenderTreeUpdater::updateTextRenderer):
(WebCore::RenderTreeUpdater::updateBeforeOrAfterPseudoElement):
(WebCore::RenderTreeUpdater::tearDownRenderers):

    Tear down render tree using ComposedTreeIterator for traversal.

(WebCore::RenderTreeUpdater::tearDownRenderer):

* style/RenderTreeUpdater.h:
* style/StyleTreeResolver.cpp:
(WebCore::Style::ensurePlaceholderStyle):
(WebCore::Style::TreeResolver::styleForElement):
(WebCore::Style::resetStyleForNonRenderedDescendants):
(WebCore::Style::affectsRenderedSubtree):
(WebCore::Style::SelectorFilterPusher::SelectorFilterPusher): Deleted.
(WebCore::Style::SelectorFilterPusher::push): Deleted.
(WebCore::Style::SelectorFilterPusher::~SelectorFilterPusher): Deleted.

    Unused class.

(WebCore::Style::detachTextRenderer): Deleted.
(WebCore::Style::detachChildren): Deleted.
(WebCore::Style::detachShadowRoot): Deleted.
(WebCore::Style::detachSlotAssignees): Deleted.
(WebCore::Style::detachRenderTree): Deleted.

    Remove the old recursive code.

* style/StyleTreeResolver.h:

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

Source/WebCore/ChangeLog
Source/WebCore/dom/ContainerNode.cpp
Source/WebCore/dom/Document.cpp
Source/WebCore/dom/Element.cpp
Source/WebCore/html/HTMLPlugInImageElement.cpp
Source/WebCore/mathml/MathMLSelectElement.cpp
Source/WebCore/style/RenderTreeUpdater.cpp
Source/WebCore/style/RenderTreeUpdater.h
Source/WebCore/style/StyleTreeResolver.cpp
Source/WebCore/style/StyleTreeResolver.h

index fa237dc..c77c2e4 100644 (file)
@@ -1,3 +1,53 @@
+2016-04-05  Antti Koivisto  <antti@apple.com>
+
+        Render tree teardown should be iterative
+        https://bugs.webkit.org/show_bug.cgi?id=156233
+
+        Reviewed by Andreas Kling.
+
+        * dom/ContainerNode.cpp:
+        (WebCore::destroyRenderTreeIfNeeded):
+        (WebCore::ContainerNode::takeAllChildrenFrom):
+        * dom/Document.cpp:
+        (WebCore::Document::destroyRenderTree):
+        * dom/Element.cpp:
+        (WebCore::disconnectPseudoElement):
+        * html/HTMLPlugInImageElement.cpp:
+        (WebCore::HTMLPlugInImageElement::prepareForDocumentSuspension):
+        * mathml/MathMLSelectElement.cpp:
+        (WebCore::MathMLSelectElement::updateSelectedChild):
+        * style/RenderTreeUpdater.cpp:
+        (WebCore::RenderTreeUpdater::updateElementRenderer):
+        (WebCore::RenderTreeUpdater::updateTextRenderer):
+        (WebCore::RenderTreeUpdater::updateBeforeOrAfterPseudoElement):
+        (WebCore::RenderTreeUpdater::tearDownRenderers):
+
+            Tear down render tree using ComposedTreeIterator for traversal.
+
+        (WebCore::RenderTreeUpdater::tearDownRenderer):
+
+        * style/RenderTreeUpdater.h:
+        * style/StyleTreeResolver.cpp:
+        (WebCore::Style::ensurePlaceholderStyle):
+        (WebCore::Style::TreeResolver::styleForElement):
+        (WebCore::Style::resetStyleForNonRenderedDescendants):
+        (WebCore::Style::affectsRenderedSubtree):
+        (WebCore::Style::SelectorFilterPusher::SelectorFilterPusher): Deleted.
+        (WebCore::Style::SelectorFilterPusher::push): Deleted.
+        (WebCore::Style::SelectorFilterPusher::~SelectorFilterPusher): Deleted.
+
+            Unused class.
+
+        (WebCore::Style::detachTextRenderer): Deleted.
+        (WebCore::Style::detachChildren): Deleted.
+        (WebCore::Style::detachShadowRoot): Deleted.
+        (WebCore::Style::detachSlotAssignees): Deleted.
+        (WebCore::Style::detachRenderTree): Deleted.
+
+            Remove the old recursive code.
+
+        * style/StyleTreeResolver.h:
+
 2016-04-05  Antoine Quint  <graouts@apple.com>
 
         [WebGL2] Allow enabling WebGL2 with a runtime flag
index 246efa6..b99fd41 100644 (file)
 #include "RadioNodeList.h"
 #include "RenderBox.h"
 #include "RenderTheme.h"
+#include "RenderTreeUpdater.h"
 #include "RenderWidget.h"
 #include "RootInlineBox.h"
 #include "SVGDocumentExtensions.h"
 #include "SVGElement.h"
 #include "SVGNames.h"
 #include "SelectorQuery.h"
-#include "StyleTreeResolver.h"
 #include "TemplateContentDocumentFragment.h"
 #include <algorithm>
 #include <wtf/CurrentTime.h>
@@ -103,9 +103,9 @@ static inline void destroyRenderTreeIfNeeded(Node& child)
     if (!child.renderer() && !child.isNamedFlowContentNode() && !is<HTMLSlotElement>(child))
         return;
     if (is<Element>(child))
-        Style::detachRenderTree(downcast<Element>(child));
+        RenderTreeUpdater::tearDownRenderers(downcast<Element>(child));
     else if (is<Text>(child))
-        Style::detachTextRenderer(downcast<Text>(child));
+        RenderTreeUpdater::tearDownRenderer(downcast<Text>(child));
 }
 
 void ContainerNode::takeAllChildrenFrom(ContainerNode* oldParent)
index 3e77a67..e2782e0 100644 (file)
@@ -2353,7 +2353,7 @@ void Document::destroyRenderTree()
     m_activeElement = nullptr;
 
     if (m_documentElement)
-        Style::detachRenderTree(*m_documentElement);
+        RenderTreeUpdater::tearDownRenderers(*m_documentElement);
 
     clearChildNeedsStyleRecalc();
 
index 0bc294e..0e0949d 100644 (file)
@@ -75,6 +75,7 @@
 #include "RenderNamedFlowFragment.h"
 #include "RenderRegion.h"
 #include "RenderTheme.h"
+#include "RenderTreeUpdater.h"
 #include "RenderView.h"
 #include "RenderWidget.h"
 #include "SVGDocumentExtensions.h"
@@ -2703,7 +2704,7 @@ static void disconnectPseudoElement(PseudoElement* pseudoElement)
     if (!pseudoElement)
         return;
     if (pseudoElement->renderer())
-        Style::detachRenderTree(*pseudoElement);
+        RenderTreeUpdater::tearDownRenderers(*pseudoElement);
     ASSERT(pseudoElement->hostElement());
     pseudoElement->clearHostElement();
 }
index d13f948..8d70ac6 100644 (file)
@@ -43,6 +43,7 @@
 #include "RenderEmbeddedObject.h"
 #include "RenderImage.h"
 #include "RenderSnapshottedPlugIn.h"
+#include "RenderTreeUpdater.h"
 #include "SchemeRegistry.h"
 #include "ScriptController.h"
 #include "SecurityOrigin.h"
@@ -322,7 +323,7 @@ void HTMLPlugInImageElement::didMoveToNewDocument(Document* oldDocument)
 void HTMLPlugInImageElement::prepareForDocumentSuspension()
 {
     if (renderer())
-        Style::detachRenderTree(*this);
+        RenderTreeUpdater::tearDownRenderers(*this);
 
     HTMLPlugInElement::prepareForDocumentSuspension();
 }
index 1226a36..2cc2141 100644 (file)
@@ -33,9 +33,9 @@
 #include "HTMLNames.h"
 #include "MathMLNames.h"
 #include "RenderMathMLRow.h"
+#include "RenderTreeUpdater.h"
 #include "SVGElement.h"
 #include "SVGNames.h"
-#include "StyleTreeResolver.h"
 
 namespace WebCore {
 
@@ -202,7 +202,7 @@ void MathMLSelectElement::updateSelectedChild()
         return;
 
     if (m_selectedChild && m_selectedChild->renderer())
-        Style::detachRenderTree(*m_selectedChild);
+        RenderTreeUpdater::tearDownRenderers(*m_selectedChild);
 
     m_selectedChild = newSelectedChild;
     setNeedsStyleRecalc();
index 0d1424c..ebecfdc 100644 (file)
@@ -242,7 +242,7 @@ void RenderTreeUpdater::updateElementRenderer(Element& element, const Style::Ele
 {
     bool shouldTearDownRenderers = update.change == Style::Detach && (element.renderer() || element.isNamedFlowContentNode());
     if (shouldTearDownRenderers)
-        detachRenderTree(element, Style::ReattachDetach);
+        tearDownRenderers(element, TeardownType::KeepHoverAndActive);
 
     bool shouldCreateNewRenderer = !element.renderer() && update.style && !hasImplicitDisplayContents(element);
     if (shouldCreateNewRenderer) {
@@ -409,7 +409,7 @@ void RenderTreeUpdater::updateTextRenderer(Text& text)
     if (hasRenderer) {
         if (needsRenderer)
             return;
-        Style::detachTextRenderer(text);
+        tearDownRenderer(text);
         invalidateWhitespaceOnlyTextSiblingsAfterAttachIfNeeded(text);
         return;
     }
@@ -506,4 +506,59 @@ void RenderTreeUpdater::updateBeforeOrAfterPseudoElement(Element& current, Pseud
         pseudoElement->didRecalcStyle(elementUpdate.change);
 }
 
+void RenderTreeUpdater::tearDownRenderers(Element& root, TeardownType teardownType)
+{
+    WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
+
+    Vector<Element*, 30> teardownStack;
+
+    auto push = [&] (Element& element) {
+        if (element.hasCustomStyleResolveCallbacks())
+            element.willDetachRenderers();
+        if (teardownType != TeardownType::KeepHoverAndActive)
+            element.clearHoverAndActiveStatusBeforeDetachingRenderer();
+        element.clearStyleDerivedDataBeforeDetachingRenderer();
+
+        teardownStack.append(&element);
+    };
+
+    auto pop = [&] (unsigned depth) {
+        while (teardownStack.size() > depth) {
+            auto& element = *teardownStack.takeLast();
+
+            if (auto* renderer = element.renderer()) {
+                renderer->destroyAndCleanupAnonymousWrappers();
+                element.setRenderer(nullptr);
+            }
+            if (element.hasCustomStyleResolveCallbacks())
+                element.didDetachRenderers();
+        }
+    };
+
+    push(root);
+
+    auto descendants = composedTreeDescendants(root);
+    for (auto it = descendants.begin(), end = descendants.end(); it != end; ++it) {
+        pop(it.depth());
+
+        if (is<Text>(*it)) {
+            tearDownRenderer(downcast<Text>(*it));
+            continue;
+        }
+
+        push(downcast<Element>(*it));
+    }
+
+    pop(0);
+}
+
+void RenderTreeUpdater::tearDownRenderer(Text& text)
+{
+    auto* renderer = text.renderer();
+    if (!renderer)
+        return;
+    renderer->destroyAndCleanupAnonymousWrappers();
+    text.setRenderer(nullptr);
+}
+
 }
index 14eada3..0b1fa49 100644 (file)
@@ -50,6 +50,10 @@ public:
 
     void commit(std::unique_ptr<const Style::Update>);
 
+    enum class TeardownType { Normal, KeepHoverAndActive };
+    static void tearDownRenderers(Element&, TeardownType = TeardownType::Normal);
+    static void tearDownRenderer(Text&);
+
 private:
     void updateRenderTree(ContainerNode& root);
     void updateTextRenderer(Text&);
index 0324dd5..b16125f 100644 (file)
@@ -50,37 +50,6 @@ namespace WebCore {
 
 namespace Style {
 
-class SelectorFilterPusher {
-public:
-    enum PushMode { Push, NoPush };
-    SelectorFilterPusher(SelectorFilter& selectorFilter, Element& parent, PushMode pushMode = Push)
-        : m_selectorFilter(selectorFilter)
-        , m_parent(parent)
-    {
-        if (pushMode == Push)
-            push();
-    }
-    void push()
-    {
-        if (m_didPush)
-            return;
-        m_didPush = true;
-        m_selectorFilter.pushParent(&m_parent);
-    }
-    ~SelectorFilterPusher()
-    {
-        if (!m_didPush)
-            return;
-        m_selectorFilter.popParent();
-    }
-    
-private:
-    SelectorFilter& m_selectorFilter;
-    Element& m_parent;
-    bool m_didPush { false };
-};
-
-
 static RenderStyle* placeholderStyle;
 
 static void ensurePlaceholderStyle(Document& document)
@@ -176,13 +145,6 @@ Ref<RenderStyle> TreeResolver::styleForElement(Element& element, RenderStyle& in
     return WTFMove(elementStyle.renderStyle);
 }
 
-void detachTextRenderer(Text& textNode)
-{
-    if (textNode.renderer())
-        textNode.renderer()->destroyAndCleanupAnonymousWrappers();
-    textNode.setRenderer(0);
-}
-
 static void resetStyleForNonRenderedDescendants(Element& current)
 {
     // FIXME: This is not correct with shadow trees. This should be done with ComposedTreeIterator.
@@ -204,72 +166,6 @@ static void resetStyleForNonRenderedDescendants(Element& current)
     }
 }
 
-static void detachChildren(ContainerNode& current, DetachType detachType)
-{
-    for (Node* child = current.firstChild(); child; child = child->nextSibling()) {
-        if (is<Text>(*child))
-            detachTextRenderer(downcast<Text>(*child));
-        else if (is<Element>(*child))
-            detachRenderTree(downcast<Element>(*child), detachType);
-    }
-    current.clearChildNeedsStyleRecalc();
-}
-
-static void detachShadowRoot(ShadowRoot& shadowRoot, DetachType detachType)
-{
-    detachChildren(shadowRoot, detachType);
-}
-
-#if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
-static void detachSlotAssignees(HTMLSlotElement& slot, DetachType detachType)
-{
-    ASSERT(!slot.renderer());
-    if (auto* assignedNodes = slot.assignedNodes()) {
-        for (auto* child : *assignedNodes) {
-            if (is<Text>(*child))
-                detachTextRenderer(downcast<Text>(*child));
-            else if (is<Element>(*child))
-                detachRenderTree(downcast<Element>(*child), detachType);
-        }
-    } else
-        detachChildren(slot, detachType);
-
-    slot.clearNeedsStyleRecalc();
-    slot.clearChildNeedsStyleRecalc();
-}
-#endif
-
-void detachRenderTree(Element& current, DetachType detachType)
-{
-    WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
-
-    if (current.hasCustomStyleResolveCallbacks())
-        current.willDetachRenderers();
-
-    current.clearStyleDerivedDataBeforeDetachingRenderer();
-
-    // Do not remove the element's hovered and active status
-    // if performing a reattach.
-    if (detachType != ReattachDetach)
-        current.clearHoverAndActiveStatusBeforeDetachingRenderer();
-
-#if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
-    if (is<HTMLSlotElement>(current))
-        detachSlotAssignees(downcast<HTMLSlotElement>(current), detachType);
-#endif
-    else if (ShadowRoot* shadowRoot = current.shadowRoot())
-        detachShadowRoot(*shadowRoot, detachType);
-
-    detachChildren(current, detachType);
-
-    if (current.renderer())
-        current.renderer()->destroyAndCleanupAnonymousWrappers();
-    current.setRenderer(nullptr);
-
-    if (current.hasCustomStyleResolveCallbacks())
-        current.didDetachRenderers();
-}
-
 static bool affectsRenderedSubtree(Element& element, const RenderStyle& newStyle)
 {
     if (element.renderer())
index 7789e4d..fdd37db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -108,10 +108,6 @@ private:
     std::unique_ptr<Update> m_update;
 };
 
-enum DetachType { NormalDetach, ReattachDetach };
-void detachRenderTree(Element&, DetachType = NormalDetach);
-void detachTextRenderer(Text&);
-
 void queuePostResolutionCallback(std::function<void ()>);
 bool postResolutionCallbacksAreSuspended();