| 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 "AXTextStateChangeIntent.h" |
| 29 | #include "EditAction.h" |
| 30 | #include "VisibleSelection.h" |
| 31 | |
| 32 | #ifndef NDEBUG |
| 33 | #include <wtf/HashSet.h> |
| 34 | #endif |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class CompositeEditCommand; |
| 39 | class Document; |
| 40 | class Element; |
| 41 | class Frame; |
| 42 | |
| 43 | String inputTypeNameForEditingAction(EditAction); |
| 44 | |
| 45 | class EditCommand : public RefCounted<EditCommand> { |
| 46 | public: |
| 47 | virtual ~EditCommand(); |
| 48 | |
| 49 | void setParent(CompositeEditCommand*); |
| 50 | |
| 51 | virtual EditAction editingAction() const; |
| 52 | |
| 53 | const VisibleSelection& startingSelection() const { return m_startingSelection; } |
| 54 | const VisibleSelection& endingSelection() const { return m_endingSelection; } |
| 55 | |
| 56 | virtual bool isInsertTextCommand() const { return false; } |
| 57 | virtual bool isSimpleEditCommand() const { return false; } |
| 58 | virtual bool isCompositeEditCommand() const { return false; } |
| 59 | bool isTopLevelCommand() const { return !m_parent; } |
| 60 | |
| 61 | virtual void doApply() = 0; |
| 62 | |
| 63 | protected: |
| 64 | explicit EditCommand(Document&, EditAction = EditAction::Unspecified); |
| 65 | EditCommand(Document&, const VisibleSelection&, const VisibleSelection&); |
| 66 | |
| 67 | const Frame& frame() const; |
| 68 | Frame& frame(); |
| 69 | const Document& document() const { return m_document; } |
| 70 | Document& document() { return m_document; } |
| 71 | CompositeEditCommand* parent() const { return m_parent; } |
| 72 | void setStartingSelection(const VisibleSelection&); |
| 73 | WEBCORE_EXPORT void setEndingSelection(const VisibleSelection&); |
| 74 | |
| 75 | bool isEditingTextAreaOrTextInput() const; |
| 76 | |
| 77 | void postTextStateChangeNotification(AXTextEditType, const String&); |
| 78 | void postTextStateChangeNotification(AXTextEditType, const String&, const VisiblePosition&); |
| 79 | |
| 80 | private: |
| 81 | Ref<Document> m_document; |
| 82 | VisibleSelection m_startingSelection; |
| 83 | VisibleSelection m_endingSelection; |
| 84 | CompositeEditCommand* m_parent { nullptr }; |
| 85 | EditAction m_editingAction { EditAction::Unspecified }; |
| 86 | }; |
| 87 | |
| 88 | enum ShouldAssumeContentIsAlwaysEditable { |
| 89 | AssumeContentIsAlwaysEditable, |
| 90 | DoNotAssumeContentIsAlwaysEditable, |
| 91 | }; |
| 92 | |
| 93 | class SimpleEditCommand : public EditCommand { |
| 94 | public: |
| 95 | virtual void doUnapply() = 0; |
| 96 | virtual void doReapply(); // calls doApply() |
| 97 | |
| 98 | #ifndef NDEBUG |
| 99 | virtual void getNodesInCommand(HashSet<Node*>&) = 0; |
| 100 | #endif |
| 101 | |
| 102 | protected: |
| 103 | explicit SimpleEditCommand(Document&, EditAction = EditAction::Unspecified); |
| 104 | |
| 105 | #ifndef NDEBUG |
| 106 | void addNodeAndDescendants(Node*, HashSet<Node*>&); |
| 107 | #endif |
| 108 | |
| 109 | private: |
| 110 | bool isSimpleEditCommand() const override { return true; } |
| 111 | }; |
| 112 | |
| 113 | inline SimpleEditCommand* toSimpleEditCommand(EditCommand* command) |
| 114 | { |
| 115 | ASSERT(command); |
| 116 | ASSERT_WITH_SECURITY_IMPLICATION(command->isSimpleEditCommand()); |
| 117 | return static_cast<SimpleEditCommand*>(command); |
| 118 | } |
| 119 | |
| 120 | } // namespace WebCore |
| 121 | |