]>
git.saurik.com Git - iphone-api.git/blob - WebCore/RangeBoundaryPoint.h
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef RangeBoundaryPoint_h
27 #define RangeBoundaryPoint_h
34 class RangeBoundaryPoint
{
37 explicit RangeBoundaryPoint(PassRefPtr
<Node
> container
);
39 const Position
& position() const;
40 Node
* container() const;
42 Node
* childBefore() const;
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
);
52 void childBeforeWillBeRemoved();
53 void invalidateOffset() const;
56 static const int invalidOffset
= -1;
58 mutable Position m_position
;
62 inline RangeBoundaryPoint::RangeBoundaryPoint()
67 inline RangeBoundaryPoint::RangeBoundaryPoint(PassRefPtr
<Node
> container
)
68 : m_position(container
, 0)
73 inline Node
* RangeBoundaryPoint::container() const
75 return m_position
.container
.get();
78 inline Node
* RangeBoundaryPoint::childBefore() const
83 inline const Position
& RangeBoundaryPoint::position() const
85 if (m_position
.posOffset
>= 0)
87 ASSERT(m_childBefore
);
88 m_position
.posOffset
= m_childBefore
->nodeIndex() + 1;
92 inline int RangeBoundaryPoint::offset() const
94 return position().posOffset
;
97 inline void RangeBoundaryPoint::clear()
103 inline void RangeBoundaryPoint::set(PassRefPtr
<Node
> container
, int offset
, Node
* childBefore
)
106 ASSERT(childBefore
== (offset
? container
->childNode(offset
- 1) : 0));
107 m_position
.container
= container
;
108 m_position
.posOffset
= offset
;
109 m_childBefore
= childBefore
;
112 inline void RangeBoundaryPoint::setOffset(int offset
)
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
;
121 inline void RangeBoundaryPoint::setToChild(Node
* child
)
124 ASSERT(child
->parentNode());
125 m_position
.container
= child
->parentNode();
126 m_childBefore
= child
->previousSibling();
127 m_position
.posOffset
= m_childBefore
? invalidOffset
: 0;
130 inline void RangeBoundaryPoint::setToStart(PassRefPtr
<Node
> container
)
133 m_position
.container
= container
;
134 m_position
.posOffset
= 0;
138 inline void RangeBoundaryPoint::setToEnd(PassRefPtr
<Node
> container
)
141 m_position
.container
= container
;
142 if (m_position
.container
->offsetInCharacters()) {
143 m_position
.posOffset
= m_position
.container
->maxCharacterOffset();
146 m_childBefore
= m_position
.container
->lastChild();
147 m_position
.posOffset
= m_childBefore
? invalidOffset
: 0;
151 inline void RangeBoundaryPoint::childBeforeWillBeRemoved()
153 ASSERT(m_position
.posOffset
);
154 m_childBefore
= m_childBefore
->previousSibling();
156 m_position
.posOffset
= 0;
157 else if (m_position
.posOffset
> 0)
158 --m_position
.posOffset
;
161 inline void RangeBoundaryPoint::invalidateOffset() const
163 m_position
.posOffset
= invalidOffset
;
166 inline bool operator==(const RangeBoundaryPoint
& a
, const RangeBoundaryPoint
& b
)
168 if (a
.container() != b
.container())
170 if (a
.childBefore() || b
.childBefore()) {
171 if (a
.childBefore() != b
.childBefore())
174 if (a
.offset() != b
.offset())