Home | History | Annotate | Download | only in editing
      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 : 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();
     59     virtual EditAction editingAction() const;
     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->lastDescendant(); }
     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     void handlePasteAsQuotationNode();
     95 
     96     VisiblePosition positionAtStartOfInsertedContent() const;
     97     VisiblePosition positionAtEndOfInsertedContent() const;
     98 
     99     bool shouldPerformSmartReplace() const;
    100     void addSpacesForSmartReplace();
    101     void completeHTMLReplacement(const Position& lastPositionToSelect);
    102     void mergeTextNodesAroundPosition(Position&, Position& positionOnlyToBeUpdated);
    103 
    104     bool performTrivialReplace(const ReplacementFragment&);
    105 
    106     Position m_startOfInsertedContent;
    107     Position m_endOfInsertedContent;
    108     RefPtr<EditingStyle> m_insertionStyle;
    109     bool m_selectReplacement;
    110     bool m_smartReplace;
    111     bool m_matchStyle;
    112     RefPtr<DocumentFragment> m_documentFragment;
    113     bool m_preventNesting;
    114     bool m_movingParagraph;
    115     EditAction m_editAction;
    116     bool m_sanitizeFragment;
    117     bool m_shouldMergeEnd;
    118 };
    119 
    120 } // namespace WebCore
    121 
    122 #endif // ReplaceSelectionCommand_h
    123