PositionIterator.cpp 6.33 KB
Newer Older
weinig's avatar
weinig committed
1
/*
2
 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
weinig's avatar
weinig committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "PositionIterator.h"

29
#include "HTMLNames.h"
weinig's avatar
weinig committed
30
#include "Node.h"
hyatt@apple.com's avatar
hyatt@apple.com committed
31
#include "RenderBlock.h"
weinig's avatar
weinig committed
32 33 34 35 36 37
#include "htmlediting.h"

namespace WebCore {

using namespace HTMLNames;

darin@apple.com's avatar
darin@apple.com committed
38 39
PositionIterator::operator Position() const
{
40 41
    if (m_nodeAfterPositionInAnchor) {
        ASSERT(m_nodeAfterPositionInAnchor->parentNode() == m_anchorNode);
42 43 44
        // FIXME: This check is inadaquete because any ancestor could be ignored by editing
        if (editingIgnoresContent(m_nodeAfterPositionInAnchor->parentNode()))
            return positionBeforeNode(m_anchorNode);
45
        return positionInParentBeforeNode(m_nodeAfterPositionInAnchor);
eric@webkit.org's avatar
eric@webkit.org committed
46
    }
47
    if (m_anchorNode->hasChildNodes())
48
        return lastPositionInOrAfterNode(m_anchorNode);
49
    return Position(m_anchorNode, m_offsetInAnchor);
darin@apple.com's avatar
darin@apple.com committed
50 51
}

weinig's avatar
weinig committed
52 53
void PositionIterator::increment()
{
54
    if (!m_anchorNode)
weinig's avatar
weinig committed
55 56
        return;

57 58 59 60
    if (m_nodeAfterPositionInAnchor) {
        m_anchorNode = m_nodeAfterPositionInAnchor;
        m_nodeAfterPositionInAnchor = m_anchorNode->firstChild();
        m_offsetInAnchor = 0;
weinig's avatar
weinig committed
61 62 63
        return;
    }

64 65
    if (!m_anchorNode->hasChildNodes() && m_offsetInAnchor < lastOffsetForEditing(m_anchorNode))
        m_offsetInAnchor = Position::uncheckedNextOffset(m_anchorNode, m_offsetInAnchor);
weinig's avatar
weinig committed
66
    else {
67 68 69 70
        m_nodeAfterPositionInAnchor = m_anchorNode;
        m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
        m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->nextSibling();
        m_offsetInAnchor = 0;
weinig's avatar
weinig committed
71 72 73 74 75
    }
}

void PositionIterator::decrement()
{
76
    if (!m_anchorNode)
weinig's avatar
weinig committed
77 78
        return;

79 80 81 82 83
    if (m_nodeAfterPositionInAnchor) {
        m_anchorNode = m_nodeAfterPositionInAnchor->previousSibling();
        if (m_anchorNode) {
            m_nodeAfterPositionInAnchor = 0;
            m_offsetInAnchor = m_anchorNode->hasChildNodes() ? 0 : lastOffsetForEditing(m_anchorNode);
weinig's avatar
weinig committed
84
        } else {
85 86 87
            m_nodeAfterPositionInAnchor = m_nodeAfterPositionInAnchor->parentNode();
            m_anchorNode = m_nodeAfterPositionInAnchor->parentNode();
            m_offsetInAnchor = 0;
weinig's avatar
weinig committed
88 89 90
        }
        return;
    }
91 92 93 94
    
    if (m_anchorNode->hasChildNodes()) {
        m_anchorNode = m_anchorNode->lastChild();
        m_offsetInAnchor = m_anchorNode->hasChildNodes()? 0: lastOffsetForEditing(m_anchorNode);
weinig's avatar
weinig committed
95
    } else {
96 97 98
        if (m_offsetInAnchor)
            m_offsetInAnchor = Position::uncheckedPreviousOffset(m_anchorNode, m_offsetInAnchor);
        else {
99 100
            m_nodeAfterPositionInAnchor = m_anchorNode;
            m_anchorNode = m_anchorNode->parentNode();
weinig's avatar
weinig committed
101 102 103 104
        }
    }
}

105 106 107 108 109 110 111
void PositionIterator::setOffsetInLeafNode(int offset)
{
    ASSERT(!m_anchorNode->hasChildNodes());
    ASSERT(0 <= offset && offset <= lastOffsetForEditing(m_anchorNode));
    m_offsetInAnchor = offset;
}

weinig's avatar
weinig committed
112 113
bool PositionIterator::atStart() const
{
114
    if (!m_anchorNode)
weinig's avatar
weinig committed
115
        return true;
116
    if (m_anchorNode->parentNode())
weinig's avatar
weinig committed
117
        return false;
118
    return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling());
weinig's avatar
weinig committed
119 120 121 122
}

bool PositionIterator::atEnd() const
{
123
    if (!m_anchorNode)
weinig's avatar
weinig committed
124
        return true;
125
    if (m_nodeAfterPositionInAnchor)
weinig's avatar
weinig committed
126
        return false;
127
    return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode));
weinig's avatar
weinig committed
128 129 130 131
}

bool PositionIterator::atStartOfNode() const
{
132
    if (!m_anchorNode)
weinig's avatar
weinig committed
133
        return true;
134 135 136
    if (!m_nodeAfterPositionInAnchor)
        return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor;
    return !m_nodeAfterPositionInAnchor->previousSibling();
weinig's avatar
weinig committed
137 138 139 140
}

bool PositionIterator::atEndOfNode() const
{
141
    if (!m_anchorNode)
weinig's avatar
weinig committed
142
        return true;
143
    if (m_nodeAfterPositionInAnchor)
weinig's avatar
weinig committed
144
        return false;
145
    return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode);
weinig's avatar
weinig committed
146 147 148 149
}

bool PositionIterator::isCandidate() const
{
150
    if (!m_anchorNode)
weinig's avatar
weinig committed
151 152
        return false;

153
    RenderObject* renderer = m_anchorNode->renderer();
weinig's avatar
weinig committed
154 155 156 157 158 159 160
    if (!renderer)
        return false;
    
    if (renderer->style()->visibility() != VISIBLE)
        return false;

    if (renderer->isBR())
161
        return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
weinig's avatar
weinig committed
162 163

    if (renderer->isText())
164
        return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText();
weinig's avatar
weinig committed
165

166
    if (isTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode))
167
        return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode());
weinig's avatar
weinig committed
168

169 170 171 172
    if (!m_anchorNode->hasTagName(htmlTag) && renderer->isBlockFlow()) {
        if (toRenderBlock(renderer)->height() || m_anchorNode->hasTagName(bodyTag)) {
            if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer))
                return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode);
173
            return m_anchorNode->rendererIsEditable() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary();
174
        }
175
    }
176

weinig's avatar
weinig committed
177 178 179 180
    return false;
}

} // namespace WebCore