Title: [200994] trunk/Source/WebCore
Revision
200994
Author
[email protected]
Date
2016-05-16 21:11:34 -0700 (Mon, 16 May 2016)

Log Message

Add RenderDescendantIterator to traverse a RenderObject's descendants
https://bugs.webkit.org/show_bug.cgi?id=157785

Reviewed by Zalan Bujtas.

Add RenderDescendantIterator to traverse a RenderObject's descendants. I
am planning to use it in the iOS Text Autosizing code (See Bug 157784).

* WebCore.xcodeproj/project.pbxproj:
* rendering/RenderDescendantIterator.h: Added.
(WebCore::RenderDescendantIterator<T>::RenderDescendantIterator):
(WebCore::RenderDescendantIterator<T>::operator):
(WebCore::RenderDescendantConstIterator<T>::RenderDescendantConstIterator):
(WebCore::RenderDescendantConstIterator<T>::operator):
(WebCore::RenderDescendantIteratorAdapter<T>::RenderDescendantIteratorAdapter):
(WebCore::RenderDescendantIteratorAdapter<T>::begin):
(WebCore::RenderDescendantIteratorAdapter<T>::end):
(WebCore::RenderDescendantConstIteratorAdapter<T>::RenderDescendantConstIteratorAdapter):
(WebCore::RenderDescendantConstIteratorAdapter<T>::begin):
(WebCore::RenderDescendantConstIteratorAdapter<T>::end):
(WebCore::descendantsOfType):
* rendering/RenderIterator.h:
(WebCore::RenderObjectTraversal::firstChild):
(WebCore::RenderObjectTraversal::nextAncestorSibling):
(WebCore::RenderObjectTraversal::next):
(WebCore::RenderTraversal::firstChild):
(WebCore::RenderTraversal::lastChild):
(WebCore::RenderTraversal::nextSibling):
(WebCore::RenderTraversal::previousSibling):
(WebCore::RenderTraversal::findAncestorOfType):
(WebCore::RenderTraversal::firstWithin):
(WebCore::RenderTraversal::next):
(WebCore::RenderIterator<T>::traverseNext):
(WebCore::RenderConstIterator<T>::traverseNext):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (200993 => 200994)


--- trunk/Source/WebCore/ChangeLog	2016-05-17 04:02:40 UTC (rev 200993)
+++ trunk/Source/WebCore/ChangeLog	2016-05-17 04:11:34 UTC (rev 200994)
@@ -1,3 +1,40 @@
+2016-05-16  Chris Dumez  <[email protected]>
+
+        Add RenderDescendantIterator to traverse a RenderObject's descendants
+        https://bugs.webkit.org/show_bug.cgi?id=157785
+
+        Reviewed by Zalan Bujtas.
+
+        Add RenderDescendantIterator to traverse a RenderObject's descendants. I
+        am planning to use it in the iOS Text Autosizing code (See Bug 157784).
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * rendering/RenderDescendantIterator.h: Added.
+        (WebCore::RenderDescendantIterator<T>::RenderDescendantIterator):
+        (WebCore::RenderDescendantIterator<T>::operator):
+        (WebCore::RenderDescendantConstIterator<T>::RenderDescendantConstIterator):
+        (WebCore::RenderDescendantConstIterator<T>::operator):
+        (WebCore::RenderDescendantIteratorAdapter<T>::RenderDescendantIteratorAdapter):
+        (WebCore::RenderDescendantIteratorAdapter<T>::begin):
+        (WebCore::RenderDescendantIteratorAdapter<T>::end):
+        (WebCore::RenderDescendantConstIteratorAdapter<T>::RenderDescendantConstIteratorAdapter):
+        (WebCore::RenderDescendantConstIteratorAdapter<T>::begin):
+        (WebCore::RenderDescendantConstIteratorAdapter<T>::end):
+        (WebCore::descendantsOfType):
+        * rendering/RenderIterator.h:
+        (WebCore::RenderObjectTraversal::firstChild):
+        (WebCore::RenderObjectTraversal::nextAncestorSibling):
+        (WebCore::RenderObjectTraversal::next):
+        (WebCore::RenderTraversal::firstChild):
+        (WebCore::RenderTraversal::lastChild):
+        (WebCore::RenderTraversal::nextSibling):
+        (WebCore::RenderTraversal::previousSibling):
+        (WebCore::RenderTraversal::findAncestorOfType):
+        (WebCore::RenderTraversal::firstWithin):
+        (WebCore::RenderTraversal::next):
+        (WebCore::RenderIterator<T>::traverseNext):
+        (WebCore::RenderConstIterator<T>::traverseNext):
+
 2016-05-16  Alex Christensen  <[email protected]>
 
         Remove YouTube site-specific hack

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (200993 => 200994)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2016-05-17 04:02:40 UTC (rev 200993)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2016-05-17 04:11:34 UTC (rev 200994)
@@ -2883,6 +2883,7 @@
 		836CB1F71BD1E41800AF1591 /* JSHTMLTableHeaderCellElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 836CB1F31BD1E41800AF1591 /* JSHTMLTableHeaderCellElement.cpp */; };
 		836CB1F81BD1E41800AF1591 /* JSHTMLTableDataCellElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 836CB1F41BD1E41800AF1591 /* JSHTMLTableDataCellElement.h */; };
 		836CB1F91BD1E41800AF1591 /* JSHTMLTableDataCellElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 836CB1F51BD1E41800AF1591 /* JSHTMLTableDataCellElement.cpp */; };
