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 CompositeEditCommand_h
     27 #define CompositeEditCommand_h
     28 
     29 #include "CSSPropertyNames.h"
     30 #include "core/editing/EditCommand.h"
     31 #include "core/editing/UndoStep.h"
     32 #include "wtf/Vector.h"
     33 
     34 namespace WebCore {
     35 
     36 class EditingStyle;
     37 class Element;
     38 class HTMLElement;
     39 class Text;
     40 
     41 class EditCommandComposition : public UndoStep {
     42 public:
     43     static PassRefPtr<EditCommandComposition> create(Document*, const VisibleSelection&, const VisibleSelection&, EditAction);
     44 
     45     virtual bool belongsTo(const Frame&) const OVERRIDE;
     46     virtual void unapply() OVERRIDE;
     47     virtual void reapply() OVERRIDE;
     48     EditAction editingAction() const OVERRIDE { return m_editAction; }
     49     void append(SimpleEditCommand*);
     50     bool wasCreateLinkCommand() const { return m_editAction == EditActionCreateLink; }
     51 
     52     const VisibleSelection& startingSelection() const { return m_startingSelection; }
     53     const VisibleSelection& endingSelection() const { return m_endingSelection; }
     54     void setStartingSelection(const VisibleSelection&);
     55     void setEndingSelection(const VisibleSelection&);
     56     Element* startingRootEditableElement() const { return m_startingRootEditableElement.get(); }
     57     Element* endingRootEditableElement() const { return m_endingRootEditableElement.get(); }
     58 
     59 private:
     60     EditCommandComposition(Document*, const VisibleSelection& startingSelection, const VisibleSelection& endingSelection, EditAction);
     61 
     62     RefPtr<Document> m_document;
     63     VisibleSelection m_startingSelection;
     64     VisibleSelection m_endingSelection;
     65     Vector<RefPtr<SimpleEditCommand> > m_commands;
     66     RefPtr<Element> m_startingRootEditableElement;
     67     RefPtr<Element> m_endingRootEditableElement;
     68     EditAction m_editAction;
     69 };
     70 
     71 class CompositeEditCommand : public EditCommand {
     72 public:
     73     virtual ~CompositeEditCommand();
     74 
     75     void apply();
     76     bool isFirstCommand(EditCommand* command) { return !m_commands.isEmpty() && m_commands.first() == command; }
     77     EditCommandComposition* composition() { return m_composition.get(); }
     78     EditCommandComposition* ensureComposition();
     79 
     80     virtual bool isCreateLinkCommand() const;
     81     virtual bool isTypingCommand() const;
     82     virtual bool preservesTypingStyle() const;
     83     virtual bool shouldRetainAutocorrectionIndicator() const;
     84     virtual void setShouldRetainAutocorrectionIndicator(bool);
     85     virtual bool shouldStopCaretBlinking() const { return false; }
     86 
     87 protected:
     88     explicit CompositeEditCommand(Document&);
     89 
     90     //
     91     // sugary-sweet convenience functions to help create and apply edit commands in composite commands
     92     //
     93     void appendNode(PassRefPtr<Node>, PassRefPtr<ContainerNode> parent);
     94     void applyCommandToComposite(PassRefPtr<EditCommand>);
     95     void applyCommandToComposite(PassRefPtr<CompositeEditCommand>, const VisibleSelection&);
     96     void applyStyle(const EditingStyle*, EditAction = EditActionChangeAttributes);
     97     void applyStyle(const EditingStyle*, const Position& start, const Position& end, EditAction = EditActionChangeAttributes);
     98     void applyStyledElement(PassRefPtr<Element>);
     99     void removeStyledElement(PassRefPtr<Element>);
    100     void deleteSelection(bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
    101     void deleteSelection(const VisibleSelection&, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = true, bool sanitizeMarkup = true);
    102     virtual void deleteTextFromNode(PassRefPtr<Text>, unsigned offset, unsigned count);
    103     bool isRemovableBlock(const Node*);
    104     void insertNodeAfter(PassRefPtr<Node>, PassRefPtr<Node> refChild);
    105     void insertNodeAt(PassRefPtr<Node>, const Position&);
    106     void insertNodeAtTabSpanPosition(PassRefPtr<Node>, const Position&);
    107     void insertNodeBefore(PassRefPtr<Node>, PassRefPtr<Node> refChild, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
    108     void insertParagraphSeparator(bool useDefaultParagraphElement = false, bool pasteBlockqutoeIntoUnquotedArea = false);
    109     void insertLineBreak();
    110     void insertTextIntoNode(PassRefPtr<Text>, unsigned offset, const String& text);
    111     void mergeIdenticalElements(PassRefPtr<Element>, PassRefPtr<Element>);
    112     void rebalanceWhitespace();
    113     void rebalanceWhitespaceAt(const Position&);
    114     void rebalanceWhitespaceOnTextSubstring(PassRefPtr<Text>, int startOffset, int endOffset);
    115     void prepareWhitespaceAtPositionForSplit(Position&);
    116     void replaceCollapsibleWhitespaceWithNonBreakingSpaceIfNeeded(const VisiblePosition&);
    117     bool canRebalance(const Position&) const;
    118     bool shouldRebalanceLeadingWhitespaceFor(const String&) const;
    119     void removeCSSProperty(PassRefPtr<Element>, CSSPropertyID);
    120     void removeNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute);
    121     void removeChildrenInRange(PassRefPtr<Node>, unsigned from, unsigned to);
    122     virtual void removeNode(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
    123     HTMLElement* replaceElementWithSpanPreservingChildrenAndAttributes(PassRefPtr<HTMLElement>);
    124     void removeNodePreservingChildren(PassRefPtr<Node>, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable);
    125     void removeNodeAndPruneAncestors(PassRefPtr<Node>, Node* excludeNode = 0);
    126     void moveRemainingSiblingsToNewParent(Node*, Node* pastLastNodeToMove, PassRefPtr<Element> prpNewParent);
    127     void updatePositionForNodeRemovalPreservingChildren(Position&, Node*);
    128     void prune(PassRefPtr<Node>, Node* excludeNode = 0);
    129     void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
    130     Position replaceSelectedTextInNode(const String&);
    131     void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
    132     Position positionOutsideTabSpan(const Position&);
    133     void setNodeAttribute(PassRefPtr<Element>, const QualifiedName& attribute, const AtomicString& value);
    134     void splitElement(PassRefPtr<Element>, PassRefPtr<Node> atChild);
    135     void splitTextNode(PassRefPtr<Text>, unsigned offset);
    136     void splitTextNodeContainingElement(PassRefPtr<Text>, unsigned offset);
    137     void wrapContentsInDummySpan(PassRefPtr<Element>);
    138 
    139     void deleteInsignificantText(PassRefPtr<Text>, unsigned start, unsigned end);
    140     void deleteInsignificantText(const Position& start, const Position& end);
    141     void deleteInsignificantTextDownstream(const Position&);
    142 
    143     PassRefPtr<Node> appendBlockPlaceholder(PassRefPtr<Element>);
    144     PassRefPtr<Node> insertBlockPlaceholder(const Position&);
    145     PassRefPtr<Node> addBlockPlaceholderIfNeeded(Element*);
    146     void removePlaceholderAt(const Position&);
    147 
    148     PassRefPtr<Node> insertNewDefaultParagraphElementAt(const Position&);
    149 
    150     PassRefPtr<Node> moveParagraphContentsToNewBlockIfNecessary(const Position&);
    151 
    152     void pushAnchorElementDown(Node*);
    153 
    154     // FIXME: preserveSelection and preserveStyle should be enums
    155     void moveParagraph(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0);
    156     void moveParagraphs(const VisiblePosition&, const VisiblePosition&, const VisiblePosition&, bool preserveSelection = false, bool preserveStyle = true, Node* constrainingAncestor = 0);
    157     void moveParagraphWithClones(const VisiblePosition& startOfParagraphToMove, const VisiblePosition& endOfParagraphToMove, Element* blockElement, Node* outerNode);
    158     void cloneParagraphUnderNewElement(Position& start, Position& end, Node* outerNode, Element* blockElement);
    159     void cleanupAfterDeletion(VisiblePosition destination = VisiblePosition());
    160 
    161     bool breakOutOfEmptyListItem();
    162     bool breakOutOfEmptyMailBlockquotedParagraph();
    163 
    164     Position positionAvoidingSpecialElementBoundary(const Position&);
    165 
    166     PassRefPtr<Node> splitTreeToNode(Node*, Node*, bool splitAncestor = false);
    167 
    168     Vector<RefPtr<EditCommand> > m_commands;
    169 
    170 private:
    171     bool isCompositeEditCommand() const OVERRIDE { return true; }
    172 
    173     RefPtr<EditCommandComposition> m_composition;
    174 };
    175 
    176 DEFINE_TYPE_CASTS(CompositeEditCommand, EditCommand, command, command->isCompositeEditCommand(), command.isCompositeEditCommand());
    177 
    178 } // namespace WebCore
    179 
    180 #endif // CompositeEditCommand_h
    181