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 "ExceptionCode.h"
31 #include "HTMLNames.h"
32 #include "Position.h"
33 #include "TextDirection.h"
34 #include <wtf/Forward.h>
35 #include <wtf/unicode/CharacterNames.h>
36 
37 namespace WebCore {
38 
39 class Document;
40 class Element;
41 class HTMLElement;
42 class Node;
43 class Position;
44 class Range;
45 class VisiblePosition;
46 class VisibleSelection;
47 
48 
49 // This file contains a set of helper functions used by the editing commands
50 
51 // -------------------------------------------------------------------------
52 // Node
53 // -------------------------------------------------------------------------
54 
55 // Functions returning Node
56 
57 Node* highestAncestor(Node*);
58 Node* highestEditableRoot(const Position&);
59 Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
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 bool editingIgnoresContent(const Node*);
82 bool canHaveChildrenForEditing(const Node*);
83 bool isAtomicNode(const Node*);
84 bool isBlock(const Node*);
85 bool isSpecialElement(const Node*);
86 bool isTabSpanNode(const Node*);
87 bool isTabSpanTextNode(const Node*);
88 bool isMailBlockquote(const Node*);
89 bool isTableElement(Node*);
90 bool isTableCell(const Node*);
91 bool isEmptyTableCell(const Node*);
92 bool isTableStructureNode(const Node*);
93 bool isListElement(Node*);
94 bool isListItem(Node*);
95 bool isNodeRendered(const Node*);
96 bool isNodeVisiblyContainedWithin(Node*, const Range*);
97 bool isRenderedAsNonInlineTableImageOrHR(const Node*);
98 bool isNodeInTextFormControl(Node* node);
99 
100 TextDirection directionOfEnclosingBlock(const Position&);
101 
102 // -------------------------------------------------------------------------
103 // Position
104 // -------------------------------------------------------------------------
105 
106 // Functions returning Position
107 
108 Position nextCandidate(const Position&);
109 Position previousCandidate(const Position&);
110 
111 Position nextVisuallyDistinctCandidate(const Position&);
112 Position previousVisuallyDistinctCandidate(const Position&);
113 
114 Position positionOutsideTabSpan(const Position&);
115 Position positionBeforeContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
116 Position positionAfterContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
117 Position positionOutsideContainingSpecialElement(const Position&, Node** containingSpecialElement=0);
118 
firstPositionInOrBeforeNode(Node * node)119 inline Position firstPositionInOrBeforeNode(Node* node)
120 {
121     if (!node)
122         return Position();
123     return editingIgnoresContent(node) ? positionBeforeNode(node) : firstPositionInNode(node);
124 }
125 
lastPositionInOrAfterNode(Node * node)126 inline Position lastPositionInOrAfterNode(Node* node)
127 {
128     if (!node)
129         return Position();
130     return editingIgnoresContent(node) ? positionAfterNode(node) : lastPositionInNode(node);
131 }
132 
133 // comparision functions on Position
134 
135 int comparePositions(const Position&, const Position&);
136 
137 // boolean functions on Position
138 
139 bool isEditablePosition(const Position&);
140 bool isRichlyEditablePosition(const Position&);
141 bool isFirstVisiblePositionInSpecialElement(const Position&);
142 bool isLastVisiblePositionInSpecialElement(const Position&);
143 bool lineBreakExistsAtPosition(const Position&);
144 bool isVisiblyAdjacent(const Position& first, const Position& second);
145 bool isAtUnsplittableElement(const Position&);
146 
147 // miscellaneous functions on Position
148 
149 unsigned numEnclosingMailBlockquotes(const Position&);
150 
151 // -------------------------------------------------------------------------
152 // VisiblePosition
153 // -------------------------------------------------------------------------
154 
155 // Functions returning VisiblePosition
156 
157 VisiblePosition firstEditablePositionAfterPositionInRoot(const Position&, Node*);
158 VisiblePosition lastEditablePositionBeforePositionInRoot(const Position&, Node*);
159 VisiblePosition visiblePositionBeforeNode(Node*);
160 VisiblePosition visiblePositionAfterNode(Node*);
161 
162 bool lineBreakExistsAtVisiblePosition(const VisiblePosition&);
163 
164 int comparePositions(const VisiblePosition&, const VisiblePosition&);
165 int indexForVisiblePosition(const VisiblePosition&);
166 
167 // -------------------------------------------------------------------------
168 // Range
169 // -------------------------------------------------------------------------
170 
171 // Functions returning Range
172 
173 PassRefPtr<Range> createRange(PassRefPtr<Document>, const VisiblePosition& start, const VisiblePosition& end, ExceptionCode&);
174 PassRefPtr<Range> extendRangeToWrappingNodes(PassRefPtr<Range> rangeToExtend, const Range* maximumRange, const Node* rootNode);
175 PassRefPtr<Range> avoidIntersectionWithNode(const Range*, Node*);
176 
177 // -------------------------------------------------------------------------
178 // HTMLElement
179 // -------------------------------------------------------------------------
180 
181 // Functions returning HTMLElement
182 
183 PassRefPtr<HTMLElement> createDefaultParagraphElement(Document*);
184 PassRefPtr<HTMLElement> createBreakElement(Document*);
185 PassRefPtr<HTMLElement> createOrderedListElement(Document*);
186 PassRefPtr<HTMLElement> createUnorderedListElement(Document*);
187 PassRefPtr<HTMLElement> createListItemElement(Document*);
188 PassRefPtr<HTMLElement> createHTMLElement(Document*, const QualifiedName&);
189 PassRefPtr<HTMLElement> createHTMLElement(Document*, const AtomicString&);
190 
191 HTMLElement* enclosingList(Node*);
192 HTMLElement* outermostEnclosingList(Node*, Node* rootList = 0);
193 Node* enclosingListChild(Node*);
194 
195 // -------------------------------------------------------------------------
196 // Element
197 // -------------------------------------------------------------------------
198 
199 // Functions returning Element
200 
201 PassRefPtr<Element> createTabSpanElement(Document*);
202 PassRefPtr<Element> createTabSpanElement(Document*, PassRefPtr<Node> tabTextNode);
203 PassRefPtr<Element> createTabSpanElement(Document*, const String& tabText);
204 PassRefPtr<Element> createBlockPlaceholderElement(Document*);
205 
206 Element* editableRootForPosition(const Position&);
207 Element* unsplittableElementForPosition(const Position&);
208 
209 // Boolean functions on Element
210 
211 bool canMergeLists(Element* firstList, Element* secondList);
212 
213 // -------------------------------------------------------------------------
214 // VisibleSelection
215 // -------------------------------------------------------------------------
216 
217 // Functions returning VisibleSelection
218 VisibleSelection avoidIntersectionWithNode(const VisibleSelection&, Node*);
219 VisibleSelection selectionForParagraphIteration(const VisibleSelection&);
220 
221 
222 // Miscellaneous functions on Text
isWhitespace(UChar c)223 inline bool isWhitespace(UChar c)
224 {
225     return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
226 }
227 
isAmbiguousBoundaryCharacter(UChar character)228 inline bool isAmbiguousBoundaryCharacter(UChar character)
229 {
230     // These are characters that can behave as word boundaries, but can appear within words.
231     // 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.
232     // FIXME: this is required until 6853027 is fixed and text checking can do this for us.
233     return character == '\'' || character == rightSingleQuotationMark || character == hebrewPunctuationGershayim;
234 }
235 
236 String stringWithRebalancedWhitespace(const String&, bool startIsStartOfParagraph, bool endIsEndOfParagraph);
237 const String& nonBreakingSpaceString();
238 
239 }
240 
241 #endif
242