1 | /* |
2 | * Copyright (C) 2004, 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 | #pragma once |
27 | |
28 | #include "EditingBoundary.h" |
29 | #include "Position.h" |
30 | |
31 | namespace WTF { |
32 | class TextStream; |
33 | } |
34 | |
35 | namespace WebCore { |
36 | |
37 | // VisiblePosition default affinity is downstream because |
38 | // the callers do not really care (they just want the |
39 | // deep position without regard to line position), and this |
40 | // is cheaper than UPSTREAM |
41 | #define VP_DEFAULT_AFFINITY DOWNSTREAM |
42 | |
43 | // Callers who do not know where on the line the position is, |
44 | // but would like UPSTREAM if at a line break or DOWNSTREAM |
45 | // otherwise, need a clear way to specify that. The |
46 | // constructors auto-correct UPSTREAM to DOWNSTREAM if the |
47 | // position is not at a line break. |
48 | #define VP_UPSTREAM_IF_POSSIBLE UPSTREAM |
49 | |
50 | class InlineBox; |
51 | class Node; |
52 | |
53 | class VisiblePosition { |
54 | public: |
55 | // NOTE: UPSTREAM affinity will be used only if pos is at end of a wrapped line, |
56 | // otherwise it will be converted to DOWNSTREAM |
57 | VisiblePosition() : m_affinity(VP_DEFAULT_AFFINITY) { } |
58 | WEBCORE_EXPORT VisiblePosition(const Position&, EAffinity = VP_DEFAULT_AFFINITY); |
59 | |
60 | void clear() { m_deepPosition.clear(); } |
61 | |
62 | bool isNull() const { return m_deepPosition.isNull(); } |
63 | bool isNotNull() const { return m_deepPosition.isNotNull(); } |
64 | bool isOrphan() const { return m_deepPosition.isOrphan(); } |
65 | |
66 | Position deepEquivalent() const { return m_deepPosition; } |
67 | EAffinity affinity() const { ASSERT(m_affinity == UPSTREAM || m_affinity == DOWNSTREAM); return m_affinity; } |
68 | void setAffinity(EAffinity affinity) { m_affinity = affinity; } |
69 | |
70 | // FIXME: Change the following functions' parameter from a boolean to StayInEditableContent. |
71 | |
72 | // next() and previous() will increment/decrement by a character cluster. |
73 | WEBCORE_EXPORT VisiblePosition next(EditingBoundaryCrossingRule = CanCrossEditingBoundary, bool* reachedBoundary = nullptr) const; |
74 | WEBCORE_EXPORT VisiblePosition previous(EditingBoundaryCrossingRule = CanCrossEditingBoundary, bool* reachedBoundary = nullptr) const; |
75 | VisiblePosition honorEditingBoundaryAtOrBefore(const VisiblePosition&, bool* reachedBoundary = nullptr) const; |
76 | VisiblePosition honorEditingBoundaryAtOrAfter(const VisiblePosition&, bool* reachedBoundary = nullptr) const; |
77 | |
78 | WEBCORE_EXPORT VisiblePosition left(bool stayInEditableContent = false, bool* reachedBoundary = nullptr) const; |
79 | WEBCORE_EXPORT VisiblePosition right(bool stayInEditableContent = false, bool* reachedBoundary = nullptr) const; |
80 | |
81 | WEBCORE_EXPORT UChar32 characterAfter() const; |
82 | UChar32 characterBefore() const { return previous().characterAfter(); } |
83 | |
84 | // FIXME: This does not handle [table, 0] correctly. |
85 | Element* rootEditableElement() const { return m_deepPosition.isNotNull() ? m_deepPosition.deprecatedNode()->rootEditableElement() : 0; } |
86 | |
87 | void getInlineBoxAndOffset(InlineBox*& inlineBox, int& caretOffset) const |
88 | { |
89 | m_deepPosition.getInlineBoxAndOffset(m_affinity, inlineBox, caretOffset); |
90 | } |
91 | |
92 | void getInlineBoxAndOffset(TextDirection primaryDirection, InlineBox*& inlineBox, int& caretOffset) const |
93 | { |
94 | m_deepPosition.getInlineBoxAndOffset(m_affinity, primaryDirection, inlineBox, caretOffset); |
95 | } |
96 | |
97 | // Rect is local to the returned renderer |
98 | WEBCORE_EXPORT LayoutRect localCaretRect(RenderObject*&) const; |
99 | // Bounds of (possibly transformed) caret in absolute coords |
100 | WEBCORE_EXPORT IntRect absoluteCaretBounds(bool* insideFixed = nullptr) const; |
101 | // Abs x/y position of the caret ignoring transforms. |
102 | // FIXME: navigation with transforms should be smarter. |
103 | WEBCORE_EXPORT int lineDirectionPointForBlockDirectionNavigation() const; |
104 | |
105 | // This is a tentative enhancement of operator== to account for affinity. |
106 | // FIXME: Combine this function with operator== |
107 | bool equals(const VisiblePosition&) const; |
108 | |
109 | #if ENABLE(TREE_DEBUGGING) |
110 | void debugPosition(const char* msg = "" ) const; |
111 | void formatForDebugger(char* buffer, unsigned length) const; |
112 | void showTreeForThis() const; |
113 | #endif |
114 | |
115 | private: |
116 | void init(const Position&, EAffinity); |
117 | Position canonicalPosition(const Position&); |
118 | |
119 | Position leftVisuallyDistinctCandidate() const; |
120 | Position rightVisuallyDistinctCandidate() const; |
121 | |
122 | Position m_deepPosition; |
123 | EAffinity m_affinity; |
124 | }; |
125 | |
126 | // FIXME: This shouldn't ignore affinity. |
127 | inline bool operator==(const VisiblePosition& a, const VisiblePosition& b) |
128 | { |
129 | return a.deepEquivalent() == b.deepEquivalent(); |
130 | } |
131 | |
132 | inline bool operator!=(const VisiblePosition& a, const VisiblePosition& b) |
133 | { |
134 | return !(a == b); |
135 | } |
136 | |
137 | inline bool operator<(const VisiblePosition& a, const VisiblePosition& b) |
138 | { |
139 | return a.deepEquivalent() < b.deepEquivalent(); |
140 | } |
141 | |
142 | inline bool operator>(const VisiblePosition& a, const VisiblePosition& b) |
143 | { |
144 | return a.deepEquivalent() > b.deepEquivalent(); |
145 | } |
146 | |
147 | inline bool operator<=(const VisiblePosition& a, const VisiblePosition& b) |
148 | { |
149 | return a.deepEquivalent() <= b.deepEquivalent(); |
150 | } |
151 | |
152 | inline bool operator>=(const VisiblePosition& a, const VisiblePosition& b) |
153 | { |
154 | return a.deepEquivalent() >= b.deepEquivalent(); |
155 | } |
156 | |
157 | WEBCORE_EXPORT RefPtr<Range> makeRange(const VisiblePosition&, const VisiblePosition&); |
158 | bool setStart(Range*, const VisiblePosition&); |
159 | bool setEnd(Range*, const VisiblePosition&); |
160 | VisiblePosition startVisiblePosition(const Range*, EAffinity); |
161 | VisiblePosition endVisiblePosition(const Range*, EAffinity); |
162 | |
163 | WEBCORE_EXPORT Element* enclosingBlockFlowElement(const VisiblePosition&); |
164 | |
165 | bool isFirstVisiblePositionInNode(const VisiblePosition&, const Node*); |
166 | bool isLastVisiblePositionInNode(const VisiblePosition&, const Node*); |
167 | |
168 | WTF::TextStream& operator<<(WTF::TextStream&, EAffinity); |
169 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const VisiblePosition&); |
170 | |
171 | } // namespace WebCore |
172 | |
173 | #if ENABLE(TREE_DEBUGGING) |
174 | // Outside the WebCore namespace for ease of invocation from the debugger. |
175 | void showTree(const WebCore::VisiblePosition*); |
176 | void showTree(const WebCore::VisiblePosition&); |
177 | #endif |
178 | |