2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / PositionIterator.cpp
blob218ace16dd0bbbd87408265b41a98e2d919099a7
1 /*
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
6 * are met:
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 #include "config.h"
27 #include "PositionIterator.h"
29 #include "Node.h"
30 #include "RenderObject.h"
31 #include "htmlediting.h"
33 namespace WebCore {
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()
44 if (!m_parent)
45 return;
47 if (m_child) {
48 m_parent = m_child;
49 m_child = m_parent->firstChild();
50 m_offset = 0;
51 return;
54 if (!m_parent->hasChildNodes() && m_offset < maxDeepOffset(m_parent))
55 m_offset = Position::uncheckedNextOffset(m_parent, m_offset);
56 else {
57 m_child = m_parent;
58 m_parent = m_child->parentNode();
59 m_child = m_child->nextSibling();
60 m_offset = 0;
64 void PositionIterator::decrement()
66 if (!m_parent)
67 return;
69 if (m_child) {
70 m_parent = m_child->previousSibling();
71 if (m_parent) {
72 m_child = 0;
73 m_offset = m_parent->hasChildNodes() ? 0 : maxDeepOffset(m_parent);
74 } else {
75 m_child = m_child->parentNode();
76 m_parent = m_child->parentNode();
77 m_offset = 0;
79 return;
82 if (m_offset) {
83 m_offset = Position::uncheckedPreviousOffset(m_parent, m_offset);
84 } else {
85 if (m_parent->hasChildNodes()) {
86 m_parent = m_parent->lastChild();
87 if (!m_parent->hasChildNodes())
88 m_offset = maxDeepOffset(m_parent);
89 } else {
90 m_child = m_parent;
91 m_parent = m_parent->parentNode();
96 bool PositionIterator::atStart() const
98 if (!m_parent)
99 return true;
100 if (m_parent->parentNode())
101 return false;
102 return !m_parent->hasChildNodes() && !m_offset || m_child && !m_child->previousSibling();
105 bool PositionIterator::atEnd() const
107 if (!m_parent)
108 return true;
109 if (m_child)
110 return false;
111 return !m_parent->parentNode() && (m_parent->hasChildNodes() || m_offset >= maxDeepOffset(m_parent));
114 bool PositionIterator::atStartOfNode() const
116 if (!m_parent)
117 return true;
118 if (!m_child)
119 return !m_parent->hasChildNodes() && !m_offset;
120 return !m_child->previousSibling();
123 bool PositionIterator::atEndOfNode() const
125 if (!m_parent)
126 return true;
127 if (m_child)
128 return false;
129 return m_parent->hasChildNodes() || m_offset >= maxDeepOffset(m_parent);
132 bool PositionIterator::isCandidate() const
134 if (!m_parent)
135 return false;
137 RenderObject* renderer = m_parent->renderer();
138 if (!renderer)
139 return false;
141 if (renderer->style()->visibility() != VISIBLE)
142 return false;
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);
157 return false;
160 } // namespace WebCore