Home | History | Annotate | Download | only in editing
      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 
     26 namespace WebCore {
     27 
     28 class Range;
     29 class Position;
     30 
     31 class TextCheckingParagraph {
     32 public:
     33     explicit TextCheckingParagraph(PassRefPtr<Range> checkingRange);
     34     ~TextCheckingParagraph();
     35 
     36     int rangeLength() const;
     37     PassRefPtr<Range> subrange(int characterOffset, int characterCount) const;
     38     int offsetTo(const Position&, ExceptionCode&) const;
     39     void expandRangeToNextEnd();
     40 
     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]; }
     45 
     46     bool isEmpty() const;
     47     bool isTextEmpty() const { return text().isEmpty(); }
     48     bool isRangeEmpty() const { return checkingStart() >= checkingEnd(); }
     49 
     50     int checkingStart() const;
     51     int checkingEnd() const;
     52     int checkingLength() const;
     53     String checkingSubstring() const { return textSubstring(checkingStart(), checkingLength()); }
     54 
     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;
     59 
     60 private:
     61     void invalidateParagraphRangeValues();
     62     PassRefPtr<Range> checkingRange() const { return m_checkingRange; }
     63     PassRefPtr<Range> offsetAsRange() const;
     64     const String& text() const;
     65 
     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;
     73 };
     74 
     75 class TextCheckingHelper {
     76     WTF_MAKE_NONCOPYABLE(TextCheckingHelper);
     77 public:
     78     TextCheckingHelper(EditorClient*, PassRefPtr<Range>);
     79     ~TextCheckingHelper();
     80 
     81     String findFirstMisspelling(int& firstMisspellingOffset, bool markAll, RefPtr<Range>& firstMisspellingRange);
     82     String findFirstMisspellingOrBadGrammar(bool checkGrammar, bool& outIsSpelling, int& outFirstFoundOffset, GrammarDetail& outGrammarDetail);
     83     String findFirstBadGrammar(GrammarDetail& outGrammarDetail, int& outGrammarPhraseOffset, bool markAll);
     84     int findFirstGrammarDetail(const Vector<GrammarDetail>& grammarDetails, int badGrammarPhraseLocation, int badGrammarPhraseLength, int startOffset, int endOffset, bool markAll);
     85     void markAllMisspellings(RefPtr<Range>& firstMisspellingRange);
     86     void markAllBadGrammar();
     87 
     88     bool isUngrammatical(Vector<String>& guessesVector) const;
     89     Vector<String> guessesForMisspelledOrUngrammaticalRange(bool checkGrammar, bool& misspelled, bool& ungrammatical) const;
     90 private:
     91     EditorClient* m_client;
     92     RefPtr<Range> m_range;
     93 };
     94 
     95 } // namespace WebCore
     96 
     97 #endif // TextCheckingHelper_h
     98