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