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 "EditCommand.h"
30#include "CSSPropertyNames.h"
31#include "UndoStep.h"
32#include <wtf/Vector.h>
33
34namespace WebCore {
35
36class EditingStyle;
37class HTMLElement;
38class StyledElement;
39class Text;
40
41class EditCommandComposition : public UndoStep {
42public:
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
62private:
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
74class CompositeEditCommand : public EditCommand {
75public:
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 isDictationCommand() const { return false; }
86    virtual bool preservesTypingStyle() const;
87    virtual bool shouldRetainAutocorrectionIndicator() const;
88    virtual void setShouldRetainAutocorrectionIndicator(bool);
89    virtual bool shouldStopCaretBlinking() const { return false; }
90
91protected:
92    explicit CompositeEditCommand(Document*);
93
94    //
95    // sugary-sweet convenience functions to help create and apply edit commands in composite commands
96    //
97    void appendNode(PassRefPtr<Node>, PassRefPtr<ContainerNode> parent);
98    void applyCommandToComposite(PassRefPtr<EditCommand>);
99    void applyCommandToComposite(PassRefPtr<CompositeEditCommand>, const VisibleSelection&);
100    void applyStyle(const EditingStyle*, EditAction = EditActionChangeAttributes);
101    void applyStyle(const EditingStyle*, const Position& start, const Position& end, EditAction = EditActionChangeAttributes);
102    void applyStyledElement(PassRefPtr<Element>);
103    void removeStyledElement(PassRefPtr<Element>);
104    void deleteSelection(bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
105    void deleteSelection(const VisibleSelection&, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
106    virtual void deleteTextFromNode(PassRefPtr<Text>, unsigned offset, unsigned count);
107    bool isRemovableBlock(const Node*);
108    void insertNodeAfter(PassRefPtr<Node>, PassRefPtr<Node> refChild);
109    void insertNodeAt(PassRefPtr<Node>, const Position&);
110    void insertNodeAtTabSpanPosition(PassRefPtr<Node>, const Position&);
111    void insertNodeBefore(PassRefPtr<Node>, PassRefPtr<Node> refChild, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
112    void insertParagraphSeparator(bool useDefaultParagraphElement = false, bool pasteBlockqutoeIntoUnquotedArea = false);
113    void insertLineBreak();
114    void insertTextIntoNode(PassRefPtr<Text>, unsigned offset, const String& text);
115    void mergeIdenticalElements(PassRefPtr<Element>, PassRefPtr<Element>);
116    void rebalanceWhitespace();
117    void rebalanceWhitespaceAt(const Position&);
118    void rebalanceWhitespaceOnTextSubstring(PassRefPtr<Text>, int startOffset, int endOffset);
119    void prepareWhitespaceAtPositionForSplit(Position&);
120    bool canRebalance(const Position&) const;
121    bool shouldRebalanceLeadingWhitespaceFor(const String&) const;
122    void removeCSSProperty(PassRefPtr<StyledElement>, CSSPropertyID);
123    void removeNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute);
124    void removeChildrenInRange(PassRefPtr<Node>, unsigned from, unsigned to);
125    virtual void removeNode(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
126    HTMLElement* replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement>);
127    void removeNodePreservingChildren(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
128    void removeNodeAndPruneAncestors(PassRefPtr<Node>);
129    void moveRemainingSiblingsToNewParent(Node*, Node* pastLastNodeToMove, PassRefPtr<Element> prpNewParent);
130    void updatePositionForNodeRemovalPreservingChildren(Position&, Node*);
131    void prune(PassRefPtr<Node>);
132    void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
133    Position replaceSelectedTextInNode(const String&);
134    void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
135    Position positionOutsideTabSpan(const Position&);
136    void setNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute, const AtomicString& value);
137    void splitElement(PassRefPtr<Element>, PassRefPtr<Node> atChild);
138    void splitTextNode(PassRefPtr<Text>, unsigned offset);
139    void splitTextNodeContainingElement(PassRefPtr<Text>, unsigned offset);
140    void wrapContentsInDummySpan(PassRefPtr<Element>);
141
142    void deleteInsignificantText(PassRefPtr<Text>, unsigned start, unsigned end);
143    void deleteInsignificantText(const Position& start, const Position& end);
144    void deleteInsignificantTextDownstream(const Position&);
145
146    PassRefPtr<Node> appendBlockPlaceholder(PassRefPtr<Element>);
147    PassRefPtr<Node> insertBlockPlaceholder(const Position&);
148    PassRefPtr<Node> addBlockPlaceholderIfNeeded(Element*);
149    void removePlaceholderAt(const Position&);
150
151    PassRefPtr<Node> insertNewDefaultParagraphElementAt(const Position&);
152
153    PassRefPtr<Node> moveParagraphContentsToNewBlockIfNecessary(const Position&);
154
155    void pushAnchorElementDown(Node*);
156
157    void moveParagraph(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true);
158    void moveParagraphs(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true);
159    void moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode);
160    void cloneParagraphUnderNewElement(const Position& start, const 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
172private:
173    bool isCompositeEditCommand() const OVERRIDE { return true; }
174
175    RefPtr<EditCommandComposition> m_composition;
176};
177
178void applyCommand(PassRefPtr<CompositeEditCommand>);
179
180inline 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