File indexing completed on 2024-05-05 16:11:41

0001 /*
0002  * Copyright (C) 2004 Apple Computer, Inc.  All rights reserved.
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0014  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0016  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0017  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0018  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0020  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0021  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0023  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include "dom_positioniterator.h"
0027 
0028 #include "dom_nodeimpl.h"
0029 
0030 namespace DOM
0031 {
0032 
0033 Position PositionIterator::peekPrevious() const
0034 {
0035     Position pos = m_current;
0036 
0037     if (pos.isEmpty()) {
0038         return pos;
0039     }
0040 
0041     if (pos.offset() <= 0) {
0042         NodeImpl *prevNode = pos.node()->previousLeafNode();
0043         if (prevNode) {
0044             pos = Position(prevNode, prevNode->maxOffset());
0045         }
0046     } else {
0047         pos = Position(pos.node(), pos.offset() - 1);
0048     }
0049 
0050     return pos;
0051 }
0052 
0053 Position PositionIterator::peekNext() const
0054 {
0055     Position pos = m_current;
0056 
0057     if (pos.isEmpty()) {
0058         return pos;
0059     }
0060 
0061     if (pos.offset() >= pos.node()->maxOffset()) {
0062         NodeImpl *nextNode = pos.node()->nextLeafNode();
0063         if (nextNode) {
0064             pos = Position(nextNode, 0);
0065         }
0066     } else {
0067         pos = Position(pos.node(), pos.offset() + 1);
0068     }
0069 
0070     return pos;
0071 }
0072 
0073 bool PositionIterator::atStart() const
0074 {
0075     if (m_current.isEmpty()) {
0076         return true;
0077     }
0078 
0079     return m_current.offset() == 0 &&
0080            m_current.node()->previousLeafNode() == nullptr;
0081 }
0082 
0083 bool PositionIterator::atEnd() const
0084 {
0085     if (m_current.isEmpty()) {
0086         return true;
0087     }
0088 
0089     return m_current.offset() == m_current.node()->maxOffset() &&
0090            m_current.node()->nextLeafNode() == nullptr;
0091 }
0092 
0093 } // namespace DOM