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 "core/dom/PositionIterator.h"
29 #include "HTMLNames.h"
30 #include "core/editing/htmlediting.h"
31 #include "core/rendering/RenderBlock.h"
35 using namespace HTMLNames;
37 PositionIterator::operator Position() const
39 if (m_nodeAfterPositionInAnchor) {
40 ASSERT(m_nodeAfterPositionInAnchor->parentNode() == m_anchorNode);
41 // FIXME: This check is inadaquete because any ancestor could be ignored by editing
42 if (editingIgnoresContent(m_nodeAfterPositionInAnchor->parentNode()))
43 return positionBeforeNode(m_anchorNode);
44 return positionInParentBeforeNode(m_nodeAfterPositionInAnchor);
46 if (m_anchorNode->hasChildNodes())
47 return lastPositionInOrAfterNode(m_anchorNode);
48 return createLegacyEditingPosition(m_anchorNode, m_offsetInAnchor);
51 void PositionIterator::increment()
56 if (m_nodeAfterPositionInAnchor) {
57 m_anchorNode = m_nodeAfterPositionInAnchor;
58 m_nodeAfterPositionInAnchor = m_anchorNode->firstChild();
63 if (!m_anchorNode->hasChildNodes() && m_offsetInAnchor < lastOffsetForEditing(m_anchorNode))
64 m_offsetInAnchor = Position::uncheckedNextOffset(m_anchorNode, m_offsetInAnchor);
66 m_nodeAfterPositionInAnchor = m_anchorNode;
67 m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
68 m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->nextSibling();
73 void PositionIterator::decrement()
78 if (m_nodeAfterPositionInAnchor) {
79 m_anchorNode = m_nodeAfterPositionInAnchor->previousSibling();
81 m_nodeAfterPositionInAnchor = 0;
82 m_offsetInAnchor = m_anchorNode->hasChildNodes() ? 0 : lastOffsetForEditing(m_anchorNode);
84 m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->parentNode();
85 m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
91 if (m_anchorNode->hasChildNodes()) {
92 m_anchorNode = m_anchorNode->lastChild();
93 m_offsetInAnchor = m_anchorNode->hasChildNodes()? 0: lastOffsetForEditing(m_anchorNode);
96 m_offsetInAnchor = Position::uncheckedPreviousOffset(m_anchorNode, m_offsetInAnchor);
98 m_nodeAfterPositionInAnchor = m_anchorNode;
99 m_anchorNode = m_anchorNode->parentNode();
104 bool PositionIterator::atStart() const
108 if (m_anchorNode->parentNode())
110 return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
113 bool PositionIterator::atEnd() const
117 if (m_nodeAfterPositionInAnchor)
119 return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
122 bool PositionIterator::atStartOfNode() const
126 if (!m_nodeAfterPositionInAnchor)
127 return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
128 return !m_nodeAfterPositionInAnchor->previousSibling();
131 bool PositionIterator::atEndOfNode() const
135 if (m_nodeAfterPositionInAnchor)
137 return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
140 bool PositionIterator::isCandidate() const
145 RenderObject* renderer = m_anchorNode->renderer();
149 if (renderer->style()->visibility() != VISIBLE)
152 if (renderer->isBR())
153 return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
155 if (renderer->isText())
156 return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText();
158 if (isTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode))
159 return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
161 if (!m_anchorNode->hasTagName(htmlTag) && renderer->isRenderBlockFlow()) {
162 if (toRenderBlock(renderer)->logicalHeight() || m_anchorNode->hasTagName(bodyTag)) {
163 if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer))
164 return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode);
165 return m_anchorNode->rendererIsEditable() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary();
172 } // namespace WebCore