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
30namespace WebCore {
31
32class EditingStyle;
33
34class DeleteSelectionCommand : public CompositeEditCommand {
35public:
36 static Ref<DeleteSelectionCommand> create(Document& document, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = false, bool sanitizeMarkup = true, EditAction editingAction = EditAction::Delete)
37 {
38 return adoptRef(*new DeleteSelectionCommand(document, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements, sanitizeMarkup, editingAction));
39 }
40 static Ref<DeleteSelectionCommand> create(const VisibleSelection& selection, bool smartDelete = false, bool mergeBlocksAfterDelete = true, bool replace = false, bool expandForSpecialElements = false, bool sanitizeMarkup = true, EditAction editingAction = EditAction::Delete)
41 {
42 return adoptRef(*new DeleteSelectionCommand(selection, smartDelete, mergeBlocksAfterDelete, replace, expandForSpecialElements, sanitizeMarkup, editingAction));
43 }
44
45protected:
46 DeleteSelectionCommand(Document&, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool santizeMarkup, EditAction = EditAction::Delete);
47
48private:
49 DeleteSelectionCommand(const VisibleSelection&, bool smartDelete, bool mergeBlocksAfterDelete, bool replace, bool expandForSpecialElements, bool sanitizeMarkup, EditAction);
50
51 void doApply() override;
52
53 bool preservesTypingStyle() const override;
54
55 void initializeStartEnd(Position&, Position&);
56 void setStartingSelectionOnSmartDelete(const Position&, const Position&);
57 bool initializePositionData();
58 void saveTypingStyleState();
59 void insertPlaceholderForAncestorBlockContent();
60 bool handleSpecialCaseBRDelete();
61 void handleGeneralDelete();
62 void fixupWhitespace();
63 void mergeParagraphs();
64 void removePreviouslySelectedEmptyTableRows();
65 void calculateEndingPosition();
66 void calculateTypingStyleAfterDelete();
67 void clearTransientState();
68 void makeStylingElementsDirectChildrenOfEditableRootToPreventStyleLoss();
69 void removeNode(Node&, ShouldAssumeContentIsAlwaysEditable = DoNotAssumeContentIsAlwaysEditable) override;
70 void deleteTextFromNode(Text&, unsigned, unsigned) override;
71 void removeRedundantBlocks();
72 bool shouldSmartDeleteParagraphSpacers();
73 void smartDeleteParagraphSpacers();
74
75 // This function provides access to original string after the correction has been deleted.
76 String originalStringForAutocorrectionAtBeginningOfSelection();
77
78 void removeNodeUpdatingStates(Node&, ShouldAssumeContentIsAlwaysEditable);
79 void insertBlockPlaceholderForTableCellIfNeeded(Element&);
80
81 bool m_hasSelectionToDelete;
82 bool m_smartDelete;
83 bool m_mergeBlocksAfterDelete;
84 bool m_needPlaceholder;
85 bool m_replace;
86 bool m_expandForSpecialElements;
87 bool m_pruneStartBlockIfNecessary;
88 bool m_startsAtEmptyLine;
89 bool m_sanitizeMarkup;
90
91 // This data is transient and should be cleared at the end of the doApply function.
92 VisibleSelection m_selectionToDelete;
93 Position m_upstreamStart;
94 Position m_downstreamStart;
95 Position m_upstreamEnd;
96 Position m_downstreamEnd;
97 Position m_endingPosition;
98 Position m_leadingWhitespace;
99 Position m_trailingWhitespace;
100 RefPtr<Node> m_startBlock;
101 RefPtr<Node> m_endBlock;
102 RefPtr<EditingStyle> m_typingStyle;
103 RefPtr<EditingStyle> m_deleteIntoBlockquoteStyle;
104 RefPtr<Node> m_startRoot;
105 RefPtr<Node> m_endRoot;
106 RefPtr<Node> m_startTableRow;
107 RefPtr<Node> m_endTableRow;
108 RefPtr<Node> m_temporaryPlaceholder;
109};
110
111} // namespace WebCore
112