File indexing completed on 2024-04-28 15:24:52

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 #ifndef __dom_position_h__
0027 #define __dom_position_h__
0028 
0029 #include <QDebug>
0030 
0031 namespace DOM
0032 {
0033 
0034 class ElementImpl;
0035 class NodeImpl;
0036 
0037 NodeImpl *rootNavigableElement(NodeImpl *node);
0038 bool inSameRootNavigableElement(NodeImpl *n1, NodeImpl *n2);
0039 
0040 void printEnclosingBlockTree(NodeImpl *node);
0041 void printRootEditableTree(NodeImpl *node);
0042 
0043 class Position
0044 {
0045 public:
0046     Position() : m_node(nullptr), m_offset(0) {}
0047     Position(NodeImpl *node, long offset);
0048     Position(const Position &);
0049     ~Position();
0050 
0051     NodeImpl *node() const
0052     {
0053         return m_node;
0054     }
0055     long offset() const
0056     {
0057         return m_offset;
0058     }
0059 
0060     ElementImpl *element() const;
0061 
0062     long renderedOffset() const;
0063 
0064     bool isEmpty() const
0065     {
0066         return m_node == nullptr;
0067     }
0068     bool notEmpty() const
0069     {
0070         return m_node != nullptr;
0071     }
0072 
0073     Position equivalentLeafPosition() const;
0074     Position previousRenderedEditablePosition() const;
0075     Position nextRenderedEditablePosition() const;
0076     Position previousCharacterPosition() const;
0077     Position nextCharacterPosition() const;
0078     Position previousWordPosition() const;
0079     Position nextWordPosition() const;
0080     Position previousLinePosition(int x) const;
0081     Position nextLinePosition(int x) const;
0082     Position equivalentUpstreamPosition() const;
0083     Position equivalentDownstreamPosition() const;
0084     Position equivalentRangeCompliantPosition() const;
0085     Position equivalentShallowPosition() const;
0086     bool atStartOfContainingEditableBlock() const;
0087     bool atStartOfRootEditableElement() const;
0088     bool inRenderedContent() const;
0089     bool inRenderedText() const;
0090     bool rendersOnSameLine(const Position &pos) const;
0091     bool rendersInDifferentPosition(const Position &pos) const;
0092     bool isFirstRenderedPositionOnLine() const;
0093     bool isLastRenderedPositionOnLine() const;
0094     bool isLastRenderedPositionInEditableBlock() const;
0095     bool inFirstEditableInRootEditableElement() const;
0096     bool inLastEditableInRootEditableElement() const;
0097     bool inFirstEditableInContainingEditableBlock() const;
0098     bool inLastEditableInContainingEditableBlock() const;
0099 
0100     Position &operator=(const Position &o);
0101 
0102     friend bool operator==(const Position &a, const Position &b);
0103     friend bool operator!=(const Position &a, const Position &b);
0104 
0105 private:
0106     NodeImpl *m_node;
0107     long m_offset;
0108 };
0109 
0110 inline bool operator==(const Position &a, const Position &b)
0111 {
0112     return a.node() == b.node() && a.offset() == b.offset();
0113 }
0114 
0115 inline bool operator!=(const Position &a, const Position &b)
0116 {
0117     return !(a == b);
0118 }
0119 
0120 QDebug operator<<(QDebug stream, const Position &position);
0121 
0122 } // namespace DOM
0123 
0124 #endif // __dom_position_h__