| 1 | /* |
| 2 | * Copyright (c) 2011 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #pragma once |
| 33 | |
| 34 | #include <wtf/OptionSet.h> |
| 35 | #include <wtf/RefCounted.h> |
| 36 | #include <wtf/Vector.h> |
| 37 | #include <wtf/text/WTFString.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | enum class TextCheckingType : uint8_t { |
| 42 | None = 0, |
| 43 | Spelling = 1 << 0, |
| 44 | Grammar = 1 << 1, |
| 45 | Link = 1 << 2, |
| 46 | Quote = 1 << 3, |
| 47 | Dash = 1 << 4, |
| 48 | Replacement = 1 << 5, |
| 49 | Correction = 1 << 6, |
| 50 | ShowCorrectionPanel = 1 << 7, |
| 51 | }; |
| 52 | |
| 53 | #if PLATFORM(MAC) |
| 54 | typedef uint64_t NSTextCheckingTypes; |
| 55 | WEBCORE_EXPORT NSTextCheckingTypes nsTextCheckingTypes(OptionSet<TextCheckingType>); |
| 56 | #endif |
| 57 | |
| 58 | enum TextCheckingProcessType { |
| 59 | TextCheckingProcessBatch, |
| 60 | TextCheckingProcessIncremental |
| 61 | }; |
| 62 | |
| 63 | struct GrammarDetail { |
| 64 | int location; |
| 65 | int length; |
| 66 | Vector<String> guesses; |
| 67 | String userDescription; |
| 68 | }; |
| 69 | |
| 70 | struct TextCheckingResult { |
| 71 | TextCheckingType type; |
| 72 | int location; |
| 73 | int length; |
| 74 | Vector<GrammarDetail> details; |
| 75 | String replacement; |
| 76 | }; |
| 77 | |
| 78 | const int unrequestedTextCheckingSequence = -1; |
| 79 | |
| 80 | class TextCheckingRequestData { |
| 81 | friend class SpellCheckRequest; // For access to m_sequence. |
| 82 | public: |
| 83 | TextCheckingRequestData() = default; |
| 84 | TextCheckingRequestData(int sequence, const String& text, OptionSet<TextCheckingType> checkingTypes, TextCheckingProcessType processType) |
| 85 | : m_text { text } |
| 86 | , m_sequence { sequence } |
| 87 | , m_processType { processType } |
| 88 | , m_checkingTypes { checkingTypes } |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | int sequence() const { return m_sequence; } |
| 93 | const String& text() const { return m_text; } |
| 94 | OptionSet<TextCheckingType> checkingTypes() const { return m_checkingTypes; } |
| 95 | TextCheckingProcessType processType() const { return m_processType; } |
| 96 | |
| 97 | private: |
| 98 | String m_text; |
| 99 | int m_sequence { unrequestedTextCheckingSequence }; |
| 100 | TextCheckingProcessType m_processType { TextCheckingProcessIncremental }; |
| 101 | OptionSet<TextCheckingType> m_checkingTypes; |
| 102 | }; |
| 103 | |
| 104 | class TextCheckingRequest : public RefCounted<TextCheckingRequest> { |
| 105 | public: |
| 106 | virtual ~TextCheckingRequest() = default; |
| 107 | |
| 108 | virtual const TextCheckingRequestData& data() const = 0; |
| 109 | virtual void didSucceed(const Vector<TextCheckingResult>&) = 0; |
| 110 | virtual void didCancel() = 0; |
| 111 | }; |
| 112 | |
| 113 | } |
| 114 | |