initial import
[vuplus_webkit] / Source / WebCore / editing / htmlediting.h
1 /*
2  * Copyright (C) 2004, 2006, 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 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 htmlediting_h
27 #define htmlediting_h
28
29 #include "EditingBoundary.h"
30 #include "Position.h"
31 #include "TextDirection.h"
32 #include <wtf/Forward.h>
33 #include <wtf/unicode/CharacterNames.h>
34
35 namespace WebCore {
36
37 class Document;
38 class Element;
39 class HTMLElement;
40 class HTMLTextFormControlElement;
41 class Node;
42 class Position;
43 class Range;
44 class VisiblePosition;
45 class VisibleSelection;
46
47
48 // This file contains a set of helper functions used by the editing commands
49
50 // -------------------------------------------------------------------------
51 // Node
52 // -------------------------------------------------------------------------
53
54 // Functions returning Node
55
56 Node* highestAncestor(Node*);
57 Node* highestEditableRoot(const Position&);
58 Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*),
59     EditingBoundaryCrossingRule = CannotCrossEditingBoundary, Node* stayWithin = 0);
60 Node* lowestEditableAncestor(Node*);   
61
62 Node* enclosingBlock(Node*, EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
63 Node* enclosingTableCell(const Position&);
64 Node* enclosingEmptyListItem(const VisiblePosition&);
65 Node* enclosingAnchorElement(const Position&);
66 Node* enclosingNodeWithTag(const Position&, const QualifiedName&);
67 Node* enclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
68
69 Node* tabSpanNode(const Node*);
70 Node* isLastPositionBeforeTable(const VisiblePosition&);
71 Node* isFirstPositionAfterTable(const VisiblePosition&);
72
73 // offset functions on Node
74
75 int lastOffsetForEditing(const Node*);
76 int caretMinOffset(const Node*);
77 int caretMaxOffset(const Node*);
78
79 // boolean functions on Node
80
81 // FIXME: editingIgnoresContent, canHaveChildrenForEditing, and isAtomicNode
82 // should be renamed to reflect its usage.
83
84 // Returns true for nodes that either have no content, or have content that is ignored (skipped over) while editing.
85 // There are no VisiblePositions inside these nodes.
86 inline bool editingIgnoresContent(const Node* node)
87 {
88     return !node->canContainRangeEndPoint();
89 }
90
91 inline bool canHaveChildrenForEditing(const Node* node)
92 {
93     return !node->isTextNode() && node->canContainRangeEndPoint();
94 }
95
96 bool isAtomicNode(const Node*);
97 bool isBlock(const Node*);
98 bool isSpecialElement(const Node*);
99 bool isTabSpanNode(const Node*);
100 bool isTabSpanTextNode(const Node*);
101 bool isMailBlockquote(const Node*);
102 bool isTableElement(Node*);
103 bool isTableCell(const Node*);
104 bool isEmptyTableCell(const Node*);
105 bool isTableStructureNode(const Node*);
106 bool isListElement(Node*);
107 bool isListItem(Node*);
108 bool isNodeRendered(const Node*);
109 bool isNodeVisiblyContainedWithin(Node*, const Range*);
110 bool isRenderedAsNonInlineTableImageOrHR(const Node*);
111     
112 TextDirection directionOfEnclosingBlock(const Position&);
113
114 // -------------------------------------------------------------------------
115 // Position
116 // -------------------------------------------------------------------------
117     
118 // Functions returning Position
119     
120 Position nextCandidate(const Position&);
121 Position previousCandidate(const Position&);
122     
123 Position nextVisuallyDistinctCandidate(const Position&);
124 Position previousVisuallyDistinctCandidate(const Position&);
125
126 Position positionOutsideTabSpan(const Position&);
127 Position positionBeforeContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
128 Position positionAfterContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
129 Position positionOutsideContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
130
131 inline Position firstPositionInOrBeforeNode(Node* node)
132 {
133     if (!node)
134         return Position();
135     return editingIgnoresContent(node) ? positionBeforeNode(node) : firstPositionInNode(node);
136 }
137
138 inline Position lastPositionInOrAfterNode(Node* node)
139 {
140     if (!node)
141         return Position();
142     return editingIgnoresContent(node) ? positionAfterNode(node) : lastPositionInNode(node);
143 }
144
145 // comparision functions on Position
146     
147 int comparePositions(const Position&, const Position&);
148
149 // boolean functions on Position
150     
151 bool isEditablePosition(const Position&);
152 bool isRichlyEditablePosition(const Position&);
153 bool isFirstVisiblePositionInSpecialElement(const Position&);
154 bool isLastVisiblePositionInSpecialElement(const Position&);
155 bool lineBreakExistsAtPosition(const Position&);
156 bool isVisiblyAdjacent(const Position& first, const Position& second);
157 bool isAtUnsplittableElement(const Position&);
158
159 // miscellaneous functions on Position
160
161 unsigned numEnclosingMailBlockquotes(const Position&);
162
163 // -------------------------------------------------------------------------
164 // VisiblePosition
165 // -------------------------------------------------------------------------
166     
167 // Functions returning VisiblePosition
168     
169 VisiblePosition firstEditablePositionAfterPositionInRoot(const Position&, Node*);
170 VisiblePosition lastEditablePositionBeforePositionInRoot(const Position&, Node*);
171 VisiblePosition visiblePositionBeforeNode(Node*);
172 VisiblePosition visiblePositionAfterNode(Node*);
173
174 bool lineBreakExistsAtVisiblePosition(const VisiblePosition&);
175     
176 int comparePositions(const VisiblePosition&, const VisiblePosition&);
177
178 int indexForVisiblePosition(const VisiblePosition&, Element **scope);
179 VisiblePosition visiblePositionForIndex(int index, Element *scope);
180
181 // -------------------------------------------------------------------------
182 // Range
183 // -------------------------------------------------------------------------
184
185 // Functions returning Range
186
187 PassRefPtr<Range> createRange(PassRefPtr<Document>, const VisiblePosition& start, const VisiblePosition& end, ExceptionCode&);
188 PassRefPtr<Range> extendRangeToWrappingNodes(PassRefPtr<Range> rangeToExtend, const Range* maximumRange, const Node* rootNode);
189 PassRefPtr<Range> avoidIntersectionWithNode(const Range*, Node*);
190
191 // -------------------------------------------------------------------------
192 // HTMLElement
193 // -------------------------------------------------------------------------
194     
195 // Functions returning HTMLElement
196     
197 PassRefPtr<HTMLElement> createDefaultParagraphElement(Document*);
198 PassRefPtr<HTMLElement> createBreakElement(Document*);
199 PassRefPtr<HTMLElement> createOrderedListElement(Document*);
200 PassRefPtr<HTMLElement> createUnorderedListElement(Document*);
201 PassRefPtr<HTMLElement> createListItemElement(Document*);
202 PassRefPtr<HTMLElement> createHTMLElement(Document*, const QualifiedName&);
203 PassRefPtr<HTMLElement> createHTMLElement(Document*, const AtomicString&);
204
205 HTMLElement* enclosingList(Node*);
206 HTMLElement* outermostEnclosingList(Node*, Node* rootList = 0);
207 Node* enclosingListChild(Node*);
208
209 // -------------------------------------------------------------------------
210 // Element
211 // -------------------------------------------------------------------------
212     
213 // Functions returning Element
214     
215 PassRefPtr<Element> createTabSpanElement(Document*);
216 PassRefPtr<Element> createTabSpanElement(Document*, PassRefPtr<Node> tabTextNode);
217 PassRefPtr<Element> createTabSpanElement(Document*, const String& tabText);
218 PassRefPtr<Element> createBlockPlaceholderElement(Document*);
219
220 Element* editableRootForPosition(const Position&);
221 Element* unsplittableElementForPosition(const Position&);
222
223 // Boolean functions on Element
224     
225 bool canMergeLists(Element* firstList, Element* secondList);
226     
227 // -------------------------------------------------------------------------
228 // VisibleSelection
229 // -------------------------------------------------------------------------
230
231 // Functions returning VisibleSelection
232 VisibleSelection avoidIntersectionWithNode(const VisibleSelection&, Node*);
233 VisibleSelection selectionForParagraphIteration(const VisibleSelection&);
234     
235
236 // Miscellaneous functions on Text
237 inline bool isWhitespace(UChar c)
238 {
239     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
240 }
241
242 inline bool isAmbiguousBoundaryCharacter(UChar character)
243 {
244     // These are characters that can behave as word boundaries, but can appear within words.
245     // If they are just typed, i.e. if they are immediately followed by a caret, we want to delay text checking until the next character has been typed.
246     // FIXME: this is required until 6853027 is fixed and text checking can do this for us.
247     return character == '\'' || character == rightSingleQuotationMark || character == hebrewPunctuationGershayim;
248 }
249
250 String stringWithRebalancedWhitespace(const String&, bool startIsStartOfParagraph, bool endIsEndOfParagraph);
251 const String& nonBreakingSpaceString();
252
253 }
254
255 #endif