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
32namespace blink {
33
34class DocumentFragment;
35class ReplacementFragment;
36
37class ReplaceSelectionCommand FINAL : public CompositeEditCommand {
38public:
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 PassRefPtrWillBeRawPtr<ReplaceSelectionCommand> create(Document& document, PassRefPtrWillBeRawPtr<DocumentFragment> fragment, CommandOptions options, EditAction action = EditActionPaste)
51    {
52        return adoptRefWillBeNoop(new ReplaceSelectionCommand(document, fragment, options, action));
53    }
54
55    virtual void trace(Visitor*) OVERRIDE;
56
57private:
58    ReplaceSelectionCommand(Document&, PassRefPtrWillBeRawPtr<DocumentFragment>, CommandOptions, EditAction);
59
60    virtual void doApply() OVERRIDE;
61    virtual EditAction editingAction() const OVERRIDE;
62
63    class InsertedNodes {
64        STACK_ALLOCATED();
65    public:
66        void respondToNodeInsertion(Node&);
67        void willRemoveNodePreservingChildren(Node&);
68        void willRemoveNode(Node&);
69        void didReplaceNode(Node&, Node& newNode);
70
71        Node* firstNodeInserted() const { return m_firstNodeInserted.get(); }
72        Node* lastLeafInserted() const { return m_lastNodeInserted ? &NodeTraversal::lastWithinOrSelf(*m_lastNodeInserted) : 0; }
73        Node* pastLastLeaf() const { return m_lastNodeInserted ? NodeTraversal::next(NodeTraversal::lastWithinOrSelf(*m_lastNodeInserted)) : 0; }
74
75    private:
76        RefPtrWillBeMember<Node> m_firstNodeInserted;
77        RefPtrWillBeMember<Node> m_lastNodeInserted;
78    };
79
80    Node* insertAsListItems(PassRefPtrWillBeRawPtr<HTMLElement> listElement, Element* insertionBlock, const Position&, InsertedNodes&);
81
82    void updateNodesInserted(Node*);
83    bool shouldRemoveEndBR(HTMLBRElement*, const VisiblePosition&);
84
85    bool shouldMergeStart(bool, bool, bool);
86    bool shouldMergeEnd(bool selectionEndWasEndOfParagraph);
87    bool shouldMerge(const VisiblePosition&, const VisiblePosition&);
88
89    void mergeEndIfNeeded();
90
91    void removeUnrenderedTextNodesAtEnds(InsertedNodes&);
92
93    void removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes&);
94    void makeInsertedContentRoundTrippableWithHTMLTreeBuilder(const InsertedNodes&);
95    void moveElementOutOfAncestor(PassRefPtrWillBeRawPtr<Element>, PassRefPtrWillBeRawPtr<ContainerNode> ancestor);
96    void handleStyleSpans(InsertedNodes&);
97
98    VisiblePosition positionAtStartOfInsertedContent() const;
99    VisiblePosition positionAtEndOfInsertedContent() const;
100
101    bool shouldPerformSmartReplace() const;
102    void addSpacesForSmartReplace();
103    void completeHTMLReplacement(const Position& lastPositionToSelect);
104    void mergeTextNodesAroundPosition(Position&, Position& positionOnlyToBeUpdated);
105
106    bool performTrivialReplace(const ReplacementFragment&);
107
108    Position m_startOfInsertedContent;
109    Position m_endOfInsertedContent;
110    RefPtrWillBeMember<EditingStyle> m_insertionStyle;
111    bool m_selectReplacement;
112    bool m_smartReplace;
113    bool m_matchStyle;
114    RefPtrWillBeMember<DocumentFragment> m_documentFragment;
115    bool m_preventNesting;
116    bool m_movingParagraph;
117    EditAction m_editAction;
118    bool m_sanitizeFragment;
119    bool m_shouldMergeEnd;
120};
121
122} // namespace blink
123
124#endif // ReplaceSelectionCommand_h
125