2 * Copyright (C) 2007, 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.
27 #include "PositionIterator.h"
30 #include "RenderObject.h"
31 #include "htmlediting.h"
35 using namespace HTMLNames
;
37 PositionIterator::operator Position() const
39 return Position(m_parent
, m_child
? m_child
->nodeIndex() : (m_parent
->hasChildNodes() ? maxDeepOffset(m_parent
) : m_offset
));
42 void PositionIterator::increment()
49 m_child
= m_parent
->firstChild();
54 if (!m_parent
->hasChildNodes() && m_offset
< maxDeepOffset(m_parent
))
55 m_offset
= Position::uncheckedNextOffset(m_parent
, m_offset
);
58 m_parent
= m_child
->parentNode();
59 m_child
= m_child
->nextSibling();
64 void PositionIterator::decrement()
70 m_parent
= m_child
->previousSibling();
73 m_offset
= m_parent
->hasChildNodes() ? 0 : maxDeepOffset(m_parent
);
75 m_child
= m_child
->parentNode();
76 m_parent
= m_child
->parentNode();
83 m_offset
= Position::uncheckedPreviousOffset(m_parent
, m_offset
);
85 if (m_parent
->hasChildNodes()) {
86 m_parent
= m_parent
->lastChild();
87 if (!m_parent
->hasChildNodes())
88 m_offset
= maxDeepOffset(m_parent
);
91 m_parent
= m_parent
->parentNode();
96 bool PositionIterator::atStart() const
100 if (m_parent
->parentNode())
102 return !m_parent
->hasChildNodes() && !m_offset
|| m_child
&& !m_child
->previousSibling();
105 bool PositionIterator::atEnd() const
111 return !m_parent
->parentNode() && (m_parent
->hasChildNodes() || m_offset
>= maxDeepOffset(m_parent
));
114 bool PositionIterator::atStartOfNode() const
119 return !m_parent
->hasChildNodes() && !m_offset
;
120 return !m_child
->previousSibling();
123 bool PositionIterator::atEndOfNode() const
129 return m_parent
->hasChildNodes() || m_offset
>= maxDeepOffset(m_parent
);
132 bool PositionIterator::isCandidate() const
137 RenderObject
* renderer
= m_parent
->renderer();
141 if (renderer
->style()->visibility() != VISIBLE
)
144 if (renderer
->isBR())
145 return !m_offset
&& !Position::nodeIsUserSelectNone(m_parent
->parent());
147 if (renderer
->isText())
148 return Position(*this).inRenderedText() && !Position::nodeIsUserSelectNone(m_parent
);
150 if (isTableElement(m_parent
) || editingIgnoresContent(m_parent
))
151 return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_parent
->parent());
153 if (!m_parent
->hasTagName(htmlTag
) && renderer
->isBlockFlow() && !Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer
) &&
154 (renderer
->height() || m_parent
->hasTagName(bodyTag
)))
155 return atStartOfNode() && !Position::nodeIsUserSelectNone(m_parent
);
160 } // namespace WebCore