1 /* 2 * Copyright (C) 2005, 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 CompositeEditCommand_h 27 #define CompositeEditCommand_h 28 29 #include "CSSPropertyNames.h" 30 #include "core/editing/EditCommand.h" 31 #include "core/editing/UndoStep.h" 32 #include "wtf/Vector.h" 33 34 namespace WebCore { 35 36 class EditingStyle; 37 class Element; 38 class HTMLElement; 39 class Text; 40 41 class EditCommandComposition : public UndoStep { 42 public: 43 static PassRefPtr<EditCommandComposition> create(Document*, const VisibleSelection&, const VisibleSelection&, EditAction); 44 45 virtual void unapply() OVERRIDE; 46 virtual void reapply() OVERRIDE; 47 EditAction editingAction() const OVERRIDE { return m_editAction; } 48 void append(SimpleEditCommand*); 49 bool wasCreateLinkCommand() const { return m_editAction == EditActionCreateLink; } 50 51 const VisibleSelection& startingSelection() const { return m_startingSelection; } 52 const VisibleSelection& endingSelection() const { return m_endingSelection; } 53 void setStartingSelection(const VisibleSelection&); 54 void setEndingSelection(const VisibleSelection&); 55 Element* startingRootEditableElement() const { return m_startingRootEditableElement.get(); } 56 Element* endingRootEditableElement() const { return m_endingRootEditableElement.get(); } 57 58 #ifndef NDEBUG 59 virtual void getNodesInCommand(HashSet<Node*>&); 60 #endif 61 62 private: 63 EditCommandComposition(Document*, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection, EditAction); 64 65 RefPtr<Document> m_document; 66 VisibleSelection m_startingSelection; 67 VisibleSelection m_endingSelection; 68 Vector<RefPtr<SimpleEditCommand> > m_commands; 69 RefPtr<Element> m_startingRootEditableElement; 70 RefPtr<Element> m_endingRootEditableElement; 71 EditAction m_editAction; 72 }; 73 74 class CompositeEditCommand : public EditCommand { 75 public: 76 virtual ~CompositeEditCommand(); 77 78 void apply(); 79 bool isFirstCommand(EditCommand* command) { return !m_commands.isEmpty() && m_commands.first() == command; } 80 EditCommandComposition* composition() { return m_composition.get(); } 81 EditCommandComposition* ensureComposition(); 82 83 virtual bool isCreateLinkCommand() const; 84 virtual bool isTypingCommand() const; 85 virtual bool preservesTypingStyle() const; 86 virtual bool shouldRetainAutocorrectionIndicator() const; 87 virtual void setShouldRetainAutocorrectionIndicator(bool); 88 virtual bool shouldStopCaretBlinking() const { return false; } 89 90 protected: 91 explicit CompositeEditCommand(Document*); 92 93 // 94 // sugary-sweet convenience functions to help create and apply edit commands in composite commands 95 // 96 void appendNode(PassRefPtr<Node>, PassRefPtr<ContainerNode> parent); 97 void applyCommandToComposite(PassRefPtr<EditCommand>); 98 void applyCommandToComposite(PassRefPtr<CompositeEditCommand>, const VisibleSelection&); 99 void applyStyle(const EditingStyle*, EditAction = EditActionChangeAttributes); 100 void applyStyle(const EditingStyle*, const Position& start, const Position& end, EditAction = EditActionChangeAttributes); 101 void applyStyledElement(PassRefPtr<Element>); 102 void removeStyledElement(PassRefPtr<Element>); 103 void deleteSelection(bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true); 104 void deleteSelection(const VisibleSelection&, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true); 105 virtual void deleteTextFromNode(PassRefPtr<Text>, unsigned offset, unsigned count); 106 bool isRemovableBlock(const Node*); 107 void insertNodeAfter(PassRefPtr<Node>, PassRefPtr<Node> refChild); 108 void insertNodeAt(PassRefPtr<Node>, const Position&); 109 void insertNodeAtTabSpanPosition(PassRefPtr<Node>, const Position&); 110 void insertNodeBefore(PassRefPtr<Node>, PassRefPtr<Node> refChild, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable); 111 void insertParagraphSeparator(bool useDefaultParagraphElement = false, bool pasteBlockqutoeIntoUnquotedArea = false); 112 void insertLineBreak(); 113 void insertTextIntoNode(PassRefPtr<Text>, unsigned offset, const String& text); 114 void mergeIdenticalElements(PassRefPtr<Element>, PassRefPtr<Element>); 115 void rebalanceWhitespace(); 116 void rebalanceWhitespaceAt(const Position&); 117 void rebalanceWhitespaceOnTextSubstring(PassRefPtr<Text>, int startOffset, int endOffset); 118 void prepareWhitespaceAtPositionForSplit(Position&); 119 bool canRebalance(const Position&) const; 120 bool shouldRebalanceLeadingWhitespaceFor(const String&) const; 121 void removeCSSProperty(PassRefPtr<Element>, CSSPropertyID); 122 void removeNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute); 123 void removeChildrenInRange(PassRefPtr<Node>, unsigned from, unsigned to); 124 virtual void removeNode(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable); 125 HTMLElement* replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement>); 126 void removeNodePreservingChildren(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable); 127 void removeNodeAndPruneAncestors(PassRefPtr<Node>, Node* excludeNode = 0); 128 void moveRemainingSiblingsToNewParent(Node*, Node* pastLastNodeToMove, PassRefPtr<Element> prpNewParent); 129 void updatePositionForNodeRemovalPreservingChildren(Position&, Node*); 130 void prune(PassRefPtr<Node>, Node* excludeNode = 0); 131 void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText); 132 Position replaceSelectedTextInNode(const String&); 133 void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText); 134 Position positionOutsideTabSpan(const Position&); 135 void setNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute, const AtomicString& value); 136 void splitElement(PassRefPtr<Element>, PassRefPtr<Node> atChild); 137 void splitTextNode(PassRefPtr<Text>, unsigned offset); 138 void splitTextNodeContainingElement(PassRefPtr<Text>, unsigned offset); 139 void wrapContentsInDummySpan(PassRefPtr<Element>); 140 141 void deleteInsignificantText(PassRefPtr<Text>, unsigned start, unsigned end); 142 void deleteInsignificantText(const Position& start, const Position& end); 143 void deleteInsignificantTextDownstream(const Position&); 144 145 PassRefPtr<Node> appendBlockPlaceholder(PassRefPtr<Element>); 146 PassRefPtr<Node> insertBlockPlaceholder(const Position&); 147 PassRefPtr<Node> addBlockPlaceholderIfNeeded(Element*); 148 void removePlaceholderAt(const Position&); 149 150 PassRefPtr<Node> insertNewDefaultParagraphElementAt(const Position&); 151 152 PassRefPtr<Node> moveParagraphContentsToNewBlockIfNecessary(const Position&); 153 154 void pushAnchorElementDown(Node*); 155 156 // FIXME: preserveSelection and preserveStyle should be enums 157 void moveParagraph(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0); 158 void moveParagraphs(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0); 159 void moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode); 160 void cloneParagraphUnderNewElement(Position& start, Position& end, Node* outerNode, Element* blockElement); 161 void cleanupAfterDeletion(VisiblePosition destination = VisiblePosition()); 162 163 bool breakOutOfEmptyListItem(); 164 bool breakOutOfEmptyMailBlockquotedParagraph(); 165 166 Position positionAvoidingSpecialElementBoundary(const Position&); 167 168 PassRefPtr<Node> splitTreeToNode(Node*, Node*, bool splitAncestor = false); 169 170 Vector<RefPtr<EditCommand> > m_commands; 171 172 private: 173 bool isCompositeEditCommand() const OVERRIDE { return true; } 174 175 RefPtr<EditCommandComposition> m_composition; 176 }; 177 178 void applyCommand(PassRefPtr<CompositeEditCommand>); 179 180 inline CompositeEditCommand* toCompositeEditCommand(EditCommand* command) 181 { 182 ASSERT(command); 183 ASSERT(command->isCompositeEditCommand()); 184 return static_cast<CompositeEditCommand*>(command); 185 } 186 187 } // namespace WebCore 188 189 #endif // CompositeEditCommand_h 190