Title: [154700] trunk/Source/WebCore
Revision
154700
Author
[email protected]
Date
2013-08-27 11:22:25 -0700 (Tue, 27 Aug 2013)

Log Message

Better mutation and event assertions for descendant iterators
https://bugs.webkit.org/show_bug.cgi?id=120368

Reviewed by Andreas Kling.

Add mutation assertions to all functions.
Drop the no-event-dispatch assertion when the iterator reaches the end. This reduces need for iterator scoping
just to avoid assertions.

* dom/ChildIterator.h:
(WebCore::::domTreeHasMutated):
(WebCore::::operator):
(WebCore::=):
* dom/DescendantIterator.h:
(WebCore::::domTreeHasMutated):
(WebCore::::operator):
(WebCore::=):
* dom/Document.cpp:
(WebCore::Document::childrenChanged):
        
    Make idiomatic.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (154699 => 154700)


--- trunk/Source/WebCore/ChangeLog	2013-08-27 18:02:46 UTC (rev 154699)
+++ trunk/Source/WebCore/ChangeLog	2013-08-27 18:22:25 UTC (rev 154700)
@@ -1,3 +1,27 @@
+2013-08-27  Antti Koivisto  <[email protected]>
+
+        Better mutation and event assertions for descendant iterators
+        https://bugs.webkit.org/show_bug.cgi?id=120368
+
+        Reviewed by Andreas Kling.
+
+        Add mutation assertions to all functions.
+        Drop the no-event-dispatch assertion when the iterator reaches the end. This reduces need for iterator scoping
+        just to avoid assertions.
+
+        * dom/ChildIterator.h:
+        (WebCore::::domTreeHasMutated):
+        (WebCore::::operator):
+        (WebCore::=):
+        * dom/DescendantIterator.h:
+        (WebCore::::domTreeHasMutated):
+        (WebCore::::operator):
+        (WebCore::=):
+        * dom/Document.cpp:
+        (WebCore::Document::childrenChanged):
+        
+            Make idiomatic.
+
 2013-08-27  Renata Hodovan  <[email protected]>
 
         Missing null-check of parent renderer in WebCore::HTMLEmbedElement::rendererIsNeeded()

Modified: trunk/Source/WebCore/dom/ChildIterator.h (154699 => 154700)


--- trunk/Source/WebCore/dom/ChildIterator.h	2013-08-27 18:02:46 UTC (rev 154699)
+++ trunk/Source/WebCore/dom/ChildIterator.h	2013-08-27 18:22:25 UTC (rev 154700)
@@ -40,13 +40,15 @@
     ChildIterator();
     ChildIterator(ElementType* current);
     ChildIterator& operator++();
-    ElementType& operator*() { return *m_current; }
-    ElementType* operator->() { return m_current; }
+    ElementType& operator*();
+    ElementType* operator->();
     bool operator!=(const ChildIterator& other) const;
 
 private:
     ElementType* m_current;
+
 #ifndef NDEBUG
+    bool domTreeHasMutated() const;
     OwnPtr<NoEventDispatchAssertion> m_noEventDispatchAssertion;
     uint64_t m_initialDOMTreeVersion;
 #endif
@@ -87,18 +89,48 @@
 {
 }
 
+#ifndef NDEBUG
 template <typename ElementType>
+inline bool ChildIterator<ElementType>::domTreeHasMutated() const
+{
+    return m_initialDOMTreeVersion && m_current && m_current->document()->domTreeVersion() != m_initialDOMTreeVersion;
+}
+#endif
+
+template <typename ElementType>
 inline ChildIterator<ElementType>& ChildIterator<ElementType>::operator++()
 {
     ASSERT(m_current);
-    ASSERT(m_current->document()->domTreeVersion() == m_initialDOMTreeVersion);
+    ASSERT(!domTreeHasMutated());
     m_current = Traversal<ElementType>::nextSibling(m_current);
+#ifndef NDEBUG
+    // Drop the assertion when the iterator reaches the end.
+    if (!m_current)
+        m_noEventDispatchAssertion = nullptr;
+#endif
     return *this;
 }
 
 template <typename ElementType>
