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