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 | |
31 | namespace WTF { |
32 | class StringBuilder; |
33 | } |
34 | |
35 | namespace WebCore { |
36 | namespace TextNodeTraversal { |
37 | |
38 | // First text child of the node. |
39 | Text* firstChild(const Node&); |
40 | Text* firstChild(const ContainerNode&); |
41 | |
42 | // First text descendant of the node. |
43 | Text* firstWithin(const Node&); |
44 | Text* firstWithin(const ContainerNode&); |
45 | |
46 | // Pre-order traversal skipping non-text nodes. |
47 | Text* next(const Node&); |
48 | Text* next(const Node&, const Node* stayWithin); |
49 | Text* next(const Text&); |
50 | Text* next(const Text&, const Node* stayWithin); |
51 | |
52 | // Next text sibling. |
53 | Text* nextSibling(const Node&); |
54 | |
55 | // Concatenated text contents of a subtree. |
56 | String contentsAsString(const Node&); |
57 | String contentsAsString(const ContainerNode&); |
58 | void appendContents(const ContainerNode&, StringBuilder& result); |
59 | String childTextContent(const ContainerNode&); |
60 | |
61 | } |
62 | |
63 | namespace TextNodeTraversal { |
64 | |
65 | template <class NodeType> |
66 | inline 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 | } |
73 | inline Text* firstChild(const Node& current) { return firstTextChildTemplate(current); } |
74 | inline Text* firstChild(const ContainerNode& current) { return firstTextChildTemplate(current); } |
75 | |
76 | template <class NodeType> |
77 | inline Text* firstTextWithinTemplate(NodeType& current) |
78 | { |
79 | Node* node = current.firstChild(); |
80 | while (node && !is<Text>(*node)) |
81 | node = NodeTraversal::next(*node, ¤t); |
82 | return downcast<Text>(node); |
83 | } |
84 | inline Text* firstWithin(const Node& current) { return firstTextWithinTemplate(current); } |
85 | inline Text* firstWithin(const ContainerNode& current) { return firstTextWithinTemplate(current); } |
86 | |
87 | template <class NodeType> |
88 | inline 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 | } |
95 | inline Text* next(const Node& current) { return traverseNextTextTemplate(current); } |
96 | inline Text* next(const Text& current) { return traverseNextTextTemplate(current); } |
97 | |
98 | template <class NodeType> |
99 | inline 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 | } |
106 | inline Text* next(const Node& current, const Node* stayWithin) { return traverseNextTextTemplate(current, stayWithin); } |
107 | inline Text* next(const Text& current, const Node* stayWithin) { return traverseNextTextTemplate(current, stayWithin); } |
108 | |
109 | inline 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 | |