#ifndef TextIterator_h
#define TextIterator_h
#include "DeprecatedString.h"
#include "InlineTextBox.h"
#include "Range.h"
#include <wtf/Vector.h>
namespace WebCore {
inline bool isCollapsibleWhitespace(UChar c)
{
switch (c) {
case ' ':
case '\n':
return true;
default:
return false;
}
}
DeprecatedString plainText(const Range*);
PassRefPtr<Range> findPlainText(const Range*, const String&, bool forward, bool caseSensitive);
enum IteratorKind { CONTENT = 0, RUNFINDER = 1 };
class TextIterator
{
public:
TextIterator();
explicit TextIterator(const Range *, IteratorKind kind = CONTENT );
bool atEnd() const { return !m_positionNode; }
void advance();
int length() const { return m_textLength; }
const UChar* characters() const { return m_textCharacters; }
PassRefPtr<Range> range() const;
static int rangeLength(const Range *r);
static PassRefPtr<Range> rangeFromLocationAndLength(Document *doc, int rangeLocation, int rangeLength);
private:
void exitNode();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void handleTextBox();
void emitCharacter(UChar, Node *textNode, Node *offsetBaseNode, int textStartOffset, int textEndOffset);
Node *m_node;
int m_offset;
bool m_handledNode;
bool m_handledChildren;
Node *m_endContainer;
int m_endOffset;
Node *m_pastEndNode;
Node *m_positionNode;
mutable Node *m_positionOffsetBaseNode;
mutable int m_positionStartOffset;
mutable int m_positionEndOffset;
const UChar* m_textCharacters;
int m_textLength;
bool m_needAnotherNewline;
InlineTextBox *m_textBox;
Node *m_lastTextNode;
bool m_lastTextNodeEndedWithCollapsedSpace;
UChar m_lastCharacter;
UChar m_singleCharacterBuffer;
Vector<InlineTextBox*> m_sortedTextBoxes;
size_t m_sortedTextBoxesPosition;
};
class SimplifiedBackwardsTextIterator
{
public:
SimplifiedBackwardsTextIterator();
explicit SimplifiedBackwardsTextIterator(const Range *);
bool atEnd() const { return !m_positionNode; }
void advance();
int length() const { return m_textLength; }
const UChar* characters() const { return m_textCharacters; }
PassRefPtr<Range> range() const;
private:
void exitNode();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void emitCharacter(UChar, Node *Node, int startOffset, int endOffset);
void emitNewline();
Node* m_node;
int m_offset;
bool m_handledNode;
bool m_handledChildren;
Node* m_startNode;
int m_startOffset;
Node* m_positionNode;
int m_positionStartOffset;
int m_positionEndOffset;
const UChar* m_textCharacters;
int m_textLength;
Node* m_lastTextNode;
UChar m_lastCharacter;
UChar m_singleCharacterBuffer;
};
class CharacterIterator {
public:
CharacterIterator();
explicit CharacterIterator(const Range *r);
void advance(int numCharacters);
bool atBreak() const { return m_atBreak; }
bool atEnd() const { return m_textIterator.atEnd(); }
int length() const { return m_textIterator.length() - m_runOffset; }
const UChar* characters() const { return m_textIterator.characters() + m_runOffset; }
DeprecatedString string(int numChars);
int characterOffset() const { return m_offset; }
PassRefPtr<Range> range() const;
private:
int m_offset;
int m_runOffset;
bool m_atBreak;
TextIterator m_textIterator;
};
class WordAwareIterator {
public:
WordAwareIterator();
explicit WordAwareIterator(const Range *r);
bool atEnd() const { return !m_didLookAhead && m_textIterator.atEnd(); }
void advance();
int length() const;
const UChar* characters() const;
PassRefPtr<Range> range() const { return m_range; }
private:
const UChar* m_previousText;
int m_previousLength;
DeprecatedString m_buffer;
bool m_didLookAhead;
RefPtr<Range> m_range;
TextIterator m_textIterator;
};
}
#endif