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 "Node.h" 30 #include "RenderBlock.h" 31 #include "htmlediting.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 Position(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 void PositionIterator::setOffsetInLeafNode(int offset) 105 { 106 ASSERT(!m_anchorNode->hasChildNodes()); 107 ASSERT(0 <= offset && offset <= lastOffsetForEditing(m_anchorNode)); 108 m_offsetInAnchor = offset; 109 } 110 111 bool PositionIterator::atStart() const 112 { 113 if (!m_anchorNode) 114 return true; 115 if (m_anchorNode->parentNode()) 116 return false; 117 return (!m_anchorNode->hasChildNodes() && !m_offsetInAnchor) || (m_nodeAfterPositionInAnchor && !m_nodeAfterPositionInAnchor->previousSibling()); 118 } 119 120 bool PositionIterator::atEnd() const 121 { 122 if (!m_anchorNode) 123 return true; 124 if (m_nodeAfterPositionInAnchor) 125 return false; 126 return !m_anchorNode->parentNode() && (m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode)); 127 } 128 129 bool PositionIterator::atStartOfNode() const 130 { 131 if (!m_anchorNode) 132 return true; 133 if (!m_nodeAfterPositionInAnchor) 134 return !m_anchorNode->hasChildNodes() && !m_offsetInAnchor; 135 return !m_nodeAfterPositionInAnchor->previousSibling(); 136 } 137 138 bool PositionIterator::atEndOfNode() const 139 { 140 if (!m_anchorNode) 141 return true; 142 if (m_nodeAfterPositionInAnchor) 143 return false; 144 return m_anchorNode->hasChildNodes() || m_offsetInAnchor >= lastOffsetForEditing(m_anchorNode); 145 } 146 147 bool PositionIterator::isCandidate() const 148 { 149 if (!m_anchorNode) 150 return false; 151 152 RenderObject* renderer = m_anchorNode->renderer(); 153 if (!renderer) 154 return false; 155 156 if (renderer->style()->visibility() != VISIBLE) 157 return false; 158 159 if (renderer->isBR()) 160 return !m_offsetInAnchor && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode()); 161 162 if (renderer->isText()) 163 return !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).inRenderedText(); 164 165 if (isTableElement(m_anchorNode) || editingIgnoresContent(m_anchorNode)) 166 return (atStartOfNode() || atEndOfNode()) && !Position::nodeIsUserSelectNone(m_anchorNode->parentNode()); 167 168 if (!m_anchorNode->hasTagName(htmlTag) && renderer->isBlockFlow()) { 169 if (toRenderBlock(renderer)->height() || m_anchorNode->hasTagName(bodyTag)) { 170 if (!Position::hasRenderedNonAnonymousDescendantsWithHeight(renderer)) 171 return atStartOfNode() && !Position::nodeIsUserSelectNone(m_anchorNode); 172 return m_anchorNode->rendererIsEditable() && !Position::nodeIsUserSelectNone(m_anchorNode) && Position(*this).atEditingBoundary(); 173 } 174 } 175 176 return false; 177 } 178 179 } // namespace WebCore 180