| 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 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 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 | #pragma once |
| 27 | |
| 28 | #include "CompositeEditCommand.h" |
| 29 | #include "NodeTraversal.h" |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | class DocumentFragment; |
| 34 | class ReplacementFragment; |
| 35 | |
| 36 | class ReplaceSelectionCommand : public CompositeEditCommand { |
| 37 | public: |
| 38 | enum CommandOption { |
| 39 | SelectReplacement = 1 << 0, |
| 40 | SmartReplace = 1 << 1, |
| 41 | MatchStyle = 1 << 2, |
| 42 | PreventNesting = 1 << 3, |
| 43 | MovingParagraph = 1 << 4, |
| 44 | SanitizeFragment = 1 << 5, |
| 45 | IgnoreMailBlockquote = 1 << 6, |
| 46 | }; |
| 47 | |
| 48 | static Ref<ReplaceSelectionCommand> create(Document& document, RefPtr<DocumentFragment>&& fragment, OptionSet<CommandOption> options, EditAction editingAction = EditAction::Insert) |
| 49 | { |
| 50 | return adoptRef(*new ReplaceSelectionCommand(document, WTFMove(fragment), options, editingAction)); |
| 51 | } |
| 52 | |
| 53 | VisibleSelection visibleSelectionForInsertedText() const { return m_visibleSelectionForInsertedText; } |
| 54 | |
| 55 | private: |
| 56 | ReplaceSelectionCommand(Document&, RefPtr<DocumentFragment>&&, OptionSet<CommandOption>, EditAction); |
| 57 | |
| 58 | String inputEventData() const final; |
| 59 | RefPtr<DataTransfer> inputEventDataTransfer() const final; |
| 60 | bool willApplyCommand() final; |
| 61 | void doApply() override; |
| 62 | |
| 63 | class InsertedNodes { |
| 64 | public: |
| 65 | void respondToNodeInsertion(Node*); |
| 66 | void willRemoveNodePreservingChildren(Node*); |
| 67 | void willRemoveNode(Node*); |
| 68 | void didReplaceNode(Node*, Node* newNode); |
| 69 | |
| 70 | bool isEmpty() { return !m_firstNodeInserted; } |
| 71 | Node* firstNodeInserted() const { return m_firstNodeInserted.get(); } |
| 72 | Node* lastLeafInserted() const |
| 73 | { |
| 74 | ASSERT(m_lastNodeInserted); |
| 75 | return m_lastNodeInserted->lastDescendant(); |
| 76 | } |
| 77 | Node* pastLastLeaf() const |
| 78 | { |
| 79 | ASSERT(m_lastNodeInserted); |
| 80 | return NodeTraversal::next(*lastLeafInserted()); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | RefPtr<Node> m_firstNodeInserted; |
| 85 | RefPtr<Node> m_lastNodeInserted; |
| 86 | }; |
| 87 | |
| 88 | Node* insertAsListItems(HTMLElement& listElement, Node* insertionNode, const Position&, InsertedNodes&); |
| 89 | |
| 90 | void updateNodesInserted(Node*); |
| 91 | bool shouldRemoveEndBR(Node*, const VisiblePosition&); |
| 92 | |
| 93 | bool shouldMergeStart(bool, bool, bool); |
| 94 | bool shouldMergeEnd(bool selectionEndWasEndOfParagraph); |
| 95 | bool shouldMerge(const VisiblePosition&, const VisiblePosition&); |
| 96 | |
| 97 | void mergeEndIfNeeded(); |
| 98 | |
| 99 | void removeUnrenderedTextNodesAtEnds(InsertedNodes&); |
| 100 | |
| 101 | void removeRedundantStylesAndKeepStyleSpanInline(InsertedNodes&); |
| 102 | void makeInsertedContentRoundTrippableWithHTMLTreeBuilder(InsertedNodes&); |
| 103 | void moveNodeOutOfAncestor(Node&, Node& ancestor, InsertedNodes&); |
| 104 | void handleStyleSpans(InsertedNodes&); |
| 105 | void handlePasteAsQuotationNode(); |
| 106 | |
| 107 | VisiblePosition positionAtStartOfInsertedContent() const; |
| 108 | VisiblePosition positionAtEndOfInsertedContent() const; |
| 109 | |
| 110 | bool shouldPerformSmartReplace() const; |
| 111 | bool shouldPerformSmartParagraphReplace() const; |
| 112 | void addSpacesForSmartReplace(); |
| 113 | void addNewLinesForSmartReplace(); |
| 114 | void completeHTMLReplacement(const Position& lastPositionToSelect); |
| 115 | void mergeTextNodesAroundPosition(Position&, Position& positionOnlyToBeUpdated); |
| 116 | |
| 117 | ReplacementFragment* ensureReplacementFragment(); |
| 118 | bool performTrivialReplace(const ReplacementFragment&); |
| 119 | |
| 120 | VisibleSelection m_visibleSelectionForInsertedText; |
| 121 | Position m_startOfInsertedContent; |
| 122 | Position m_endOfInsertedContent; |
| 123 | RefPtr<EditingStyle> m_insertionStyle; |
| 124 | bool m_selectReplacement; |
| 125 | bool m_smartReplace; |
| 126 | bool m_matchStyle; |
| 127 | RefPtr<DocumentFragment> m_documentFragment; |
| 128 | std::unique_ptr<ReplacementFragment> m_replacementFragment; |
| 129 | String m_documentFragmentHTMLMarkup; |
| 130 | String m_documentFragmentPlainText; |
| 131 | bool m_preventNesting; |
| 132 | bool m_movingParagraph; |
| 133 | bool m_sanitizeFragment; |
| 134 | bool m_shouldMergeEnd; |
| 135 | bool m_ignoreMailBlockquote; |
| 136 | }; |
| 137 | |
| 138 | } // namespace WebCore |
| 139 | |