Update To 11.40.268.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 blink {
36
37 class Document;
38 class Element;
39 class ExceptionState;
40 class HTMLBRElement;
41 class HTMLElement;
42 class HTMLLIElement;
43 class HTMLOListElement;
44 class HTMLSpanElement;
45 class HTMLUListElement;
46 class Node;
47 class Position;
48 class PositionWithAffinity;
49 class Range;
50 class VisiblePosition;
51 class VisibleSelection;
52
53
54 // This file contains a set of helper functions used by the editing commands
55
56 // -------------------------------------------------------------------------
57 // Node
58 // -------------------------------------------------------------------------
59
60 // Functions returning Node
61
62 ContainerNode* highestEditableRoot(const Position&, EditableType = ContentIsEditable);
63
64 Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*),
65     EditingBoundaryCrossingRule = CannotCrossEditingBoundary, Node* stayWithin = nullptr);
66 Node* highestNodeToRemoveInPruning(Node*, Node* excludeNode = nullptr);
67 Element* lowestEditableAncestor(Node*);
68
69 Element* enclosingBlock(Node*, EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
70 Element* enclosingBlockFlowElement(Node&); // Deprecated, use enclosingBlock instead.
71 bool inSameContainingBlockFlowElement(Node*, Node*);
72 Element* enclosingTableCell(const Position&);
73 Node* enclosingEmptyListItem(const VisiblePosition&);
74 Element* enclosingAnchorElement(const Position&);
75 Element* enclosingElementWithTag(const Position&, const QualifiedName&);
76 Node* enclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
77
78 HTMLSpanElement* tabSpanElement(const Node*);
79 Element* isLastPositionBeforeTable(const VisiblePosition&);
80 Element* isFirstPositionAfterTable(const VisiblePosition&);
81
82 // offset functions on Node
83
84 int lastOffsetForEditing(const Node*);
85 int caretMinOffset(const Node*);
86 int caretMaxOffset(const Node*);
87
88 // boolean functions on Node
89
90 // FIXME: editingIgnoresContent, canHaveChildrenForEditing, and isAtomicNode
91 // should be renamed to reflect its usage.
92
93 // Returns true for nodes that either have no content, or have content that is ignored (skipped over) while editing.
94 // There are no VisiblePositions inside these nodes.
95 inline bool editingIgnoresContent(const Node* node)
96 {
97     return !node->canContainRangeEndPoint();
98 }
99
100 inline bool canHaveChildrenForEditing(const Node* node)
101 {
102     return !node->isTextNode() && node->canContainRangeEndPoint();
103 }
104
105 bool isAtomicNode(const Node*);
106 bool isBlock(const Node*);
107 bool isInline(const Node*);
108 bool isTabHTMLSpanElement(const Node*);
109 bool isTabHTMLSpanElementTextNode(const Node*);
110 bool isMailHTMLBlockquoteElement(const Node*);
111 bool isRenderedTableElement(const Node*);
112 bool isRenderedHTMLTableElement(const Node*);
113 bool isTableCell(const Node*);
114 bool isEmptyTableCell(const Node*);
115 bool isTableStructureNode(const Node*);
116 bool isHTMLListElement(Node*);
117 bool isListItem(const Node*);
118 bool isNodeRendered(const Node*);
119 bool isNodeVisiblyContainedWithin(Node&, const Range&);
120 bool isRenderedAsNonInlineTableImageOrHR(const Node*);
121 bool areIdenticalElements(const Node*, const Node*);
122 bool isNonTableCellHTMLBlockElement(const Node*);
123 bool isBlockFlowElement(const Node&);
124 TextDirection directionOfEnclosingBlock(const Position&);
125
126 // -------------------------------------------------------------------------
127 // Position
128 // -------------------------------------------------------------------------
129
130 // Functions returning Position
131
132 Position nextCandidate(const Position&);
133 Position previousCandidate(const Position&);
134
135 Position nextVisuallyDistinctCandidate(const Position&);
136 Position previousVisuallyDistinctCandidate(const Position&);
137
138 Position positionBeforeContainingSpecialElement(const Position&, HTMLElement** containingSpecialElement = nullptr);
139 Position positionAfterContainingSpecialElement(const Position&, HTMLElement** containingSpecialElement = nullptr);
140
141 inline Position firstPositionInOrBeforeNode(Node* node)
142 {
143     if (!node)
144         return Position();
145     return editingIgnoresContent(node) ? positionBeforeNode(node) : firstPositionInNode(node);
146 }
147
148 inline Position lastPositionInOrAfterNode(Node* node)
149 {
150     if (!node)
151         return Position();
152     return editingIgnoresContent(node) ? positionAfterNode(node) : lastPositionInNode(node);
153 }
154
155 Position lastEditablePositionBeforePositionInRoot(const Position&, Node*);
156
157 // comparision functions on Position
158
159 int comparePositions(const Position&, const Position&);
160 int comparePositions(const PositionWithAffinity&, const PositionWithAffinity&);
161
162 // boolean functions on Position
163
164 enum EUpdateStyle { UpdateStyle, DoNotUpdateStyle };
165 // FIXME: Both isEditablePosition and isRichlyEditablePosition rely on up-to-date
166 // style to give proper results. They shouldn't update style by default, but
167 // should make it clear that that is the contract.
168 // FIXME: isRichlyEditablePosition should also take EUpdateStyle.
169 bool isEditablePosition(const Position&, EditableType = ContentIsEditable, EUpdateStyle = UpdateStyle);
170 bool isRichlyEditablePosition(const Position&, EditableType = ContentIsEditable);
171 bool lineBreakExistsAtPosition(const Position&);
172 bool isVisiblyAdjacent(const Position& first, const Position& second);
173 bool isAtUnsplittableElement(const Position&);
174
175 // miscellaneous functions on Position
176
177 enum WhitespacePositionOption { NotConsiderNonCollapsibleWhitespace, ConsiderNonCollapsibleWhitespace };
178 Position leadingWhitespacePosition(const Position&, EAffinity, WhitespacePositionOption = NotConsiderNonCollapsibleWhitespace);
179 Position trailingWhitespacePosition(const Position&, EAffinity, WhitespacePositionOption = NotConsiderNonCollapsibleWhitespace);
180 unsigned numEnclosingMailBlockquotes(const Position&);
181 void updatePositionForNodeRemoval(Position&, Node&);
182
183 // -------------------------------------------------------------------------
184 // VisiblePosition
185 // -------------------------------------------------------------------------
186
187 // Functions returning VisiblePosition
188
189 VisiblePosition firstEditableVisiblePositionAfterPositionInRoot(const Position&, ContainerNode*);
190 VisiblePosition lastEditableVisiblePositionBeforePositionInRoot(const Position&, ContainerNode*);
191 VisiblePosition visiblePositionBeforeNode(Node&);
192 VisiblePosition visiblePositionAfterNode(Node&);
193
194 bool lineBreakExistsAtVisiblePosition(const VisiblePosition&);
195
196 int comparePositions(const VisiblePosition&, const VisiblePosition&);
197
198 int indexForVisiblePosition(const VisiblePosition&, RefPtrWillBeRawPtr<ContainerNode>& scope);
199 VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
200
201 // -------------------------------------------------------------------------
202 // Range
203 // -------------------------------------------------------------------------
204
205 // Functions returning Range
206
207 PassRefPtrWillBeRawPtr<Range> createRange(Document&, const VisiblePosition& start, const VisiblePosition& end, ExceptionState&);
208
209 // -------------------------------------------------------------------------
210 // HTMLElement
211 // -------------------------------------------------------------------------
212
213 // Functions returning HTMLElement
214
215 PassRefPtrWillBeRawPtr<HTMLElement> createDefaultParagraphElement(Document&);
216 PassRefPtrWillBeRawPtr<HTMLBRElement> createBreakElement(Document&);
217 PassRefPtrWillBeRawPtr<HTMLOListElement> createOrderedListElement(Document&);
218 PassRefPtrWillBeRawPtr<HTMLUListElement> createUnorderedListElement(Document&);
219 PassRefPtrWillBeRawPtr<HTMLLIElement> createListItemElement(Document&);
220 PassRefPtrWillBeRawPtr<HTMLElement> createHTMLElement(Document&, const QualifiedName&);
221 PassRefPtrWillBeRawPtr<HTMLElement> createHTMLElement(Document&, const AtomicString&);
222
223 HTMLElement* enclosingList(Node*);
224 HTMLElement* outermostEnclosingList(Node*, HTMLElement* rootList = nullptr);
225 Node* enclosingListChild(Node*);
226
227 // -------------------------------------------------------------------------
228 // Element
229 // -------------------------------------------------------------------------
230
231 // Functions returning Element
232
233 PassRefPtrWillBeRawPtr<HTMLSpanElement> createTabSpanElement(Document&);
234 PassRefPtrWillBeRawPtr<HTMLSpanElement> createTabSpanElement(Document&, PassRefPtrWillBeRawPtr<Text> tabTextNode);
235 PassRefPtrWillBeRawPtr<HTMLSpanElement> createTabSpanElement(Document&, const String& tabText);
236 PassRefPtrWillBeRawPtr<HTMLBRElement> createBlockPlaceholderElement(Document&);
237
238 Element* editableRootForPosition(const Position&, EditableType = ContentIsEditable);
239 Element* unsplittableElementForPosition(const Position&);
240
241 // Boolean functions on Element
242
243 bool canMergeLists(Element* firstList, Element* secondList);
244
245 // -------------------------------------------------------------------------
246 // VisibleSelection
247 // -------------------------------------------------------------------------
248
249 // Functions returning VisibleSelection
250 VisibleSelection selectionForParagraphIteration(const VisibleSelection&);
251
252 Position adjustedSelectionStartForStyleComputation(const VisibleSelection&);
253
254
255 // Miscellaneous functions on Text
256 inline bool isWhitespace(UChar c)
257 {
258     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
259 }
260
261 // FIXME: Can't really answer this question correctly without knowing the white-space mode.
262 inline bool isCollapsibleWhitespace(UChar c)
263 {
264     return c == ' ' || c == '\n';
265 }
266
267 inline bool isAmbiguousBoundaryCharacter(UChar character)
268 {
269     // These are characters that can behave as word boundaries, but can appear within words.
270     // 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.
271     // FIXME: this is required until 6853027 is fixed and text checking can do this for us.
272     return character == '\'' || character == rightSingleQuotationMark || character == hebrewPunctuationGershayim;
273 }
274
275 String stringWithRebalancedWhitespace(const String&, bool startIsStartOfParagraph, bool endIsEndOfParagraph);
276 const String& nonBreakingSpaceString();
277
278 }
279
280 #endif