#ifndef TextIterator_h
#define TextIterator_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;
}
}
String plainText(const Range*);
UChar* plainTextToMallocAllocatedBuffer(const Range*, unsigned& bufferLength, bool isDisplayString);
PassRefPtr<Range> findPlainText(const Range*, const String&, bool forward, bool caseSensitive);
class BitStack {
public:
BitStack();
void push(bool);
void pop();
bool top() const;
unsigned size() const;
private:
unsigned m_size;
Vector<unsigned, 1> m_words;
};
enum TextIteratorBehavior {
TextIteratorDefaultBehavior = 0,
TextIteratorEmitsCharactersBetweenAllVisiblePositions = 1 << 0,
TextIteratorEntersTextControls = 1 << 1,
TextIteratorEmitsTextsWithoutTranscoding = 1 << 2,
};
class TextIterator {
public:
TextIterator();
explicit TextIterator(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior);
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;
Node* node() const;
static int rangeLength(const Range*, bool spacesForReplacedElements = false);
static PassRefPtr<Range> rangeFromLocationAndLength(Element* scope, int rangeLocation, int rangeLength, bool spacesForReplacedElements = false);
static PassRefPtr<Range> subrange(Range* entireRange, int characterOffset, int characterCount);
private:
void exitNode();
bool shouldRepresentNodeOffsetZero();
bool shouldEmitSpaceBeforeAndAfterNode(Node*);
void representNodeOffsetZero();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void handleTextBox();
void emitCharacter(UChar, Node* textNode, Node* offsetBaseNode, int textStartOffset, int textEndOffset);
void emitText(Node* textNode, int textStartOffset, int textEndOffset);
Node* m_node;
int m_offset;
bool m_handledNode;
bool m_handledChildren;
BitStack m_fullyClippedStack;
Node* m_startContainer;
int m_startOffset;
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;
String m_text;
bool m_needsAnotherNewline;
InlineTextBox* m_textBox;
Node* m_lastTextNode;
bool m_lastTextNodeEndedWithCollapsedSpace;
UChar m_lastCharacter;
UChar m_singleCharacterBuffer;
Vector<InlineTextBox*> m_sortedTextBoxes;
size_t m_sortedTextBoxesPosition;
bool m_hasEmitted;
bool m_emitsCharactersBetweenAllVisiblePositions;
bool m_entersTextControls;
bool m_emitsTextWithoutTranscoding;
};
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*, int startOffset, int endOffset);
Node* m_node;
int m_offset;
bool m_handledNode;
bool m_handledChildren;
BitStack m_fullyClippedStack;
Node* m_startNode;
int m_startOffset;
Node* m_endNode;
int m_endOffset;
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;
Node* m_pastStartNode;
};
class CharacterIterator {
public:
CharacterIterator();
explicit CharacterIterator(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior);
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; }
String 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 BackwardsCharacterIterator {
public:
BackwardsCharacterIterator();
explicit BackwardsCharacterIterator(const Range*);
void advance(int);
bool atEnd() const { return m_textIterator.atEnd(); }
PassRefPtr<Range> range() const;
private:
int m_offset;
int m_runOffset;
bool m_atBreak;
SimplifiedBackwardsTextIterator m_textIterator;
};
class WordAwareIterator {
public:
WordAwareIterator();
explicit WordAwareIterator(const Range*);
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;
Vector<UChar> m_buffer;
bool m_didLookAhead;
RefPtr<Range> m_range;
TextIterator m_textIterator;
};
}
#endif