Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/dom/Position.h"
30 #include "core/editing/EditingBoundary.h"
31 #include "platform/text/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 ExceptionState;
40 class HTMLElement;
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* highestEditableRoot(const Position&, EditableType = ContentIsEditable);
57
58 Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*),
59     EditingBoundaryCrossingRule = CannotCrossEditingBoundary, Node* stayWithin = 0);
60 Node* highestNodeToRemoveInPruning(Node*, Node* excludeNode = 0);
61 Node* lowestEditableAncestor(Node*);
62
63 Element* enclosingBlock(Node*, EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
64 Node* enclosingTableCell(const Position&);
65 Node* enclosingEmptyListItem(const VisiblePosition&);
66 Element* enclosingAnchorElement(const Position&);
67 Node* enclosingNodeWithTag(const Position&, const QualifiedName&);
68 Node* enclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
69
70 Node* tabSpanNode(const Node*);
71 Node* isLastPositionBeforeTable(const VisiblePosition&);
72 Node* isFirstPositionAfterTable(const VisiblePosition&);
73
74 // offset functions on Node
75
76 int lastOffsetForEditing(const Node*);
77 int caretMinOffset(const Node*);
78 int caretMaxOffset(const Node*);
79
80 // boolean functions on Node
81
82 // FIXME: editingIgnoresContent, canHaveChildrenForEditing, and isAtomicNode
83 // should be renamed to reflect its usage.
84
85 // Returns true for nodes that either have no content, or have content that is ignored (skipped over) while editing.
86 // There are no VisiblePositions inside these nodes.
87 inline bool editingIgnoresContent(const Node* node)
88 {
89     return !node->canContainRangeEndPoint();
90 }
91
92 inline bool canHaveChildrenForEditing(const Node* node)
93 {
94     return !node->isTextNode() && node->canContainRangeEndPoint();
95 }
96
97 bool isAtomicNode(const Node*);
98 bool isBlock(const Node*);
99 bool isInline(const Node*);
100 bool isSpecialElement(const Node*);
101 bool isTabSpanNode(const Node*);
102 bool isTabSpanTextNode(const Node*);
103 bool isMailBlockquote(const Node*);
104 bool isRenderedTable(const Node*);
105 bool isRenderedTableElement(const Node*);
106 bool isTableCell(const Node*);
107 bool isEmptyTableCell(const Node*);
108 bool isTableStructureNode(const Node*);
109 bool isListElement(Node*);
110 bool isListItem(const Node*);
111 bool isNodeRendered(const Node*);
112 bool isNodeVisiblyContainedWithin(Node*, const Range*);
113 bool isRenderedAsNonInlineTableImageOrHR(const Node*);
114 bool areIdenticalElements(const Node*, const Node*);
115 bool isNonTableCellHTMLBlockElement(const Node*);
116 TextDirection directionOfEnclosingBlock(const Position&);
117
118 // -------------------------------------------------------------------------
119 // Position
120 // -------------------------------------------------------------------------
121
122 // Functions returning Position
123
124 Position nextCandidate(const Position&);
125 Position previousCandidate(const Position&);
126
127 Position nextVisuallyDistinctCandidate(const Position&);
128 Position previousVisuallyDistinctCandidate(const Position&);
129
130 Position positionBeforeContainingSpecialElement(const Position&, Node** containingSpecialElement = 0);
131 Position positionAfterContainingSpecialElement(const Position&, Node** containingSpecialElement = 0);
132
133 inline Position firstPositionInOrBeforeNode(Node* node)
134 {
135     if (!node)
136         return Position();
137     return editingIgnoresContent(node) ? positionBeforeNode(node) : firstPositionInNode(node);
138 }
139
140 inline Position lastPositionInOrAfterNode(Node* node)
141 {
142     if (!node)
143         return Position();
144     return editingIgnoresContent(node) ? positionAfterNode(node) : lastPositionInNode(node);
145 }
146
147 // comparision functions on Position
148
149 int comparePositions(const Position&, const Position&);
150 int comparePositions(const PositionWithAffinity&, const PositionWithAffinity&);
151
152 // boolean functions on Position
153
154 enum EUpdateStyle { UpdateStyle, DoNotUpdateStyle };
155 // FIXME: Both isEditablePosition and isRichlyEditablePosition rely on up-to-date
156 // style to give proper results. They shouldn't update style by default, but
157 // should make it clear that that is the contract.
158 // FIXME: isRichlyEditablePosition should also take EUpdateStyle.
159 bool isEditablePosition(const Position&, EditableType = ContentIsEditable, EUpdateStyle = UpdateStyle);
160 bool isRichlyEditablePosition(const Position&, EditableType = ContentIsEditable);
161 bool lineBreakExistsAtPosition(const Position&);
162 bool isVisiblyAdjacent(const Position& first, const Position& second);
163 bool isAtUnsplittableElement(const Position&);
164
165 // miscellaneous functions on Position
166
167 unsigned numEnclosingMailBlockquotes(const Position&);
168 void updatePositionForNodeRemoval(Position&, Node*);
169
170 // -------------------------------------------------------------------------
171 // VisiblePosition
172 // -------------------------------------------------------------------------
173
174 // Functions returning VisiblePosition
175
176 VisiblePosition firstEditablePositionAfterPositionInRoot(const Position&, Node*);
177 VisiblePosition lastEditablePositionBeforePositionInRoot(const Position&, Node*);
178 VisiblePosition visiblePositionBeforeNode(Node*);
179 VisiblePosition visiblePositionAfterNode(Node*);
180
181 bool lineBreakExistsAtVisiblePosition(const VisiblePosition&);
182
183 int comparePositions(const VisiblePosition&, const VisiblePosition&);
184
185 int indexForVisiblePosition(const VisiblePosition&, RefPtr<ContainerNode>& scope);
186 VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
187
188 // -------------------------------------------------------------------------
189 // Range
190 // -------------------------------------------------------------------------
191
192 // Functions returning Range
193
194 PassRefPtr<Range> createRange(Document&, const VisiblePosition& start, const VisiblePosition& end, ExceptionState&);
195
196 // -------------------------------------------------------------------------
197 // HTMLElement
198 // -------------------------------------------------------------------------
199
200 // Functions returning HTMLElement
201
202 PassRefPtr<HTMLElement> createDefaultParagraphElement(Document&);
203 PassRefPtr<HTMLElement> createBreakElement(Document&);
204 PassRefPtr<HTMLElement> createOrderedListElement(Document&);
205 PassRefPtr<HTMLElement> createUnorderedListElement(Document&);
206 PassRefPtr<HTMLElement> createListItemElement(Document&);
207 PassRefPtr<HTMLElement> createHTMLElement(Document&, const QualifiedName&);
208 PassRefPtr<HTMLElement> createHTMLElement(Document&, const AtomicString&);
209
210 HTMLElement* enclosingList(Node*);
211 HTMLElement* outermostEnclosingList(Node*, Node* rootList = 0);
212 Node* enclosingListChild(Node*);
213
214 // -------------------------------------------------------------------------
215 // Element
216 // -------------------------------------------------------------------------
217
218 // Functions returning Element
219
220 PassRefPtr<Element> createTabSpanElement(Document&);
221 PassRefPtr<Element> createTabSpanElement(Document&, PassRefPtr<Node> tabTextNode);
222 PassRefPtr<Element> createTabSpanElement(Document&, const String& tabText);
223 PassRefPtr<Element> createBlockPlaceholderElement(Document&);
224
225 Element* editableRootForPosition(const Position&, EditableType = ContentIsEditable);
226 Element* unsplittableElementForPosition(const Position&);
227
228 // Boolean functions on Element
229
230 bool canMergeLists(Element* firstList, Element* secondList);
231
232 // -------------------------------------------------------------------------
233 // VisibleSelection
234 // -------------------------------------------------------------------------
235
236 // Functions returning VisibleSelection
237 VisibleSelection selectionForParagraphIteration(const VisibleSelection&);
238
239 Position adjustedSelectionStartForStyleComputation(const VisibleSelection&);
240
241
242 // Miscellaneous functions on Text
243 inline bool isWhitespace(UChar c)
244 {
245     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
246 }
247
248 inline bool isAmbiguousBoundaryCharacter(UChar character)
249 {
250     // These are characters that can behave as word boundaries, but can appear within words.
251     // 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.
252     // FIXME: this is required until 6853027 is fixed and text checking can do this for us.
253     return character == '\'' || character == rightSingleQuotationMark || character == hebrewPunctuationGershayim;
254 }
255
256 String stringWithRebalancedWhitespace(const String&, bool startIsStartOfParagraph, bool endIsEndOfParagraph);
257 const String& nonBreakingSpaceString();
258
259 }
260
261 #endif