c8de033d0e7d07265cb01d94a845fb1984561ae6
[WebKit-https.git] / Source / WebCore / dom / PositionIterator.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  *
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "PositionIterator.h"
28
29 #include "HTMLBodyElement.h"
30 #include "HTMLElement.h"
31 #include "HTMLHtmlElement.h"
32 #include "HTMLNames.h"
33 #include "RenderBlockFlow.h"
34 #include "RenderText.h"
35 #include "htmlediting.h"
36
37 namespace WebCore {
38
39 using namespace HTMLNames;
40
41 PositionIterator::operator Position() const
42 {
43     if (m_nodeAfterPositionInAnchor) {
44         ASSERT(m_nodeAfterPositionInAnchor->parentNode() == m_anchorNode);
45         // FIXME: This check is inadaquete because any ancestor could be ignored by editing
46         if (editingIgnoresContent(m_nodeAfterPositionInAnchor->parentNode()))
47             return positionBeforeNode(m_anchorNode);
48         return positionInParentBeforeNode(m_nodeAfterPositionInAnchor);
49     }
50     if (m_anchorNode->hasChildNodes())
51         return lastPositionInOrAfterNode(m_anchorNode);
52     return createLegacyEditingPosition(m_anchorNode, m_offsetInAnchor);
53 }
54
55 void PositionIterator::increment()
56 {
57     if (!m_anchorNode)
58         return;
59
60     if (m_nodeAfterPositionInAnchor) {
61         m_anchorNode = m_nodeAfterPositionInAnchor;
62         m_nodeAfterPositionInAnchor = m_anchorNode->firstChild();
63         m_offsetInAnchor = 0;
64         return;
65     }
66
67     if (!m_anchorNode->hasChildNodes() && m_offsetInAnchor < lastOffsetForEditing(m_anchorNode))
68         m_offsetInAnchor = Position::uncheckedNextOffset(m_anchorNode, m_offsetInAnchor);
69     else {
70         m_nodeAfterPositionInAnchor = m_anchorNode;
71         m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
72         m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->nextSibling();
73         m_offsetInAnchor = 0;
74     }
75 }
76
77 void PositionIterator::decrement()
78 {
79     if (!m_anchorNode)
80         return;
81
82     if (m_nodeAfterPositionInAnchor) {
83         m_anchorNode = m_nodeAfterPositionInAnchor->previousSibling();
84         if (m_anchorNode) {
85             m_nodeAfterPositionInAnchor = 0;
86             m_offsetInAnchor = m_anchorNode->hasChildNodes() ? 0 : lastOffsetForEditing(m_anchorNode);
87         } else {
88             m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->parentNode();
89             m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
90             m_offsetInAnchor = 0;
91         }
92         return;
93     }
94     
95     if (m_anchorNode->hasChildNodes()) {
96         m_anchorNode = m_anchorNode->lastChild();
97         m_offsetInAnchor = m_anchorNode->hasChildNodes()? 0: lastOffsetForEditing(m_anchorNode);
98     } else {
99         if (m_offsetInAnchor)
100             m_offsetInAnchor = Position::uncheckedPreviousOffset(m_anchorNode, m_offsetInAnchor);
101         else {
102             m_nodeAfterPositionInAnchor = m_anchorNode;
103             m_anchorNode = m_anchorNode->parentNode();
104         }
105     }
106 }
107
108 bool PositionIterator::atStart() const
109 {
110     if (!m_anchorNode)
111         return true;
112     if (m_anchorNode->parentNode())
113         return false;
114     return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
115 }
116
117 bool PositionIterator::atEnd() const
118 {
119     if (!m_anchorNode)
120         return true;
121     if (m_nodeAfterPositionInAnchor)
122         return false;
123     return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
124 }
125
126 bool PositionIterator::atStartOfNode() const
127 {
128     if (!m_anchorNode)
129         return true;
130     if (!m_nodeAfterPositionInAnchor)
131         return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
132     return !m_nodeAfterPositionInAnchor->previousSibling();
133 }
134
135 bool PositionIterator::atEndOfNode() const
136 {
137     if (!m_anchorNode)
138         return true;
139     if (m_nodeAfterPositionInAnchor)
140         return false;
141     return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
142 }
143
144 bool PositionIterator::isCandidate() const
145 {
146     if (!m_anchorNode)
147         return false;
148
149     RenderObject* renderer = m_anchorNode->renderer();
150     if (!renderer)
151         return false;
152     
153     if (renderer->style().visibility() != VISIBLE)
154         return false;
155
156     if (renderer->isBR())
157         return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
158
159     if (is<RenderText>(*renderer))
160         return !Position::nodeIsUserSelectNone(m_anchorNode) && downcast<RenderText>(*renderer).containsCaretOffset(m_offsetInAnchor);
161
162     if (isRenderedTable(m_anchorNode) || editingIgnoresContent(m_anchorNode))
163         return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
164
165     if (!is<HTMLHtmlElement>(*m_anchorNode) && is<RenderBlockFlow>(*renderer)) {
166         RenderBlockFlow& block = downcast<RenderBlockFlow>(*renderer);
167         if (block.logicalHeight() || is<HTMLBodyElement>(*m_anchorNode)) {
168             if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(block))
169                 return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode);
170             return m_anchorNode->hasEditableStyle() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary();
171         }
172     }
173
174     return false;
175 }
176
177 } // namespace WebCore