1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "NodeTraversal.h"
29#include "Text.h"
30
31namespace WTF {
32class StringBuilder;
33}
34
35namespace WebCore {
36namespace TextNodeTraversal {
37
38// First text child of the node.
39Text* firstChild(const Node&);
40Text* firstChild(const ContainerNode&);
41
42// First text descendant of the node.
43Text* firstWithin(const Node&);
44Text* firstWithin(const ContainerNode&);
45
46// Pre-order traversal skipping non-text nodes.
47Text* next(const Node&);
48Text* next(const Node&, const Node* stayWithin);
49Text* next(const Text&);
50Text* next(const Text&, const Node* stayWithin);
51
52// Next text sibling.
53Text* nextSibling(const Node&);
54
55// Concatenated text contents of a subtree.
56String contentsAsString(const Node&);
57String contentsAsString(const ContainerNode&);
58void appendContents(const ContainerNode&, StringBuilder& result);
59String childTextContent(const ContainerNode&);
60
61}
62
63namespace TextNodeTraversal {
64
65template <class NodeType>
66inline Text* firstTextChildTemplate(NodeType& current)
67{
68 Node* node = current.firstChild();
69 while (node && !is<Text>(*node))
70 node = node->nextSibling();
71 return downcast<Text>(node);
72}
73inline Text* firstChild(const Node& current) { return firstTextChildTemplate(current); }
74inline Text* firstChild(const ContainerNode& current) { return firstTextChildTemplate(current); }
75
76template <class NodeType>
77inline Text* firstTextWithinTemplate(NodeType& current)
78{
79 Node* node = current.firstChild();
80 while (node && !is<Text>(*node))
81 node = NodeTraversal::next(*node, &current);
82 return downcast<Text>(node);
83}
84inline Text* firstWithin(const Node& current) { return firstTextWithinTemplate(current); }
85inline Text* firstWithin(const ContainerNode& current) { return firstTextWithinTemplate(current); }
86
87template <class NodeType>
88inline Text* traverseNextTextTemplate(NodeType& current)
89{
90 Node* node = NodeTraversal::next(current);
91 while (node && !is<Text>(*node))
92 node = NodeTraversal::next(*node);
93 return downcast<Text>(node);
94}
95inline Text* next(const Node& current) { return traverseNextTextTemplate(current); }
96inline Text* next(const Text& current) { return traverseNextTextTemplate(current); }
97
98template <class NodeType>
99inline Text* traverseNextTextTemplate(NodeType& current, const Node* stayWithin)
100{
101 Node* node = NodeTraversal::next(current, stayWithin);
102 while (node && !is<Text>(*node))
103 node = NodeTraversal::next(*node, stayWithin);
104 return downcast<Text>(node);
105}
106inline Text* next(const Node& current, const Node* stayWithin) { return traverseNextTextTemplate(current, stayWithin); }
107inline Text* next(const Text& current, const Node* stayWithin) { return traverseNextTextTemplate(current, stayWithin); }
108
109inline Text* nextSibling(const Node& current)
110{
111 Node* node = current.nextSibling();
112 while (node && !is<Text>(*node))
113 node = node->nextSibling();
114 return downcast<Text>(node);
115}
116
117} // namespace TextNodeTraversal
118} // namespace WebCore
119