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 ReplaceSelectionCommand_h
27 #define ReplaceSelectionCommand_h
29 #include "core/dom/NodeTraversal.h"
30 #include "core/editing/CompositeEditCommand.h"
34 class DocumentFragment;
35 class ReplacementFragment;
37 class ReplaceSelectionCommand final : public CompositeEditCommand {
40 SelectReplacement = 1 << 0,
41 SmartReplace = 1 << 1,
43 PreventNesting = 1 << 3,
44 MovingParagraph = 1 << 4,
45 SanitizeFragment = 1 << 5
48 typedef unsigned CommandOptions;
50 static PassRefPtrWillBeRawPtr<ReplaceSelectionCommand> create(Document& document, PassRefPtrWillBeRawPtr<DocumentFragment> fragment, CommandOptions options, EditAction action = EditActionPaste)
52 return adoptRefWillBeNoop(new ReplaceSelectionCommand(document, fragment, options, action));
55 virtual void trace(Visitor*) override;
58 ReplaceSelectionCommand(Document&, PassRefPtrWillBeRawPtr<DocumentFragment>, CommandOptions, EditAction);
60 virtual void doApply() override;
61 virtual EditAction editingAction() const override;
66 void respondToNodeInsertion(Node&);
67 void willRemoveNodePreservingChildren(Node&);
68 void willRemoveNode(Node&);
69 void didReplaceNode(Node&, Node& newNode);
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; }
76 RefPtrWillBeMember<Node> m_firstNodeInserted;
77 RefPtrWillBeMember<Node> m_lastNodeInserted;
80 Node* insertAsListItems(PassRefPtrWillBeRawPtr<HTMLElement> listElement, Element* insertionBlock, const Position&, InsertedNodes&);
82 void updateNodesInserted(Node*);
83 bool shouldRemoveEndBR(HTMLBRElement*, const VisiblePosition&);
85 bool shouldMergeStart(bool, bool, bool);
86 bool shouldMergeEnd(bool selectionEndWasEndOfParagraph);
87 bool shouldMerge(const VisiblePosition&, const VisiblePosition&);
89 void mergeEndIfNeeded();
91 void removeUnrenderedTextNodesAtEnds(InsertedNodes&);
93 void removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes&);
94 void makeInsertedContentRoundTrippableWithHTMLTreeBuilder(const InsertedNodes&);
95 void moveElementOutOfAncestor(PassRefPtrWillBeRawPtr<Element>, PassRefPtrWillBeRawPtr<ContainerNode> ancestor);
96 void handleStyleSpans(InsertedNodes&);
98 VisiblePosition positionAtStartOfInsertedContent() const;
99 VisiblePosition positionAtEndOfInsertedContent() const;
101 bool shouldPerformSmartReplace() const;
102 void addSpacesForSmartReplace();
103 void completeHTMLReplacement(const Position& lastPositionToSelect);
104 void mergeTextNodesAroundPosition(Position&, Position& positionOnlyToBeUpdated);
106 bool performTrivialReplace(const ReplacementFragment&);
108 Position m_startOfInsertedContent;
109 Position m_endOfInsertedContent;
110 RefPtrWillBeMember<EditingStyle> m_insertionStyle;
111 bool m_selectReplacement;
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;
124 #endif // ReplaceSelectionCommand_h