+		836DAA061CEAB80D00A2B707 /* RenderDescendantIterator.h in Headers */ = {isa = PBXBuildFile; fileRef = 836DAA051CEAB7CD00A2B707 /* RenderDescendantIterator.h */; };
 		836FBCEA178C113200B21A15 /* SVGAnimatedTypeAnimator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 836FBCE9178C113200B21A15 /* SVGAnimatedTypeAnimator.cpp */; };
 		836FBCEC178C117F00B21A15 /* SVGAnimatedProperty.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 836FBCEB178C117F00B21A15 /* SVGAnimatedProperty.cpp */; };
 		8372DB311A6780A800C697C5 /* DiagnosticLoggingResultType.h in Headers */ = {isa = PBXBuildFile; fileRef = 8372DB301A6780A800C697C5 /* DiagnosticLoggingResultType.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -10565,6 +10566,7 @@
 		836CB1F31BD1E41800AF1591 /* JSHTMLTableHeaderCellElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLTableHeaderCellElement.cpp; sourceTree = "<group>"; };
 		836CB1F41BD1E41800AF1591 /* JSHTMLTableDataCellElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSHTMLTableDataCellElement.h; sourceTree = "<group>"; };
 		836CB1F51BD1E41800AF1591 /* JSHTMLTableDataCellElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLTableDataCellElement.cpp; sourceTree = "<group>"; };
+		836DAA051CEAB7CD00A2B707 /* RenderDescendantIterator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderDescendantIterator.h; sourceTree = "<group>"; };
 		836FBCE9178C113200B21A15 /* SVGAnimatedTypeAnimator.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SVGAnimatedTypeAnimator.cpp; sourceTree = "<group>"; };
 		836FBCEB178C117F00B21A15 /* SVGAnimatedProperty.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SVGAnimatedProperty.cpp; sourceTree = "<group>"; };
 		8372DB301A6780A800C697C5 /* DiagnosticLoggingResultType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DiagnosticLoggingResultType.h; sourceTree = "<group>"; };
@@ -24037,6 +24039,7 @@
 				9392F1410AD185F400691BD4 /* RenderCounter.h */,
 				BCEA482E097D93020094C9E4 /* RenderDeprecatedFlexibleBox.cpp */,
 				BCEA482F097D93020094C9E4 /* RenderDeprecatedFlexibleBox.h */,
+				836DAA051CEAB7CD00A2B707 /* RenderDescendantIterator.h */,
 				D302754512A5FE84004BD828 /* RenderDetailsMarker.cpp */,
 				D302754612A5FE84004BD828 /* RenderDetailsMarker.h */,
 				E43A023C17EB3713004CDD25 /* RenderElement.cpp */,
@@ -28205,6 +28208,7 @@
 				0854B0211255E4E600B9CDD0 /* SVGInlineTextBox.h in Headers */,
 				B2227A300D00BF220071B782 /* SVGLangSpace.h in Headers */,
 				B2227A330D00BF220071B782 /* SVGLength.h in Headers */,
+				836DAA061CEAB80D00A2B707 /* RenderDescendantIterator.h in Headers */,
 				7134496E146941B300720312 /* SVGLengthContext.h in Headers */,
 				B2227A360D00BF220071B782 /* SVGLengthList.h in Headers */,
 				B2227A390D00BF220071B782 /* SVGLinearGradientElement.h in Headers */,

Added: trunk/Source/WebCore/rendering/RenderDescendantIterator.h (0 => 200994)


--- trunk/Source/WebCore/rendering/RenderDescendantIterator.h	                        (rev 0)
+++ trunk/Source/WebCore/rendering/RenderDescendantIterator.h	2016-05-17 04:11:34 UTC (rev 200994)
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 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
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "RenderIterator.h"
+
+namespace WebCore {
+
+template <typename T>
+class RenderDescendantIterator : public RenderIterator<T> {
+public:
+    RenderDescendantIterator(const RenderElement& root);
+    RenderDescendantIterator(const RenderElement& root, T* current);
+    RenderDescendantIterator& operator++();
+};
+
+template <typename T>
+class RenderDescendantConstIterator : public RenderConstIterator<T> {
+public:
+    RenderDescendantConstIterator(const RenderElement& root);
+    RenderDescendantConstIterator(const RenderElement& root, const T* current);
+    RenderDescendantConstIterator& operator++();
+};
+
+template <typename T>
+class RenderDescendantIteratorAdapter {
+public:
+    RenderDescendantIteratorAdapter(RenderElement& root);
+    RenderDescendantIterator<T> begin();
+    RenderDescendantIterator<T> end();
+
+private:
+    RenderElement& m_root;
+};
+
+template <typename T>
+class RenderDescendantConstIteratorAdapter {
+public:
+    RenderDescendantConstIteratorAdapter(const RenderElement& root);
+    RenderDescendantConstIterator<T> begin() const;
+    RenderDescendantConstIterator<T> end() const;
+
+private:
+    const RenderElement& m_root;
+};
+
+template <typename T> RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement&);
+template <typename T> RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement&);
+
+// RenderDescendantIterator
+
+template <typename T>
+inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root)
+    : RenderIterator<T>(&root)
+{
+}
+
+template <typename T>
+inline RenderDescendantIterator<T>::RenderDescendantIterator(const RenderElement& root, T* current)
+    : RenderIterator<T>(&root, current)
+{
+}
+
+template <typename T>
+inline RenderDescendantIterator<T>& RenderDescendantIterator<T>::operator++()
+{
+    return static_cast<RenderDescendantIterator<T>&>(RenderIterator<T>::traverseNext());
+}
+
+// RenderDescendantConstIterator
+
+template <typename T>
+inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root)
+    : RenderConstIterator<T>(&root)
+{
+}
+
+template <typename T>
+inline RenderDescendantConstIterator<T>::RenderDescendantConstIterator(const RenderElement& root, const T* current)
+    : RenderConstIterator<T>(&root, current)
+{
+}
+
+template <typename T>
+inline RenderDescendantConstIterator<T>& RenderDescendantConstIterator<T>::operator++()
+{
+    return static_cast<RenderDescendantConstIterator<T>&>(RenderConstIterator<T>::traverseNext());
+}
+
+// RenderDescendantIteratorAdapter
+
+template <typename T>
+inline RenderDescendantIteratorAdapter<T>::RenderDescendantIteratorAdapter(RenderElement& root)
+    : m_root(root)
+{
+}
+
+template <typename T>
+inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::begin()
+{
+    return RenderDescendantIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root));
+}
+
+template <typename T>
+inline RenderDescendantIterator<T> RenderDescendantIteratorAdapter<T>::end()
+{
+    return RenderDescendantIterator<T>(m_root);
+}
+
+// RenderDescendantConstIteratorAdapter
+
+template <typename T>
+inline RenderDescendantConstIteratorAdapter<T>::RenderDescendantConstIteratorAdapter(const RenderElement& root)
+    : m_root(root)
+{
+}
+
+template <typename T>
+inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::begin() const
+{
+    return RenderDescendantConstIterator<T>(m_root, RenderTraversal::firstWithin<T>(m_root));
+}
+
+template <typename T>
+inline RenderDescendantConstIterator<T> RenderDescendantConstIteratorAdapter<T>::end() const
+{
+    return RenderDescendantConstIterator<T>(m_root);
+}
+
+// Standalone functions
+
+template <typename T>
+inline RenderDescendantIteratorAdapter<T> descendantsOfType(RenderElement& root)
+{
+    return RenderDescendantIteratorAdapter<T>(root);
+}
+
+template <typename T>
+inline RenderDescendantConstIteratorAdapter<T> descendantsOfType(const RenderElement& root)
+{
+    return RenderDescendantConstIteratorAdapter<T>(root);
+}
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/rendering/RenderIterator.h (200993 => 200994)


