Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/dom/PositionIterator.h"
28
29 #include "HTMLNames.h"
30 #include "core/editing/htmlediting.h"
31 #include "core/rendering/RenderBlock.h"
32
33 namespace WebCore {
34
35 using namespace HTMLNames;
36
37 PositionIterator::operator Position() const
38 {
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);
45     }
46     if (m_anchorNode->hasChildNodes())
47         return lastPositionInOrAfterNode(m_anchorNode);
48     return createLegacyEditingPosition(m_anchorNode, m_offsetInAnchor);
49 }
50
51 void PositionIterator::increment()
52 {
53     if (!m_anchorNode)
54         return;
55
56     if (m_nodeAfterPositionInAnchor) {
57         m_anchorNode = m_nodeAfterPositionInAnchor;
58         m_nodeAfterPositionInAnchor = m_anchorNode->firstChild();
59         m_offsetInAnchor = 0;
60         return;
61     }
62
63     if (!m_anchorNode->hasChildNodes() && m_offsetInAnchor < lastOffsetForEditing(m_anchorNode))
64         m_offsetInAnchor = Position::uncheckedNextOffset(m_anchorNode, m_offsetInAnchor);
65     else {
66         m_nodeAfterPositionInAnchor = m_anchorNode;
67         m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
68         m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->nextSibling();
69         m_offsetInAnchor = 0;
70     }
71 }
72
73 void PositionIterator::decrement()
74 {
75     if (!m_anchorNode)
76         return;
77
78     if (m_nodeAfterPositionInAnchor) {
79         m_anchorNode = m_nodeAfterPositionInAnchor->previousSibling();
80         if (m_anchorNode) {
81             m_nodeAfterPositionInAnchor = 0;
82             m_offsetInAnchor = m_anchorNode->hasChildNodes() ? 0 : lastOffsetForEditing(m_anchorNode);
83         } else {
84             m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->parentNode();
85             m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
86             m_offsetInAnchor = 0;
87         }
88         return;
89     }
90
91     if (m_anchorNode->hasChildNodes()) {
92         m_anchorNode = m_anchorNode->lastChild();
93         m_offsetInAnchor = m_anchorNode->hasChildNodes()? 0: lastOffsetForEditing(m_anchorNode);
94     } else {
95         if (m_offsetInAnchor)
96             m_offsetInAnchor = Position::uncheckedPreviousOffset(m_anchorNode, m_offsetInAnchor);
97         else {
98             m_nodeAfterPositionInAnchor = m_anchorNode;
99             m_anchorNode = m_anchorNode->parentNode();
100         }
101     }
102 }
103
104 bool PositionIterator::atStart() const
105 {
106     if (!m_anchorNode)
107         return true;
108     if (m_anchorNode->parentNode())
109         return false;
110     return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
111 }
112
113 bool PositionIterator::atEnd() const
114 {
115     if (!m_anchorNode)
116         return true;
117     if (m_nodeAfterPositionInAnchor)
118         return false;
119     return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
120 }
121
122 bool PositionIterator::atStartOfNode() const
123 {
124     if (!m_anchorNode)
125         return true;
126     if (!m_nodeAfterPositionInAnchor)
127         return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
128     return !m_nodeAfterPositionInAnchor->previousSibling();
129 }
130
131 bool PositionIterator::atEndOfNode() const
132 {
133     if (!m_anchorNode)
134         return true;
135     if (m_nodeAfterPositionInAnchor)
136         return false;
137     return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
138 }
139
140 bool PositionIterator::isCandidate() const
141 {
142     if (!m_anchorNode)
143         return false;
144
145     RenderObject* renderer = m_anchorNode->renderer();
146     if (!renderer)
147         return false;
148
149     if (renderer->style()->visibility() != VISIBLE)
150         return false;
151
152     if (renderer->isBR())
153         return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
154
155     if (renderer->isText())
156         return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText();
157
158     if (isRenderedTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode))
159         return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
160
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();
166         }
167     }
168
169     return false;
170 }
171
172 } // namespace WebCore