]>
Commit | Line | Data |
---|---|---|
a90939db JF |
1 | /* |
2 | * Copyright (C) 2008 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 | #ifndef RangeBoundaryPoint_h | |
27 | #define RangeBoundaryPoint_h | |
28 | ||
29 | #include "Node.h" | |
30 | #include "Position.h" | |
31 | ||
32 | namespace WebCore { | |
33 | ||
34 | class RangeBoundaryPoint { | |
35 | public: | |
36 | RangeBoundaryPoint(); | |
37 | explicit RangeBoundaryPoint(PassRefPtr<Node> container); | |
38 | ||
39 | const Position& position() const; | |
40 | Node* container() const; | |
41 | int offset() const; | |
42 | Node* childBefore() const; | |
43 | ||
44 | void clear(); | |
45 | ||
46 | void set(PassRefPtr<Node> container, int offset, Node* childBefore); | |
47 | void setOffset(int offset); | |
48 | void setToChild(Node* child); | |
49 | void setToStart(PassRefPtr<Node> container); | |
50 | void setToEnd(PassRefPtr<Node> container); | |
51 | ||
52 | void childBeforeWillBeRemoved(); | |
53 | void invalidateOffset() const; | |
54 | ||
55 | private: | |
56 | static const int invalidOffset = -1; | |
57 | ||
58 | mutable Position m_position; | |
59 | Node* m_childBefore; | |
60 | }; | |
61 | ||
62 | inline RangeBoundaryPoint::RangeBoundaryPoint() | |
63 | : m_childBefore(0) | |
64 | { | |
65 | } | |
66 | ||
67 | inline RangeBoundaryPoint::RangeBoundaryPoint(PassRefPtr<Node> container) | |
68 | : m_position(container, 0) | |
69 | , m_childBefore(0) | |
70 | { | |
71 | } | |
72 | ||
73 | inline Node* RangeBoundaryPoint::container() const | |
74 | { | |
75 | return m_position.container.get(); | |
76 | } | |
77 | ||
78 | inline Node* RangeBoundaryPoint::childBefore() const | |
79 | { | |
80 | return m_childBefore; | |
81 | } | |
82 | ||
83 | inline const Position& RangeBoundaryPoint::position() const | |
84 | { | |
85 | if (m_position.posOffset >= 0) | |
86 | return m_position; | |
87 | ASSERT(m_childBefore); | |
88 | m_position.posOffset = m_childBefore->nodeIndex() + 1; | |
89 | return m_position; | |
90 | } | |
91 | ||
92 | inline int RangeBoundaryPoint::offset() const | |
93 | { | |
94 | return position().posOffset; | |
95 | } | |
96 | ||
97 | inline void RangeBoundaryPoint::clear() | |
98 | { | |
99 | m_position.clear(); | |
100 | m_childBefore = 0; | |
101 | } | |
102 | ||
103 | inline void RangeBoundaryPoint::set(PassRefPtr<Node> container, int offset, Node* childBefore) | |
104 | { | |
105 | ASSERT(offset >= 0); | |
106 | ASSERT(childBefore == (offset ? container->childNode(offset - 1) : 0)); | |
107 | m_position.container = container; | |
108 | m_position.posOffset = offset; | |
109 | m_childBefore = childBefore; | |
110 | } | |
111 | ||
112 | inline void RangeBoundaryPoint::setOffset(int offset) | |
113 | { | |
114 | ASSERT(m_position.container); | |
115 | ASSERT(m_position.container->offsetInCharacters()); | |
116 | ASSERT(m_position.posOffset >= 0); | |
117 | ASSERT(!m_childBefore); | |
118 | m_position.posOffset = offset; | |
119 | } | |
120 | ||
121 | inline void RangeBoundaryPoint::setToChild(Node* child) | |
122 | { | |
123 | ASSERT(child); | |
124 | ASSERT(child->parentNode()); | |
125 | m_position.container = child->parentNode(); | |
126 | m_childBefore = child->previousSibling(); | |
127 | m_position.posOffset = m_childBefore ? invalidOffset : 0; | |
128 | } | |
129 | ||
130 | inline void RangeBoundaryPoint::setToStart(PassRefPtr<Node> container) | |
131 | { | |
132 | ASSERT(container); | |
133 | m_position.container = container; | |
134 | m_position.posOffset = 0; | |
135 | m_childBefore = 0; | |
136 | } | |
137 | ||
138 | inline void RangeBoundaryPoint::setToEnd(PassRefPtr<Node> container) | |
139 | { | |
140 | ASSERT(container); | |
141 | m_position.container = container; | |
142 | if (m_position.container->offsetInCharacters()) { | |
143 | m_position.posOffset = m_position.container->maxCharacterOffset(); | |
144 | m_childBefore = 0; | |
145 | } else { | |
146 | m_childBefore = m_position.container->lastChild(); | |
147 | m_position.posOffset = m_childBefore ? invalidOffset : 0; | |
148 | } | |
149 | } | |
150 | ||
151 | inline void RangeBoundaryPoint::childBeforeWillBeRemoved() | |
152 | { | |
153 | ASSERT(m_position.posOffset); | |
154 | m_childBefore = m_childBefore->previousSibling(); | |
155 | if (!m_childBefore) | |
156 | m_position.posOffset = 0; | |
157 | else if (m_position.posOffset > 0) | |
158 | --m_position.posOffset; | |
159 | } | |
160 | ||
161 | inline void RangeBoundaryPoint::invalidateOffset() const | |
162 | { | |
163 | m_position.posOffset = invalidOffset; | |
164 | } | |
165 | ||
166 | inline bool operator==(const RangeBoundaryPoint& a, const RangeBoundaryPoint& b) | |
167 | { | |
168 | if (a.container() != b.container()) | |
169 | return false; | |
170 | if (a.childBefore() || b.childBefore()) { | |
171 | if (a.childBefore() != b.childBefore()) | |
172 | return false; | |
173 | } else { | |
174 | if (a.offset() != b.offset()) | |
175 | return false; | |
176 | } | |
177 | return true; | |
178 | } | |
179 | ||
180 | } | |
181 | ||
182 | #endif |