initial import
[vuplus_webkit] / Source / WebCore / editing / ReplaceSelectionCommand.h
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 ReplaceSelectionCommand_h
27 #define ReplaceSelectionCommand_h
28
29 #include "CompositeEditCommand.h"
30
31 namespace WebCore {
32
33 class CSSMutableStyleDeclaration;
34 class DocumentFragment;
35 class EditingStyle;
36 class Node;
37 class ReplacementFragment;
38
39 class ReplaceSelectionCommand : public CompositeEditCommand {
40 public:
41     enum CommandOption {
42         SelectReplacement = 1 << 0,
43         SmartReplace = 1 << 1,
44         MatchStyle = 1 << 2,
45         PreventNesting = 1 << 3,
46         MovingParagraph = 1 << 4
47     };
48
49     typedef unsigned CommandOptions;
50
51     static PassRefPtr<ReplaceSelectionCommand> create(Document* document, PassRefPtr<DocumentFragment> fragment, CommandOptions options, EditAction action = EditActionPaste)
52     {
53         return adoptRef(new ReplaceSelectionCommand(document, fragment, options, action));
54     }
55
56 private:
57     ReplaceSelectionCommand(Document*, PassRefPtr<DocumentFragment>, CommandOptions, EditAction);
58
59     virtual void doApply();
60     virtual EditAction editingAction() const;
61
62     void completeHTMLReplacement(const Position& lastPositionToSelect);
63
64     void insertNodeAfterAndUpdateNodesInserted(PassRefPtr<Node> insertChild, Node* refChild);
65     void insertNodeAtAndUpdateNodesInserted(PassRefPtr<Node>, const Position&);
66     void insertNodeBeforeAndUpdateNodesInserted(PassRefPtr<Node> insertChild, Node* refChild);
67     Node* insertAsListItems(PassRefPtr<Node>, Node* insertionNode, const Position&);
68
69     void updateNodesInserted(Node*);
70     bool shouldRemoveEndBR(Node*, const VisiblePosition&);
71     
72     bool shouldMergeStart(bool, bool, bool);
73     bool shouldMergeEnd(bool selectionEndWasEndOfParagraph);
74     bool shouldMerge(const VisiblePosition&, const VisiblePosition&);
75     
76     void mergeEndIfNeeded();
77     
78     void removeUnrenderedTextNodesAtEnds();
79     
80     void removeRedundantStylesAndKeepStyleSpanInline();
81     void handleStyleSpans();
82     void copyStyleToChildren(Node* parentNode, const CSSMutableStyleDeclaration* parentStyle);
83     void handlePasteAsQuotationNode();
84     
85     virtual void removeNodePreservingChildren(Node*);
86     virtual void removeNodeAndPruneAncestors(Node*);
87     
88     VisiblePosition positionAtStartOfInsertedContent();
89     VisiblePosition positionAtEndOfInsertedContent();
90     
91     bool performTrivialReplace(const ReplacementFragment&);
92
93     RefPtr<Node> m_firstNodeInserted;
94     RefPtr<Node> m_lastLeafInserted;
95     RefPtr<EditingStyle> m_insertionStyle;
96     bool m_selectReplacement;
97     bool m_smartReplace;
98     bool m_matchStyle;
99     RefPtr<DocumentFragment> m_documentFragment;
100     bool m_preventNesting;
101     bool m_movingParagraph;
102     EditAction m_editAction;
103     bool m_shouldMergeEnd;
104 };
105
106 } // namespace WebCore
107
108 #endif // ReplaceSelectionCommand_h