1 /*
2  * Copyright (C) 2004 Apple Computer, 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 COMPUTER, 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 COMPUTER, 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 #ifndef __dom_position_h__
27 #define __dom_position_h__
28 
29 #include <QDebug>
30 
31 namespace DOM
32 {
33 
34 class ElementImpl;
35 class NodeImpl;
36 
37 NodeImpl *rootNavigableElement(NodeImpl *node);
38 bool inSameRootNavigableElement(NodeImpl *n1, NodeImpl *n2);
39 
40 void printEnclosingBlockTree(NodeImpl *node);
41 void printRootEditableTree(NodeImpl *node);
42 
43 class Position
44 {
45 public:
Position()46     Position() : m_node(nullptr), m_offset(0) {}
47     Position(NodeImpl *node, long offset);
48     Position(const Position &);
49     ~Position();
50 
node()51     NodeImpl *node() const
52     {
53         return m_node;
54     }
offset()55     long offset() const
56     {
57         return m_offset;
58     }
59 
60     ElementImpl *element() const;
61 
62     long renderedOffset() const;
63 
isEmpty()64     bool isEmpty() const
65     {
66         return m_node == nullptr;
67     }
notEmpty()68     bool notEmpty() const
69     {
70         return m_node != nullptr;
71     }
72 
73     Position equivalentLeafPosition() const;
74     Position previousRenderedEditablePosition() const;
75     Position nextRenderedEditablePosition() const;
76     Position previousCharacterPosition() const;
77     Position nextCharacterPosition() const;
78     Position previousWordPosition() const;
79     Position nextWordPosition() const;
80     Position previousLinePosition(int x) const;
81     Position nextLinePosition(int x) const;
82     Position equivalentUpstreamPosition() const;
83     Position equivalentDownstreamPosition() const;
84     Position equivalentRangeCompliantPosition() const;
85     Position equivalentShallowPosition() const;
86     bool atStartOfContainingEditableBlock() const;
87     bool atStartOfRootEditableElement() const;
88     bool inRenderedContent() const;
89     bool inRenderedText() const;
90     bool rendersOnSameLine(const Position &pos) const;
91     bool rendersInDifferentPosition(const Position &pos) const;
92     bool isFirstRenderedPositionOnLine() const;
93     bool isLastRenderedPositionOnLine() const;
94     bool isLastRenderedPositionInEditableBlock() const;
95     bool inFirstEditableInRootEditableElement() const;
96     bool inLastEditableInRootEditableElement() const;
97     bool inFirstEditableInContainingEditableBlock() const;
98     bool inLastEditableInContainingEditableBlock() const;
99 
100     Position &operator=(const Position &o);
101 
102     friend bool operator==(const Position &a, const Position &b);
103     friend bool operator!=(const Position &a, const Position &b);
104 
105 private:
106     NodeImpl *m_node;
107     long m_offset;
108 };
109 
110 inline bool operator==(const Position &a, const Position &b)
111 {
112     return a.node() == b.node() && a.offset() == b.offset();
113 }
114 
115 inline bool operator!=(const Position &a, const Position &b)
116 {
117     return !(a == b);
118 }
119 
120 QDebug operator<<(QDebug stream, const Position &position);
121 
122 } // namespace DOM
123 
124 #endif // __dom_position_h__
125