1/*
2 * Copyright (C) 2004, 2006, 2008, 2016 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 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 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#pragma once
27
28#include "Position.h"
29#include <wtf/Forward.h>
30#include <wtf/unicode/CharacterNames.h>
31
32namespace WebCore {
33
34class Document;
35class HTMLElement;
36class HTMLSpanElement;
37class HTMLTextFormControlElement;
38class RenderBlock;
39class VisiblePosition;
40class VisibleSelection;
41
42// -------------------------------------------------------------------------
43// Node
44// -------------------------------------------------------------------------
45
46ContainerNode* highestEditableRoot(const Position&, EditableType = ContentIsEditable);
47
48Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary, Node* stayWithin = nullptr);
49Node* highestNodeToRemoveInPruning(Node*);
50Element* lowestEditableAncestor(Node*);
51
52Element* deprecatedEnclosingBlockFlowElement(Node*); // Use enclosingBlock instead.
53Element* enclosingBlock(Node*, EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
54Element* enclosingTableCell(const Position&);
55Node* enclosingEmptyListItem(const VisiblePosition&);
56Element* enclosingAnchorElement(const Position&);
57Element* enclosingElementWithTag(const Position&, const QualifiedName&);
58Node* enclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), EditingBoundaryCrossingRule = CannotCrossEditingBoundary);
59HTMLSpanElement* tabSpanNode(const Node*);
60Element* isLastPositionBeforeTable(const VisiblePosition&); // FIXME: Strange to name this isXXX, but return an element.
61Element* isFirstPositionAfterTable(const VisiblePosition&); // FIXME: Strange to name this isXXX, but return an element.
62
63// These two deliver leaf nodes as if the whole DOM tree were a linear chain of its leaf nodes.
64Node* nextLeafNode(const Node*);
65Node* previousLeafNode(const Node*);
66
67WEBCORE_EXPORT int lastOffsetForEditing(const Node&);
68int caretMinOffset(const Node&);
69int caretMaxOffset(const Node&);
70
71bool hasEditableStyle(const Node&, EditableType);
72bool isEditableNode(const Node&);
73
74// FIXME: editingIgnoresContent, canHaveChildrenForEditing, and isAtomicNode should be named to clarify how they differ.
75
76// Returns true for nodes that either have no content, or have content that is ignored (skipped over) while editing.
77// There are no VisiblePositions inside these nodes.
78bool editingIgnoresContent(const Node&);
79
80bool canHaveChildrenForEditing(const Node&);
81bool isAtomicNode(const Node*);
82
83bool isBlock(const Node*);
84bool isBlockFlowElement(const Node&);
85bool isInline(const Node*);
86bool isTabSpanNode(const Node*);
87bool isTabSpanTextNode(const Node*);
88bool isMailBlockquote(const Node*);
89bool isRenderedTable(const Node*);
90bool isTableCell(const Node*);
91bool isEmptyTableCell(const Node*);
92bool isTableStructureNode(const Node*);
93bool isListHTMLElement(Node*);
94bool isListItem(const Node*);
95bool isNodeRendered(const Node&);
96bool isRenderedAsNonInlineTableImageOrHR(const Node*);
97bool isNonTableCellHTMLBlockElement(const Node*);
98
99bool isNodeVisiblyContainedWithin(Node&, const Range&);
100
101bool areIdenticalElements(const Node&, const Node&);
102
103bool positionBeforeOrAfterNodeIsCandidate(Node&);
104
105// -------------------------------------------------------------------------
106// Position
107// -------------------------------------------------------------------------
108
109Position nextCandidate(const Position&);
110Position previousCandidate(const Position&);
111
112Position nextVisuallyDistinctCandidate(const Position&);
113Position previousVisuallyDistinctCandidate(const Position&);
114
115Position positionBeforeContainingSpecialElement(const Position&, HTMLElement** containingSpecialElement = nullptr);
116Position positionAfterContainingSpecialElement(const Position&, HTMLElement** containingSpecialElement = nullptr);
117
118Position firstPositionInOrBeforeNode(Node*);
119Position lastPositionInOrAfterNode(Node*);
120
121Position firstEditablePositionAfterPositionInRoot(const Position&, ContainerNode* root);
122Position lastEditablePositionBeforePositionInRoot(const Position&, ContainerNode* root);
123
124WEBCORE_EXPORT int comparePositions(const Position&, const Position&);
125
126WEBCORE_EXPORT bool isEditablePosition(const Position&, EditableType = ContentIsEditable);
127bool isRichlyEditablePosition(const Position&);
128bool lineBreakExistsAtPosition(const Position&);
129bool isAtUnsplittableElement(const Position&);
130
131unsigned numEnclosingMailBlockquotes(const Position&);
132void updatePositionForNodeRemoval(Position&, Node&);
133
134WEBCORE_EXPORT TextDirection directionOfEnclosingBlock(const Position&);
135
136// -------------------------------------------------------------------------
137// VisiblePosition
138// -------------------------------------------------------------------------
139
140VisiblePosition visiblePositionBeforeNode(Node&);
141VisiblePosition visiblePositionAfterNode(Node&);
142
143bool lineBreakExistsAtVisiblePosition(const VisiblePosition&);
144
145WEBCORE_EXPORT int comparePositions(const VisiblePosition&, const VisiblePosition&);
146
147WEBCORE_EXPORT int indexForVisiblePosition(const VisiblePosition&, RefPtr<ContainerNode>& scope);
148int indexForVisiblePosition(Node&, const VisiblePosition&, bool forSelectionPreservation);
149WEBCORE_EXPORT VisiblePosition visiblePositionForPositionWithOffset(const VisiblePosition&, int offset);
150WEBCORE_EXPORT VisiblePosition visiblePositionForIndex(int index, ContainerNode* scope);
151VisiblePosition visiblePositionForIndexUsingCharacterIterator(Node&, int index); // FIXME: Why do we need this version?
152
153// -------------------------------------------------------------------------
154// HTMLElement
155// -------------------------------------------------------------------------
156
157WEBCORE_EXPORT Ref<HTMLElement> createDefaultParagraphElement(Document&);
158Ref<HTMLElement> createHTMLElement(Document&, const QualifiedName&);
159Ref<HTMLElement> createHTMLElement(Document&, const AtomString&);
160
161WEBCORE_EXPORT HTMLElement* enclosingList(Node*);
162HTMLElement* outermostEnclosingList(Node*, Node* rootList = nullptr);
163Node* enclosingListChild(Node*);
164
165// -------------------------------------------------------------------------
166// Element
167// -------------------------------------------------------------------------
168
169Ref<Element> createTabSpanElement(Document&);
170Ref<Element> createTabSpanElement(Document&, const String& tabText);
171Ref<Element> createBlockPlaceholderElement(Document&);
172
173Element* editableRootForPosition(const Position&, EditableType = ContentIsEditable);
174Element* unsplittableElementForPosition(const Position&);
175
176bool canMergeLists(Element* firstList, Element* secondList);
177
178// -------------------------------------------------------------------------
179// VisibleSelection
180// -------------------------------------------------------------------------
181
182VisibleSelection selectionForParagraphIteration(const VisibleSelection&);
183Position adjustedSelectionStartForStyleComputation(const VisibleSelection&);
184
185// -------------------------------------------------------------------------
186
187// FIXME: This is only one of many definitions of whitespace. Possibly never the right one to use.
188bool deprecatedIsEditingWhitespace(UChar);
189
190// FIXME: Can't answer this question correctly without being passed the white-space mode.
191bool deprecatedIsCollapsibleWhitespace(UChar);
192
193bool isAmbiguousBoundaryCharacter(UChar);
194
195String stringWithRebalancedWhitespace(const String&, bool startIsStartOfParagraph, bool endIsEndOfParagraph);
196const String& nonBreakingSpaceString();
197
198// Miscellaneous functions for caret rendering.
199
200RenderBlock* rendererForCaretPainting(Node*);
201LayoutRect localCaretRectInRendererForCaretPainting(const VisiblePosition&, RenderBlock*&);
202LayoutRect localCaretRectInRendererForRect(LayoutRect&, Node*, RenderObject*, RenderBlock*&);
203IntRect absoluteBoundsForLocalCaretRect(RenderBlock* rendererForCaretPainting, const LayoutRect&, bool* insideFixed = nullptr);
204
205// -------------------------------------------------------------------------
206
207inline bool deprecatedIsEditingWhitespace(UChar c)
208{
209 return c == noBreakSpace || c == ' ' || c == '\n' || c == '\t';
210}
211
212// FIXME: Can't really answer this question correctly without knowing the white-space mode.
213inline bool deprecatedIsCollapsibleWhitespace(UChar c)
214{
215 return c == ' ' || c == '\n';
216}
217
218bool isAmbiguousBoundaryCharacter(UChar);
219
220inline bool editingIgnoresContent(const Node& node)
221{
222 return !node.canContainRangeEndPoint();
223}
224
225inline bool positionBeforeOrAfterNodeIsCandidate(Node& node)
226{
227 return isRenderedTable(&node) || editingIgnoresContent(node);
228}
229
230inline Position firstPositionInOrBeforeNode(Node* node)
231{
232 if (!node)
233 return { };
234 return editingIgnoresContent(*node) ? positionBeforeNode(node) : firstPositionInNode(node);
235}
236
237inline Position lastPositionInOrAfterNode(Node* node)
238{
239 if (!node)
240 return { };
241 return editingIgnoresContent(*node) ? positionAfterNode(node) : lastPositionInNode(node);
242}
243
244}
245