+inline ElementType& ChildIterator<ElementType>::operator*()
+{
+    ASSERT(m_current);
+    ASSERT(!domTreeHasMutated());
+    return *m_current;
+}
+
+template <typename ElementType>
+inline ElementType* ChildIterator<ElementType>::operator->()
+{
+    ASSERT(m_current);
+    ASSERT(!domTreeHasMutated());
+    return m_current;
+}
+
+template <typename ElementType>
 inline bool ChildIterator<ElementType>::operator!=(const ChildIterator& other) const
 {
+    ASSERT(!domTreeHasMutated());
     return m_current != other.m_current;
 }
 

Modified: trunk/Source/WebCore/dom/DescendantIterator.h (154699 => 154700)


--- trunk/Source/WebCore/dom/DescendantIterator.h	2013-08-27 18:02:46 UTC (rev 154699)
+++ trunk/Source/WebCore/dom/DescendantIterator.h	2013-08-27 18:22:25 UTC (rev 154700)
@@ -40,14 +40,16 @@
     DescendantIterator(const Node* root);
     DescendantIterator(const Node* root, ElementType* current);
     DescendantIterator& operator++();
-    ElementType& operator*() { return *m_current; }
-    ElementType* operator->() { return m_current; }
+    ElementType& operator*();
+    ElementType* operator->();
     bool operator!=(const DescendantIterator& other) const;
 
 private:
     const Node* m_root;
     ElementType* m_current;
+
 #ifndef NDEBUG
+    bool domTreeHasMutated() const;
     OwnPtr<NoEventDispatchAssertion> m_noEventDispatchAssertion;
     uint64_t m_initialDOMTreeVersion;
 #endif
@@ -85,24 +87,54 @@
     , m_current(current)
 #ifndef NDEBUG
     , m_noEventDispatchAssertion(adoptPtr(new NoEventDispatchAssertion))
-    , m_initialDOMTreeVersion(m_current ? m_current->document()->domTreeVersion() : 0)
+    , m_initialDOMTreeVersion(root->document()->domTreeVersion())
 #endif
 {
 }
 
+#ifndef NDEBUG
 template <typename ElementType>
+inline bool DescendantIterator<ElementType>::domTreeHasMutated() const
+{
+    return m_root->document()->domTreeVersion() != m_initialDOMTreeVersion;
+}
+#endif
+
+template <typename ElementType>
 inline DescendantIterator<ElementType>& DescendantIterator<ElementType>::operator++()
 {
     ASSERT(m_current);
-    ASSERT(m_current->document()->domTreeVersion() == m_initialDOMTreeVersion);
+    ASSERT(!domTreeHasMutated());
     m_current = Traversal<ElementType>::next(m_current, m_root);
+#ifndef NDEBUG
+    // Drop the assertion when the iterator reaches the end.
+    if (!m_current)
+        m_noEventDispatchAssertion = nullptr;
+#endif
     return *this;
 }
 
 template <typename ElementType>
+inline ElementType& DescendantIterator<ElementType>::operator*()
+{
+    ASSERT(m_current);
+    ASSERT(!domTreeHasMutated());
+    return *m_current;
+}
+
+template <typename ElementType>
+inline ElementType* DescendantIterator<ElementType>::operator->()
+{
+    ASSERT(m_current);
+    ASSERT(!domTreeHasMutated());
+    return m_current;
+}
+
+template <typename ElementType>
 inline bool DescendantIterator<ElementType>::operator!=(const DescendantIterator& other) const
 {
     ASSERT(m_root == other.m_root);
+    ASSERT(!domTreeHasMutated());
     return m_current != other.m_current;
 }
 

Modified: trunk/Source/WebCore/dom/Document.cpp (154699 => 154700)


--- trunk/Source/WebCore/dom/Document.cpp	2013-08-27 18:02:46 UTC (rev 154699)
+++ trunk/Source/WebCore/dom/Document.cpp	2013-08-27 18:22:25 UTC (rev 154700)
@@ -776,8 +776,12 @@
 void Document::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
 {
     ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
-    
-    Element* newDocumentElement = &*elementChildren(this).begin();
+
+    Element* newDocumentElement = 0;
+    auto firstElementChild = elementChildren(this).begin();
+    if (firstElementChild != elementChildren(this).end())
+        newDocumentElement = &*firstElementChild;
+
     if (newDocumentElement == m_documentElement)
         return;
     m_documentElement = newDocumentElement;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to