2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
21 #ifndef TextCheckingHelper_h
22 #define TextCheckingHelper_h
24 #include "EditorClient.h"
31 class TextCheckingParagraph {
33 explicit TextCheckingParagraph(PassRefPtr<Range> checkingRange);
34 ~TextCheckingParagraph();
36 int rangeLength() const;
37 PassRefPtr<Range> subrange(int characterOffset, int characterCount) const;
38 int offsetTo(const Position&, ExceptionCode&) const;
39 void expandRangeToNextEnd();
41 int textLength() const { return text().length(); }
42 String textSubstring(unsigned pos, unsigned len = UINT_MAX) const { return text().substring(pos, len); }
43 const UChar* textCharacters() const { return text().characters(); }
44 UChar textCharAt(int index) const { return text()[index]; }
47 bool isTextEmpty() const { return text().isEmpty(); }
48 bool isRangeEmpty() const { return checkingStart() >= checkingEnd(); }
50 int checkingStart() const;
51 int checkingEnd() const;
52 int checkingLength() const;
53 String checkingSubstring() const { return textSubstring(checkingStart(), checkingLength()); }
55 bool checkingRangeMatches(int location, int length) const { return location == checkingStart() && length == checkingLength(); }
56 bool isCheckingRangeCoveredBy(int location, int length) const { return location <= checkingStart() && location + length >= checkingStart() + checkingLength(); }
57 bool checkingRangeCovers(int location, int length) const { return location < checkingEnd() && location + length > checkingStart(); }
58 PassRefPtr<Range> paragraphRange() const;
61 void invalidateParagraphRangeValues();
62 PassRefPtr<Range> checkingRange() const { return m_checkingRange; }
63 PassRefPtr<Range> offsetAsRange() const;
64 const String& text() const;
66 RefPtr<Range> m_checkingRange;
67 mutable RefPtr<Range> m_paragraphRange;
68 mutable RefPtr<Range> m_offsetAsRange;
69 mutable String m_text;
70 mutable int m_checkingStart;
71 mutable int m_checkingEnd;
72 mutable int m_checkingLength;
75 class TextCheckingHelper : public Noncopyable {
77 TextCheckingHelper(EditorClient*, PassRefPtr<Range>);
78 ~TextCheckingHelper();
80 String findFirstMisspelling(int& firstMisspellingOffset, bool markAll, RefPtr<Range>& firstMisspellingRange);
81 String findFirstMisspellingOrBadGrammar(bool checkGrammar, bool& outIsSpelling, int& outFirstFoundOffset, GrammarDetail& outGrammarDetail);
82 String findFirstBadGrammar(GrammarDetail& outGrammarDetail, int& outGrammarPhraseOffset, bool markAll);
83 int findFirstGrammarDetail(const Vector<GrammarDetail>& grammarDetails, int badGrammarPhraseLocation, int badGrammarPhraseLength, int startOffset, int endOffset, bool markAll);
84 void markAllMisspellings(RefPtr<Range>& firstMisspellingRange);
85 void markAllBadGrammar();
87 bool isUngrammatical(Vector<String>& guessesVector) const;
88 Vector<String> guessesForMisspelledOrUngrammaticalRange(bool checkGrammar, bool& misspelled, bool& ungrammatical) const;
90 EditorClient* m_client;
91 RefPtr<Range> m_range;
94 } // namespace WebCore
96 #endif // TextCheckingHelper_h