| 1 | /* |
| 2 | * Copyright (C) 2011, 2015 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 | #include "config.h" |
| 27 | #include "SpellingCorrectionCommand.h" |
| 28 | |
| 29 | #include "AlternativeTextController.h" |
| 30 | #include "DataTransfer.h" |
| 31 | #include "Document.h" |
| 32 | #include "DocumentFragment.h" |
| 33 | #include "Editor.h" |
| 34 | #include "Frame.h" |
| 35 | #include "ReplaceSelectionCommand.h" |
| 36 | #include "SetSelectionCommand.h" |
| 37 | #include "StaticRange.h" |
| 38 | #include "TextIterator.h" |
| 39 | #include "markup.h" |
| 40 | |
| 41 | namespace WebCore { |
| 42 | |
| 43 | #if USE(AUTOCORRECTION_PANEL) |
| 44 | // On Mac OS X, we use this command to keep track of user undoing a correction for the first time. |
| 45 | // This information is needed by spell checking service to update user specific data. |
| 46 | class SpellingCorrectionRecordUndoCommand : public SimpleEditCommand { |
| 47 | public: |
| 48 | static Ref<SpellingCorrectionRecordUndoCommand> create(Document& document, const String& corrected, const String& correction) |
| 49 | { |
| 50 | return adoptRef(*new SpellingCorrectionRecordUndoCommand(document, corrected, correction)); |
| 51 | } |
| 52 | private: |
| 53 | SpellingCorrectionRecordUndoCommand(Document& document, const String& corrected, const String& correction) |
| 54 | : SimpleEditCommand(document) |
| 55 | , m_corrected(corrected) |
| 56 | , m_correction(correction) |
| 57 | , m_hasBeenUndone(false) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void doApply() override |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void doUnapply() override |
| 66 | { |
| 67 | if (!m_hasBeenUndone) { |
| 68 | frame().editor().unappliedSpellCorrection(startingSelection(), m_corrected, m_correction); |
| 69 | m_hasBeenUndone = true; |
| 70 | } |
| 71 | |
| 72 | } |
| 73 | |
| 74 | #ifndef NDEBUG |
| 75 | void getNodesInCommand(HashSet<Node*>&) override |
| 76 | { |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | String m_corrected; |
| 81 | String m_correction; |
| 82 | bool m_hasBeenUndone; |
| 83 | }; |
| 84 | #endif |
| 85 | |
| 86 | SpellingCorrectionCommand::SpellingCorrectionCommand(Range& rangeToBeCorrected, const String& correction) |
| 87 | : CompositeEditCommand(rangeToBeCorrected.startContainer().document(), EditAction::InsertReplacement) |
| 88 | , m_rangeToBeCorrected(rangeToBeCorrected) |
| 89 | , m_selectionToBeCorrected(m_rangeToBeCorrected) |
| 90 | , m_correction(correction) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | bool SpellingCorrectionCommand::willApplyCommand() |
| 95 | { |
| 96 | m_correctionFragment = createFragmentFromText(m_rangeToBeCorrected, m_correction); |
| 97 | return CompositeEditCommand::willApplyCommand(); |
| 98 | } |
| 99 | |
| 100 | void SpellingCorrectionCommand::doApply() |
| 101 | { |
| 102 | m_corrected = plainText(m_rangeToBeCorrected.ptr()); |
| 103 | if (!m_corrected.length()) |
| 104 | return; |
| 105 | |
| 106 | if (!frame().selection().shouldChangeSelection(m_selectionToBeCorrected)) |
| 107 | return; |
| 108 | |
| 109 | applyCommandToComposite(SetSelectionCommand::create(m_selectionToBeCorrected, FrameSelection::defaultSetSelectionOptions() | FrameSelection::SpellCorrectionTriggered)); |
| 110 | #if USE(AUTOCORRECTION_PANEL) |
| 111 | applyCommandToComposite(SpellingCorrectionRecordUndoCommand::create(document(), m_corrected, m_correction)); |
| 112 | #endif |
| 113 | |
| 114 | applyCommandToComposite(ReplaceSelectionCommand::create(document(), WTFMove(m_correctionFragment), ReplaceSelectionCommand::MatchStyle, EditAction::Paste)); |
| 115 | } |
| 116 | |
| 117 | String SpellingCorrectionCommand::inputEventData() const |
| 118 | { |
| 119 | if (isEditingTextAreaOrTextInput()) |
| 120 | return m_correction; |
| 121 | |
| 122 | return CompositeEditCommand::inputEventData(); |
| 123 | } |
| 124 | |
| 125 | Vector<RefPtr<StaticRange>> SpellingCorrectionCommand::targetRanges() const |
| 126 | { |
| 127 | return { 1, StaticRange::createFromRange(m_rangeToBeCorrected) }; |
| 128 | } |
| 129 | |
| 130 | RefPtr<DataTransfer> SpellingCorrectionCommand::inputEventDataTransfer() const |
| 131 | { |
| 132 | if (!isEditingTextAreaOrTextInput()) |
| 133 | return DataTransfer::createForInputEvent(m_correction, serializeFragment(*m_correctionFragment, SerializedNodes::SubtreeIncludingNode)); |
| 134 | |
| 135 | return CompositeEditCommand::inputEventDataTransfer(); |
| 136 | } |
| 137 | |
| 138 | bool SpellingCorrectionCommand::shouldRetainAutocorrectionIndicator() const |
| 139 | { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | } // namespace WebCore |
| 144 | |