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