2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef CompositeEditCommand_h
27 #define CompositeEditCommand_h
29 #include "CSSPropertyNames.h"
30 #include "core/editing/EditCommand.h"
31 #include "core/editing/UndoStep.h"
32 #include "wtf/Vector.h"
41 class EditCommandComposition FINAL : public UndoStep {
43 static PassRefPtr<EditCommandComposition> create(Document*, const VisibleSelection&, const VisibleSelection&, EditAction);
45 virtual bool belongsTo(const Frame&) const OVERRIDE;
46 virtual void unapply() OVERRIDE;
47 virtual void reapply() OVERRIDE;
48 virtual EditAction editingAction() const OVERRIDE { return m_editAction; }
49 void append(SimpleEditCommand*);
50 bool wasCreateLinkCommand() const { return m_editAction == EditActionCreateLink; }
52 const VisibleSelection& startingSelection() const { return m_startingSelection; }
53 const VisibleSelection& endingSelection() const { return m_endingSelection; }
54 void setStartingSelection(const VisibleSelection&);
55 void setEndingSelection(const VisibleSelection&);
56 Element* startingRootEditableElement() const { return m_startingRootEditableElement.get(); }
57 Element* endingRootEditableElement() const { return m_endingRootEditableElement.get(); }
60 EditCommandComposition(Document*, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection, EditAction);
62 RefPtr<Document> m_document;
63 VisibleSelection m_startingSelection;
64 VisibleSelection m_endingSelection;
65 Vector<RefPtr<SimpleEditCommand> > m_commands;
66 RefPtr<Element> m_startingRootEditableElement;
67 RefPtr<Element> m_endingRootEditableElement;
68 EditAction m_editAction;
71 class CompositeEditCommand : public EditCommand {
73 virtual ~CompositeEditCommand();
76 bool isFirstCommand(EditCommand* command) { return !m_commands.isEmpty() && m_commands.first() == command; }
77 EditCommandComposition* composition() { return m_composition.get(); }
78 EditCommandComposition* ensureComposition();
80 virtual bool isTypingCommand() const;
81 virtual bool preservesTypingStyle() const;
82 virtual void setShouldRetainAutocorrectionIndicator(bool);
83 virtual bool shouldStopCaretBlinking() const { return false; }
86 explicit CompositeEditCommand(Document&);
89 // sugary-sweet convenience functions to help create and apply edit commands in composite commands
91 void appendNode(PassRefPtr<Node>, PassRefPtr<ContainerNode> parent);
92 void applyCommandToComposite(PassRefPtr<EditCommand>);
93 void applyCommandToComposite(PassRefPtr<CompositeEditCommand>, const VisibleSelection&);
94 void applyStyle(const EditingStyle*, EditAction = EditActionChangeAttributes);
95 void applyStyle(const EditingStyle*, const Position& start, const Position& end, EditAction = EditActionChangeAttributes);
96 void applyStyledElement(PassRefPtr<Element>);
97 void removeStyledElement(PassRefPtr<Element>);
98 void deleteSelection(bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
99 void deleteSelection(const VisibleSelection&, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
100 virtual void deleteTextFromNode(PassRefPtr<Text>, unsigned offset, unsigned count);
101 bool isRemovableBlock(const Node*);
102 void insertNodeAfter(PassRefPtr<Node>, PassRefPtr<Node> refChild);
103 void insertNodeAt(PassRefPtr<Node>, const Position&);
104 void insertNodeAtTabSpanPosition(PassRefPtr<Node>, const Position&);
105 void insertNodeBefore(PassRefPtr<Node>, PassRefPtr<Node> refChild, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
106 void insertParagraphSeparator(bool useDefaultParagraphElement = false, bool pasteBlockqutoeIntoUnquotedArea = false);
107 void insertTextIntoNode(PassRefPtr<Text>, unsigned offset, const String& text);
108 void mergeIdenticalElements(PassRefPtr<Element>, PassRefPtr<Element>);
109 void rebalanceWhitespace();
110 void rebalanceWhitespaceAt(const Position&);
111 void rebalanceWhitespaceOnTextSubstring(PassRefPtr<Text>, int startOffset, int endOffset);
112 void prepareWhitespaceAtPositionForSplit(Position&);
113 void replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(const VisiblePosition&);
114 bool canRebalance(const Position&) const;
115 bool shouldRebalanceLeadingWhitespaceFor(const String&) const;
116 void removeCSSProperty(PassRefPtr<Element>, CSSPropertyID);
117 void removeNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute);
118 void removeChildrenInRange(PassRefPtr<Node>, unsigned from, unsigned to);
119 virtual void removeNode(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
120 HTMLElement* replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement>);
121 void removeNodePreservingChildren(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
122 void removeNodeAndPruneAncestors(PassRefPtr<Node>, Node* excludeNode = 0);
123 void moveRemainingSiblingsToNewParent(Node*, Node* pastLastNodeToMove, PassRefPtr<Element> prpNewParent);
124 void updatePositionForNodeRemovalPreservingChildren(Position&, Node*);
125 void prune(PassRefPtr<Node>, Node* excludeNode = 0);
126 void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
127 Position replaceSelectedTextInNode(const String&);
128 void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
129 Position positionOutsideTabSpan(const Position&);
130 void setNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute, const AtomicString& value);
131 void splitElement(PassRefPtr<Element>, PassRefPtr<Node> atChild);
132 void splitTextNode(PassRefPtr<Text>, unsigned offset);
133 void splitTextNodeContainingElement(PassRefPtr<Text>, unsigned offset);
134 void wrapContentsInDummySpan(PassRefPtr<Element>);
136 void deleteInsignificantText(PassRefPtr<Text>, unsigned start, unsigned end);
137 void deleteInsignificantText(const Position& start, const Position& end);
138 void deleteInsignificantTextDownstream(const Position&);
140 PassRefPtr<Node> appendBlockPlaceholder(PassRefPtr<Element>);
141 PassRefPtr<Node> insertBlockPlaceholder(const Position&);
142 PassRefPtr<Node> addBlockPlaceholderIfNeeded(Element*);
143 void removePlaceholderAt(const Position&);
145 PassRefPtr<Node> insertNewDefaultParagraphElementAt(const Position&);
147 PassRefPtr<Node> moveParagraphContentsToNewBlockIfNecessary(const Position&);
149 void pushAnchorElementDown(Node*);
151 // FIXME: preserveSelection and preserveStyle should be enums
152 void moveParagraph(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0);
153 void moveParagraphs(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0);
154 void moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode);
155 void cloneParagraphUnderNewElement(const Position& start, const Position& end, Node* outerNode, Element* blockElement);
156 void cleanupAfterDeletion(VisiblePosition destination = VisiblePosition());
158 bool breakOutOfEmptyListItem();
159 bool breakOutOfEmptyMailBlockquotedParagraph();
161 Position positionAvoidingSpecialElementBoundary(const Position&);
163 PassRefPtr<Node> splitTreeToNode(Node*, Node*, bool splitAncestor = false);
165 Vector<RefPtr<EditCommand> > m_commands;
168 virtual bool isCompositeEditCommand() const OVERRIDE FINAL { return true; }
170 RefPtr<EditCommandComposition> m_composition;
173 DEFINE_TYPE_CASTS(CompositeEditCommand, EditCommand, command, command->isCompositeEditCommand(), command.isCompositeEditCommand());
175 } // namespace WebCore
177 #endif // CompositeEditCommand_h