Element child and descendant iterators
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 25 Aug 2013 21:30:10 +0000 (21:30 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 25 Aug 2013 21:30:10 +0000 (21:30 +0000)
commit3bac26fa07180ebb31aefae2a5d18fe8f225f945
tree511a928a6fc26e4d2fdb0ffa7d1d6e4e16bf8647
parentb18189e22fa2ac3fac6dc10e215b87e5fd567b48
Element child and descendant iterators
https://bugs.webkit.org/show_bug.cgi?id=120248

Reviewed by Sam Weinig and Andreas Kling.

Add iterators for Element children and descendants.

To iterate over element children:

#include "ChildIterator.h"

for (auto it = elementChildren(this).begin(), end = elementChildren(this).end(); it != end; ++it) {
    Element& element = *it;
    ...

for (auto it = childrenOfType<HTMLAreaElement>(this).begin(), end = childrenOfType<HTMLAreaElement>(this).end(); it != end; ++it) {
    HTMLAreaElement& area = *it;
    ...

To iteratate over element descendants in pre-order:

#include "DescendantIterator.h"

for (auto it = elementDescendants(this).begin(), end = elementDescendants(this).end(); it != end; ++it) {
    Element& element = *it;
    ...

for (auto it = descendantsOfType<HTMLAreaElement>(this).begin(), end = descendantsOfType<HTMLAreaElement>(this).end(); it != end; ++it) {
    HTMLAreaElement& area = *it;
    ...

The iterators assert against DOM mutations and event dispatch while iterating in debug builds.

They are compatible with C++11 range-based for loops. In the future we can use

for (auto& element : elementChildren(this))
    ...

etc.

The patch all uses the new iterators in a few places.

* WebCore.xcodeproj/project.pbxproj:
* accessibility/AccessibilityNodeObject.cpp:
(WebCore::AccessibilityNodeObject::canvasHasFallbackContent):
(WebCore::siblingWithAriaRole):
* accessibility/AccessibilityRenderObject.cpp:
* accessibility/AccessibilityTable.cpp:
(WebCore::AccessibilityTable::isDataTable):
* dom/ChildIterator.h: Added.
(WebCore::ChildIterator::operator*):
(WebCore::ChildIterator::operator->):
(WebCore::::ChildIterator):
(WebCore::::operator):
(WebCore::=):
(WebCore::::ChildIteratorAdapter):
(WebCore::::begin):
(WebCore::::end):
(WebCore::elementChildren):
(WebCore::childrenOfType):
* dom/DescendantIterator.h: Added.
(WebCore::DescendantIterator::operator*):
(WebCore::DescendantIterator::operator->):
(WebCore::::DescendantIterator):
(WebCore::::operator):
(WebCore::=):
(WebCore::::DescendantIteratorAdapter):
(WebCore::::begin):
(WebCore::::end):
(WebCore::elementDescendants):
(WebCore::descendantsOfType):
* dom/Document.cpp:
(WebCore::Document::buildAccessKeyMap):
(WebCore::Document::childrenChanged):
(WebCore::Document::attach):
(WebCore::Document::detach):
* editing/ApplyStyleCommand.cpp:
(WebCore::ApplyStyleCommand::cleanupUnstyledAppleStyleSpans):
* editing/markup.cpp:
(WebCore::completeURLs):
* html/HTMLMapElement.cpp:
(WebCore::HTMLMapElement::mapMouseEvent):
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::selectMediaResource):
(WebCore::HTMLMediaElement::textTrackModeChanged):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@154581 268f45cc-cd09-0410-ab3c-d52691b4dbfc
15 files changed:
Source/WebCore/ChangeLog
Source/WebCore/GNUmakefile.list.am
Source/WebCore/Target.pri
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/accessibility/AccessibilityNodeObject.cpp
Source/WebCore/accessibility/AccessibilityRenderObject.cpp
Source/WebCore/accessibility/AccessibilityTable.cpp
Source/WebCore/dom/ChildIterator.h [new file with mode: 0644]
Source/WebCore/dom/DescendantIterator.h [new file with mode: 0644]
Source/WebCore/dom/Document.cpp
Source/WebCore/editing/ApplyStyleCommand.cpp
Source/WebCore/editing/markup.cpp
Source/WebCore/html/HTMLMapElement.cpp
Source/WebCore/html/HTMLMediaElement.cpp