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
bool PositionIterator::atStart() const
109
if (m_anchorNode->parentNode())
111
return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
114
bool PositionIterator::atEnd() const
118
if (m_nodeAfterPositionInAnchor)
120
return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
123
bool PositionIterator::atStartOfNode() const
127
if (!m_nodeAfterPositionInAnchor)
128
return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
129
return !m_nodeAfterPositionInAnchor->previousSibling();
132
bool PositionIterator::atEndOfNode() const
136
if (m_nodeAfterPositionInAnchor)
138
return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
141
bool PositionIterator::isCandidate() const
146
RenderObject* renderer = m_anchorNode->renderer();
150
if (renderer->style()->visibility() != VISIBLE)
153
if (renderer->isBR())
154
return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
156
if (renderer->isText())
157
return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText();
159
if (isTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode))
160
return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
162
if (!m_anchorNode->hasTagName(htmlTag) && renderer->isBlockFlow()) {
163
if (toRenderBlock(renderer)->height() || m_anchorNode->hasTagName(bodyTag)) {
164
if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer))
165
return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode);
166
return m_anchorNode->rendererIsEditable() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary();
173
} // namespace WebCore