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"
29 #include "HTMLNames.h"
31 #include "RenderBlock.h"
32 #include "htmlediting.h"
36 using namespace HTMLNames;
38 PositionIterator::operator Position() const
40 if (m_nodeAfterPositionInAnchor) {
41 ASSERT(m_nodeAfterPositionInAnchor->parentNode() == m_anchorNode);
42 // FIXME: This check is inadaquete because any ancestor could be ignored by editing
43 if (editingIgnoresContent(m_nodeAfterPositionInAnchor->parentNode()))
44 return positionBeforeNode(m_anchorNode);
45 return positionInParentBeforeNode(m_nodeAfterPositionInAnchor);
47 if (m_anchorNode->hasChildNodes())
48 return lastPositionInOrAfterNode(m_anchorNode);
49 return createLegacyEditingPosition(m_anchorNode, m_offsetInAnchor);
52 void PositionIterator::increment()
57 if (m_nodeAfterPositionInAnchor) {
58 m_anchorNode = m_nodeAfterPositionInAnchor;
59 m_nodeAfterPositionInAnchor = m_anchorNode->firstChild();
64 if (!m_anchorNode->hasChildNodes() && m_offsetInAnchor < lastOffsetForEditing(m_anchorNode))
65 m_offsetInAnchor = Position::uncheckedNextOffset(m_anchorNode, m_offsetInAnchor);
67 m_nodeAfterPositionInAnchor = m_anchorNode;
68 m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
69 m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->nextSibling();
74 void PositionIterator::decrement()
79 if (m_nodeAfterPositionInAnchor) {
80 m_anchorNode = m_nodeAfterPositionInAnchor->previousSibling();
82 m_nodeAfterPositionInAnchor = 0;
83 m_offsetInAnchor = m_anchorNode->hasChildNodes() ? 0 : lastOffsetForEditing(m_anchorNode);
85 m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->parentNode();
86 m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
92 if (m_anchorNode->hasChildNodes()) {
93 m_anchorNode = m_anchorNode->lastChild();
94 m_offsetInAnchor = m_anchorNode->hasChildNodes()? 0: lastOffsetForEditing(m_anchorNode);
97 m_offsetInAnchor = Position::uncheckedPreviousOffset(m_anchorNode, m_offsetInAnchor);
99 m_nodeAfterPositionInAnchor = m_anchorNode;
100 m_anchorNode = m_anchorNode->parentNode();
105 void PositionIterator::setOffsetInLeafNode(int offset)
107 ASSERT(!m_anchorNode->hasChildNodes());
108 ASSERT(0 <= offset && offset <= lastOffsetForEditing(m_anchorNode));
109 m_offsetInAnchor = offset;
112 bool PositionIterator::atStart() const
116 if (m_anchorNode->parentNode())
118 return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
121 bool PositionIterator::atEnd() const
125 if (m_nodeAfterPositionInAnchor)
127 return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
130 bool PositionIterator::atStartOfNode() const
134 if (!m_nodeAfterPositionInAnchor)
135 return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
136 return !m_nodeAfterPositionInAnchor->previousSibling();
139 bool PositionIterator::atEndOfNode() const
143 if (m_nodeAfterPositionInAnchor)
145 return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
148 bool PositionIterator::isCandidate() const
153 RenderObject* renderer = m_anchorNode->renderer();
157 if (renderer->style()->visibility() != VISIBLE)
160 if (renderer->isBR())
161 return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
163 if (renderer->isText())
164 return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText();
166 if (isTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode))
167 return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
169 if (!m_anchorNode->hasTagName(htmlTag) && renderer->isBlockFlow()) {
170 if (toRenderBlock(renderer)->height() || m_anchorNode->hasTagName(bodyTag)) {
171 if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer))
172 return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode);
173 return m_anchorNode->rendererIsEditable() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary();
180 } // namespace WebCore