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 "core/page/EditorClient.h"
     25 #include "core/platform/text/TextChecking.h"
     26 #include "wtf/text/WTFString.h"
     27 
     28 namespace WebCore {
     29 
     30 class ExceptionState;
     31 class Range;
     32 class Position;
     33 struct TextCheckingResult;
     34 
     35 class TextCheckingParagraph {
     36 public:
     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&, ExceptionState&) const;
     44     void expandRangeToNextEnd();
     45 
     46     const String& text() const;
     47     // Why not let clients call these functions on text() themselves?
     48     String textSubstring(unsigned pos, unsigned len = INT_MAX) const { return text().substring(pos, len); }
     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 checkingRangeCovers(int location, int length) const { return location < checkingEnd() && location + length > checkingStart(); }
     61     PassRefPtr<Range> paragraphRange() const;
     62 
     63 private:
     64     void invalidateParagraphRangeValues();
     65     PassRefPtr<Range> checkingRange() const { return m_checkingRange; }
     66     PassRefPtr<Range> offsetAsRange() const;
     67 
     68     RefPtr<Range> m_checkingRange;
     69     mutable RefPtr<Range> m_paragraphRange;
     70     mutable RefPtr<Range> m_offsetAsRange;
     71     mutable String m_text;
     72     mutable int m_checkingStart;
     73     mutable int m_checkingEnd;
     74     mutable int m_checkingLength;
     75 };
     76 
     77 class TextCheckingHelper {
     78     WTF_MAKE_NONCOPYABLE(TextCheckingHelper);
     79 public:
     80     TextCheckingHelper(EditorClient*, PassRefPtr<Range>);
     81     ~TextCheckingHelper();
     82 
     83     String findFirstMisspelling(int& firstMisspellingOffset, bool markAll, RefPtr<Range>& firstMisspellingRange);
     84     String findFirstMisspellingOrBadGrammar(bool checkGrammar, bool& outIsSpelling, int& outFirstFoundOffset, GrammarDetail& outGrammarDetail);
     85     String findFirstBadGrammar(GrammarDetail& outGrammarDetail, int& outGrammarPhraseOffset, bool markAll);
     86     void markAllMisspellings(RefPtr<Range>& firstMisspellingRange);
     87     void markAllBadGrammar();
     88 
     89 private:
     90     EditorClient* m_client;
     91     RefPtr<Range> m_range;
     92 
     93     int findFirstGrammarDetail(const Vector<GrammarDetail>& grammarDetails, int badGrammarPhraseLocation, int startOffset, int endOffset, bool markAll) const;
     94     bool unifiedTextCheckerEnabled() const;
     95 };
     96 
     97 void checkTextOfParagraph(TextCheckerClient*, const String&, TextCheckingTypeMask, Vector<TextCheckingResult>&);
     98 
     99 bool unifiedTextCheckerEnabled(const Frame*);
    100 
    101 } // namespace WebCore
    102 
    103 #endif // TextCheckingHelper_h
    104