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 <wtf/Forward.h> 30 #include "HTMLNames.h" 31 #include "ExceptionCode.h" 32 #include "Position.h" 33 34 namespace WebCore { 35 36 class Document; 37 class Element; 38 class HTMLElement; 39 class Node; 40 class Position; 41 class Range; 42 class String; 43 class VisiblePosition; 44 class VisibleSelection; 45 46 47 // This file contains a set of helper functions used by the editing commands 48 49 // ------------------------------------------------------------------------- 50 // Node 51 // ------------------------------------------------------------------------- 52 53 // Functions returning Node 54 55 Node* highestAncestor(Node*); 56 Node* highestEditableRoot(const Position&); 57 Node* highestEnclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*)); 58 Node* lowestEditableAncestor(Node*); 59 60 Node* enclosingBlock(Node*); 61 Node* enclosingTableCell(const Position&); 62 Node* enclosingEmptyListItem(const VisiblePosition&); 63 Node* enclosingAnchorElement(const Position&); 64 Node* enclosingNodeWithTag(const Position&, const QualifiedName&); 65 Node* enclosingNodeOfType(const Position&, bool (*nodeIsOfType)(const Node*), bool onlyReturnEditableNodes = true); 66 67 Node* tabSpanNode(const Node*); 68 Node* nearestMailBlockquote(const Node*); 69 Node* isLastPositionBeforeTable(const VisiblePosition&); 70 Node* isFirstPositionAfterTable(const VisiblePosition&); 71 72 // offset functions on Node 73 74 int lastOffsetForEditing(const Node*); 75 int caretMinOffset(const Node*); 76 int caretMaxOffset(const Node*); 77 78 // boolean functions on Node 79 80 bool editingIgnoresContent(const Node*); 81 bool canHaveChildrenForEditing(const Node*); 82 bool isAtomicNode(const Node*); 83 bool isBlock(const Node*); 84 bool isSpecialElement(const Node*); 85 bool isTabSpanNode(const Node*); 86 bool isTabSpanTextNode(const Node*); 87 bool isMailBlockquote(const Node*); 88 bool isTableElement(Node*); 89 bool isTableCell(const Node*); 90 bool isEmptyTableCell(const Node*); 91 bool isTableStructureNode(const Node*); 92 bool isListElement(Node*); 93 bool isListItem(Node*); 94 bool isNodeRendered(const Node*); 95 bool isNodeVisiblyContainedWithin(Node*, const Range*); 96 bool isRenderedAsNonInlineTableImageOrHR(const Node*); 97 bool isNodeInTextFormControl(Node* node); 98 99 // ------------------------------------------------------------------------- 100 // Position 101 // ------------------------------------------------------------------------- 102 103 // Functions returning Position 104 105 Position rangeCompliantEquivalent(const Position&); 106 Position rangeCompliantEquivalent(const VisiblePosition&); 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 positionBeforeTabSpan(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 119 // Position creation functions are inline to prevent ref-churn. 120 // Other Position creation functions are in Position.h 121 // but these depend on lastOffsetForEditing which is defined in htmlediting.h. 122 123 // NOTE: first/lastDeepEditingPositionForNode return legacy editing positions (like [img, 0]) 124 // for elements which editing ignores. The rest of the editing code will treat [img, 0] 125 // as "the last position before the img". 126 // New code should use the creation functions in Position.h instead. 127 inline Position firstDeepEditingPositionForNode(Node* anchorNode) 128 { 129 ASSERT(anchorNode); 130 return Position(anchorNode, 0); 131 } 132 133 inline Position lastDeepEditingPositionForNode(Node* anchorNode) 134 { 135 ASSERT(anchorNode); 136 return Position(anchorNode, lastOffsetForEditing(anchorNode)); 137 } 138 139 // comparision functions on Position 140 141 int comparePositions(const Position&, const Position&); 142 143 // boolean functions on Position 144 145 bool isEditablePosition(const Position&); 146 bool isRichlyEditablePosition(const Position&); 147 bool isFirstVisiblePositionInSpecialElement(const Position&); 148 bool isLastVisiblePositionInSpecialElement(const Position&); 149 bool lineBreakExistsAtPosition(const Position&); 150 bool isVisiblyAdjacent(const Position& first, const Position& second); 151 bool isAtUnsplittableElement(const Position&); 152 153 // miscellaneous functions on Position 154 155 unsigned numEnclosingMailBlockquotes(const Position&); 156 157 // ------------------------------------------------------------------------- 158 // VisiblePosition 159 // ------------------------------------------------------------------------- 160 161 // Functions returning VisiblePosition 162 163 VisiblePosition firstEditablePositionAfterPositionInRoot(const Position&, Node*); 164 VisiblePosition lastEditablePositionBeforePositionInRoot(const Position&, Node*); 165 VisiblePosition visiblePositionBeforeNode(Node*); 166 VisiblePosition visiblePositionAfterNode(Node*); 167 168 bool lineBreakExistsAtVisiblePosition(const VisiblePosition&); 169 170 int comparePositions(const VisiblePosition&, const VisiblePosition&); 171 int indexForVisiblePosition(const VisiblePosition&); 172 173 // ------------------------------------------------------------------------- 174 // Range 175 // ------------------------------------------------------------------------- 176 177 // Functions returning Range 178 179 PassRefPtr<Range> createRange(PassRefPtr<Document>, const VisiblePosition& start, const VisiblePosition& end, ExceptionCode&); 180 PassRefPtr<Range> extendRangeToWrappingNodes(PassRefPtr<Range> rangeToExtend, const Range* maximumRange, const Node* rootNode); 181 PassRefPtr<Range> avoidIntersectionWithNode(const Range*, Node*); 182 183 // ------------------------------------------------------------------------- 184 // HTMLElement 185 // ------------------------------------------------------------------------- 186 187 // Functions returning HTMLElement 188 189 PassRefPtr<HTMLElement> createDefaultParagraphElement(Document*); 190 PassRefPtr<HTMLElement> createBreakElement(Document*); 191 PassRefPtr<HTMLElement> createOrderedListElement(Document*); 192 PassRefPtr<HTMLElement> createUnorderedListElement(Document*); 193 PassRefPtr<HTMLElement> createListItemElement(Document*); 194 PassRefPtr<HTMLElement> createHTMLElement(Document*, const QualifiedName&); 195 PassRefPtr<HTMLElement> createHTMLElement(Document*, const AtomicString&); 196 197 HTMLElement* enclosingList(Node*); 198 HTMLElement* outermostEnclosingList(Node*); 199 HTMLElement* enclosingListChild(Node*); 200 201 // ------------------------------------------------------------------------- 202 // Element 203 // ------------------------------------------------------------------------- 204 205 // Functions returning Element 206 207 PassRefPtr<Element> createTabSpanElement(Document*); 208 PassRefPtr<Element> createTabSpanElement(Document*, PassRefPtr<Node> tabTextNode); 209 PassRefPtr<Element> createTabSpanElement(Document*, const String& tabText); 210 PassRefPtr<Element> createBlockPlaceholderElement(Document*); 211 212 Element* editableRootForPosition(const Position&); 213 Element* unsplittableElementForPosition(const Position&); 214 215 // Boolean functions on Element 216 217 bool canMergeLists(Element* firstList, Element* secondList); 218 219 // ------------------------------------------------------------------------- 220 // VisibleSelection 221 // ------------------------------------------------------------------------- 222 223 // Functions returning VisibleSelection 224 225 VisibleSelection avoidIntersectionWithNode(const VisibleSelection&, Node*); 226 VisibleSelection selectionForParagraphIteration(const VisibleSelection&); 227 228 229 // Miscellaneous functions on String 230 231 String stringWithRebalancedWhitespace(const String&, bool, bool); 232 const String& nonBreakingSpaceString(); 233 bool validBlockTag(const AtomicString&); 234 235 236 } 237 238 #endif 239