2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / dom / Position.h
blob2624238169740c72248dbe361f3c83496e98fa54
1 /*
2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
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.
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.
26 #ifndef Position_h
27 #define Position_h
29 #include "TextAffinity.h"
30 #include "TextDirection.h"
31 #include <wtf/PassRefPtr.h>
32 #include <wtf/RefPtr.h>
34 namespace WebCore {
36 class CSSComputedStyleDeclaration;
37 class Element;
38 class InlineBox;
39 class Node;
40 class Range;
41 class RenderObject;
43 enum EUsingComposedCharacters { NotUsingComposedCharacters = false, UsingComposedCharacters = true };
45 // FIXME: Reduce the number of operations we have on a Position.
46 // This should be more like a humble struct, without so many different
47 // member functions. We should find better homes for these functions.
49 class Position {
50 public:
51 RefPtr<Node> container;
52 int posOffset; // to be renamed to offset when we get rid of offset()
54 Position() : posOffset(0) { }
55 Position(PassRefPtr<Node> c, int o) : container(c), posOffset(o) { }
57 void clear() { container.clear(); posOffset = 0; }
59 Node* node() const { return container.get(); }
60 int offset() const { return posOffset; }
61 Element* documentElement() const;
63 bool isNull() const { return !container; }
64 bool isNotNull() const { return container; }
66 Element* element() const;
67 PassRefPtr<CSSComputedStyleDeclaration> computedStyle() const;
69 // Move up or down the DOM by one position.
70 // Offsets are computed using render text for nodes that have renderers - but note that even when
71 // using composed characters, the result may be inside a single user-visible character if a ligature is formed.
72 Position previous(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
73 Position next(EUsingComposedCharacters usingComposedCharacters=NotUsingComposedCharacters) const;
74 static int uncheckedPreviousOffset(const Node*, int current);
75 static int uncheckedNextOffset(const Node*, int current);
77 bool atStart() const;
78 bool atEnd() const;
80 // FIXME: Make these non-member functions and put them somewhere in the editing directory.
81 // These aren't really basic "position" operations. More high level editing helper functions.
82 Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
83 Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
85 // These return useful visually equivalent positions.
86 Position upstream() const;
87 Position downstream() const;
89 bool isCandidate() const;
90 bool inRenderedText() const;
91 bool isRenderedCharacter() const;
92 bool rendersInDifferentPosition(const Position&) const;
94 void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
95 void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;
97 static bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
98 static bool nodeIsUserSelectNone(Node*);
100 void debugPosition(const char* msg = "") const;
102 #ifndef NDEBUG
103 void formatForDebugger(char* buffer, unsigned length) const;
104 void showTreeForThis() const;
105 #endif
107 private:
108 int renderedOffset() const;
110 Position previousCharacterPosition(EAffinity) const;
111 Position nextCharacterPosition(EAffinity) const;
114 inline bool operator==(const Position& a, const Position& b)
116 return a.container == b.container && a.posOffset == b.posOffset;
119 inline bool operator!=(const Position& a, const Position& b)
121 return !(a == b);
124 Position startPosition(const Range*);
125 Position endPosition(const Range*);
127 } // namespace WebCore
129 #ifndef NDEBUG
130 // Outside the WebCore namespace for ease of invocation from gdb.
131 void showTree(const WebCore::Position&);
132 void showTree(const WebCore::Position*);
133 #endif
135 #endif // Position_h