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