1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef TextCheckingHelper_h
22#define TextCheckingHelper_h
23
24#include "EditorClient.h"
25#include "ExceptionCode.h"
26#include "TextChecking.h"
27#include <wtf/text/WTFString.h>
28
29namespace WebCore {
30
31class Range;
32class Position;
33struct TextCheckingResult;
34
35class TextCheckingParagraph {
36public:
37    explicit TextCheckingParagraph(PassRefPtr<Range> checkingRange);
38    TextCheckingParagraph(PassRefPtr<Range> checkingRange, PassRefPtr<Range> paragraphRange);
39    ~TextCheckingParagraph();
40
41    int rangeLength() const;
42    PassRefPtr<Range> subrange(int characterOffset, int characterCount) const;
43    int offsetTo(const Position&, ExceptionCode&) const;
44    void expandRangeToNextEnd();
45
46    int textLength() const { return text().length(); }
47    String textSubstring(unsigned pos, unsigned len = UINT_MAX) const { return text().substring(pos, len); }
48    const UChar* textCharacters() const { return text().characters(); }
49    UChar textCharAt(int index) const { return text()[static_cast<unsigned>(index)]; }
50
51    bool isEmpty() const;
52    bool isTextEmpty() const { return text().isEmpty(); }
53    bool isRangeEmpty() const { return checkingStart() >= checkingEnd(); }
54
55    int checkingStart() const;
56    int checkingEnd() const;
57    int checkingLength() const;
58    String checkingSubstring() const { return textSubstring(checkingStart(), checkingLength()); }
59
60    bool checkingRangeMatches(int location, int length) const { return location == checkingStart() && length == checkingLength(); }
61    bool isCheckingRangeCoveredBy(int location, int length) const { return location <= checkingStart() && location + length >= checkingStart() + checkingLength(); }
62    bool checkingRangeCovers(int location, int length) const { return location < checkingEnd() && location + length > checkingStart(); }
63    PassRefPtr<Range> paragraphRange() const;
64
65private:
66    void invalidateParagraphRangeValues();
67    PassRefPtr<Range> checkingRange() const { return m_checkingRange; }
68    PassRefPtr<Range> offsetAsRange() const;
69    const String& text() const;
70
71    RefPtr<Range> m_checkingRange;
72    mutable RefPtr<Range> m_paragraphRange;
73    mutable RefPtr<Range> m_offsetAsRange;
74    mutable String m_text;
75    mutable int m_checkingStart;
76    mutable int m_checkingEnd;
77    mutable int m_checkingLength;
78};
79
80class TextCheckingHelper {
81    WTF_MAKE_NONCOPYABLE(TextCheckingHelper);
82public:
83    TextCheckingHelper(EditorClient*, PassRefPtr<Range>);
84    ~TextCheckingHelper();
85
86    String findFirstMisspelling(int& firstMisspellingOffset, bool markAll, RefPtr<Range>& firstMisspellingRange);
87    String findFirstMisspellingOrBadGrammar(bool checkGrammar, bool& outIsSpelling, int& outFirstFoundOffset, GrammarDetail& outGrammarDetail);
88    void markAllMisspellings(RefPtr<Range>& firstMisspellingRange);
89#if USE(GRAMMAR_CHECKING)
90    String findFirstBadGrammar(GrammarDetail& outGrammarDetail, int& outGrammarPhraseOffset, bool markAll);
91    int findFirstGrammarDetail(const Vector<GrammarDetail>& grammarDetails, int badGrammarPhraseLocation, int badGrammarPhraseLength, int startOffset, int endOffset, bool markAll);
92    void markAllBadGrammar();
93    bool isUngrammatical(Vector<String>& guessesVector) const;
94#endif
95    Vector<String> guessesForMisspelledOrUngrammaticalRange(bool checkGrammar, bool& misspelled, bool& ungrammatical) const;
96private:
97    EditorClient* m_client;
98    RefPtr<Range> m_range;
99
100    bool unifiedTextCheckerEnabled() const;
101};
102
103void checkTextOfParagraph(TextCheckerClient*, const UChar* text, int length,
104    TextCheckingTypeMask checkingTypes, Vector<TextCheckingResult>& results);
105
106bool unifiedTextCheckerEnabled(const Frame*);
107
108} // namespace WebCore
109
110#endif // TextCheckingHelper_h
111