<https://webkit.org/b/119959> Add TextNodeTraversal

Reviewed by Andreas Kling.

Add TextNodeTraversal for cleaner and more compact traversal of Text nodes.
        
Use it where appropriate.

* WebCore.xcodeproj/project.pbxproj:
* dom/Attr.cpp:
(WebCore::Attr::childrenChanged):
* dom/NodeTraversal.h:
(WebCore::NodeTraversal::next):
* dom/ScriptElement.cpp:
(WebCore::ScriptElement::scriptContent):
* dom/Text.cpp:
(WebCore::Text::wholeText):
* dom/Text.h:
(WebCore::toText):
* dom/TextNodeTraversal.cpp: Added.
(WebCore::TextNodeTraversal::appendContents):
(WebCore::TextNodeTraversal::contentsAsString):
        
    Helpers for getting text content of a subtree.

* dom/TextNodeTraversal.h: Added.
(WebCore::TextNodeTraversal::firstTextChildTemplate):
(WebCore::TextNodeTraversal::firstChild):
(WebCore::TextNodeTraversal::firstTextWithinTemplate):
(WebCore::TextNodeTraversal::firstWithin):
(WebCore::TextNodeTraversal::traverseNextTextTemplate):
(WebCore::TextNodeTraversal::next):
(WebCore::TextNodeTraversal::nextSibling):
* editing/ApplyStyleCommand.cpp:
(WebCore::ApplyStyleCommand::joinChildTextNodes):
* html/HTMLTextAreaElement.cpp:
(WebCore::HTMLTextAreaElement::defaultValue):
(WebCore::HTMLTextAreaElement::setDefaultValue):
* html/HTMLTitleElement.cpp:
(WebCore::HTMLTitleElement::text):
* html/track/TextTrackCue.cpp:
(WebCore::TextTrackCue::determineTextDirection):
* page/Frame.cpp:
(WebCore::Frame::searchForLabelsAboveCell):
* xml/XPathUtil.cpp:
(WebCore::XPath::stringValue):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154240 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/dom/TextNodeTraversal.cpp b/Source/WebCore/dom/TextNodeTraversal.cpp
new file mode 100644
index 0000000..1f30afe
--- /dev/null
+++ b/Source/WebCore/dom/TextNodeTraversal.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2013 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. ``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
+ * 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.
+ */
+
+#include "config.h"
+#include "TextNodeTraversal.h"
+
+#include "ContainerNode.h"
+#include <wtf/text/StringBuilder.h>
+
+namespace WebCore {
+namespace TextNodeTraversal {
+
+void appendContents(const ContainerNode* root, StringBuilder& result)
+{
+    for (Text* text = TextNodeTraversal::firstWithin(root); text; text = TextNodeTraversal::next(text, root))
+        result.append(text->data());
+}
+
+String contentsAsString(const ContainerNode* root)
+{
+    StringBuilder result;
+    appendContents(root, result);
+    return result.toString();
+}
+
+String contentsAsString(const Node* root)
+{
+    if (root->isTextNode())
+        return toText(root)->data();
+    if (root->isContainerNode())
+        return contentsAsString(toContainerNode(root));
+    return String();
+}
+
+}
+}