--- trunk/Source/WebCore/rendering/RenderIterator.h	2016-05-17 04:02:40 UTC (rev 200993)
+++ trunk/Source/WebCore/rendering/RenderIterator.h	2016-05-17 04:11:34 UTC (rev 200994)
@@ -42,6 +42,7 @@
     bool operator==(const RenderIterator& other) const;
     bool operator!=(const RenderIterator& other) const;
 
+    RenderIterator& traverseNext();
     RenderIterator& traverseNextSibling();
     RenderIterator& traversePreviousSibling();
     RenderIterator& traverseAncestor();
@@ -63,6 +64,7 @@
     bool operator==(const RenderConstIterator& other) const;
     bool operator!=(const RenderConstIterator& other) const;
 
+    RenderConstIterator& traverseNext();
     RenderConstIterator& traverseNextSibling();
     RenderConstIterator& traversePreviousSibling();
     RenderConstIterator& traverseAncestor();
@@ -78,12 +80,58 @@
 
 // Traversal helpers
 
+namespace RenderObjectTraversal {
+
+template <typename U>
+inline RenderObject* firstChild(U& object)
+{
+    return object.firstChild();
+}
+
+inline RenderObject* firstChild(RenderObject& object)
+{
+    return object.firstChildSlow();
+}
+
+inline RenderObject* firstChild(RenderText&)
+{
+    return nullptr;
+}
+
+inline RenderObject* nextAncestorSibling(RenderObject& current, const RenderObject* stayWithin)
+{
+    for (auto* ancestor = current.parent(); ancestor; ancestor = ancestor->parent()) {
+        if (ancestor == stayWithin)
+            return nullptr;
+        if (auto* sibling = ancestor->nextSibling())
+            return sibling;
+    }
+    return nullptr;
+}
+
+template <typename U>
+inline RenderObject* next(U& current, const RenderObject* stayWithin)
+{
+    if (auto* child = firstChild(current))
+        return child;
+
+    if (&current == stayWithin)
+        return nullptr;
+
+    if (auto* sibling = current.nextSibling())
+        return sibling;
+
+    return nextAncestorSibling(current, stayWithin);
+}
+
+}
+
 namespace RenderTraversal {
 
 template <typename T, typename U>
 inline T* firstChild(U& current)
 {
-    RenderObject* object = current.firstChild();
+    RenderObject* object = RenderObjectTraversal::firstChild(current);
     while (object && !isRendererOfType<T>(*object))
         object = object->nextSibling();
     return static_cast<T*>(object);
@@ -126,6 +174,24 @@
     return nullptr;
 }
 
+template <typename T, typename U>
+inline T* firstWithin(U& current)
+{
+    auto* descendant = RenderObjectTraversal::firstChild(current);
+    while (descendant && !isRendererOfType<T>(*descendant))
+        descendant = RenderObjectTraversal::next(*descendant, &current);
+    return static_cast<T*>(descendant);
+}
+
+template <typename T, typename U>
+inline T* next(U& current, const RenderObject* stayWithin)
+{
+    auto* descendant = RenderObjectTraversal::next(current, stayWithin);
+    while (descendant && !isRendererOfType<T>(*descendant))
+        descendant = RenderObjectTraversal::next(*descendant, stayWithin);
+    return static_cast<T*>(descendant);
+}
+
 } // namespace WebCore::RenderTraversal
 
 // RenderIterator
@@ -153,6 +219,14 @@
 }
 
 template <typename T>
+inline RenderIterator<T>& RenderIterator<T>::traverseNext()
+{
+    ASSERT(m_current);
+    m_current = RenderTraversal::next<T>(*m_current, m_root);
+    return *this;
+}
+
+template <typename T>
 inline RenderIterator<T>& RenderIterator<T>::traversePreviousSibling()
 {
     ASSERT(m_current);
@@ -221,6 +295,14 @@
 }
 
 template <typename T>
+inline RenderConstIterator<T>& RenderConstIterator<T>::traverseNext()
+{
+    ASSERT(m_current);
+    m_current = RenderTraversal::next<T>(*m_current, m_root);
+    return *this;
+}
+
+template <typename T>
 inline RenderConstIterator<T>& RenderConstIterator<T>::traversePreviousSibling()
 {
     ASSERT(m_current);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to