Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/dom/NodeTraversal.h"
30 #include "core/editing/CompositeEditCommand.h"
31
32 namespace WebCore {
33
34 class DocumentFragment;
35 class ReplacementFragment;
36
37 class ReplaceSelectionCommand FINAL : public CompositeEditCommand {
38 public:
39     enum CommandOption {
40         SelectReplacement = 1 << 0,
41         SmartReplace = 1 << 1,
42         MatchStyle = 1 << 2,
43         PreventNesting = 1 << 3,
44         MovingParagraph = 1 << 4,
45         SanitizeFragment = 1 << 5
46     };
47
48     typedef unsigned CommandOptions;
49
50     static PassRefPtr<ReplaceSelectionCommand> create(Document& document, PassRefPtr<DocumentFragment> fragment, CommandOptions options, EditAction action = EditActionPaste)
51     {
52         return adoptRef(new ReplaceSelectionCommand(document, fragment, options, action));
53     }
54
55 private:
56     ReplaceSelectionCommand(Document&, PassRefPtr<DocumentFragment>, CommandOptions, EditAction);
57
58     virtual void doApply() OVERRIDE;
59     virtual EditAction editingAction() const OVERRIDE;
60
61     class InsertedNodes {
62     public:
63         void respondToNodeInsertion(Node&);
64         void willRemoveNodePreservingChildren(Node&);
65         void willRemoveNode(Node&);
66         void didReplaceNode(Node&, Node& newNode);
67
68         Node* firstNodeInserted() const { return m_firstNodeInserted.get(); }
69         Node* lastLeafInserted() const { return m_lastNodeInserted ? &m_lastNodeInserted->lastDescendant() : 0; }
70         Node* pastLastLeaf() const { return m_lastNodeInserted ? NodeTraversal::next(*lastLeafInserted()) : 0; }
71
72     private:
73         RefPtr<Node> m_firstNodeInserted;
74         RefPtr<Node> m_lastNodeInserted;
75     };
76
77     Node* insertAsListItems(PassRefPtr<HTMLElement> listElement, Node* insertionNode, const Position&, InsertedNodes&);
78
79     void updateNodesInserted(Node*);
80     bool shouldRemoveEndBR(Node*, const VisiblePosition&);
81
82     bool shouldMergeStart(bool, bool, bool);
83     bool shouldMergeEnd(bool selectionEndWasEndOfParagraph);
84     bool shouldMerge(const VisiblePosition&, const VisiblePosition&);
85
86     void mergeEndIfNeeded();
87
88     void removeUnrenderedTextNodesAtEnds(InsertedNodes&);
89
90     void removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes&);
91     void makeInsertedContentRoundTrippableWithHTMLTreeBuilder(InsertedNodes&);
92     void moveNodeOutOfAncestor(PassRefPtr<Node>, PassRefPtr<Node> ancestor);
93     void handleStyleSpans(InsertedNodes&);
94
95     VisiblePosition positionAtStartOfInsertedContent() const;
96     VisiblePosition positionAtEndOfInsertedContent() const;
97
98     bool shouldPerformSmartReplace() const;
99     void addSpacesForSmartReplace();
100     void completeHTMLReplacement(const Position& lastPositionToSelect);
101     void mergeTextNodesAroundPosition(Position&, Position& positionOnlyToBeUpdated);
102
103     bool performTrivialReplace(const ReplacementFragment&);
104
105     Position m_startOfInsertedContent;
106     Position m_endOfInsertedContent;
107     RefPtr<EditingStyle> m_insertionStyle;
108     bool m_selectReplacement;
109     bool m_smartReplace;
110     bool m_matchStyle;
111     RefPtr<DocumentFragment> m_documentFragment;
112     bool m_preventNesting;
113     bool m_movingParagraph;
114     EditAction m_editAction;
115     bool m_sanitizeFragment;
116     bool m_shouldMergeEnd;
117 };
118
119 } // namespace WebCore
120
121 #endif // ReplaceSelectionCommand_h