#ifndef TextIterator_h
#define TextIterator_h
#include "FindOptions.h"
#include "Range.h"
#include <wtf/Vector.h>
namespace WebCore {
class InlineTextBox;
class RenderText;
class RenderTextFragment;
enum TextIteratorBehavior {
TextIteratorDefaultBehavior = 0,
TextIteratorEmitsCharactersBetweenAllVisiblePositions = 1 << 0,
TextIteratorEntersTextControls = 1 << 1,
TextIteratorEmitsTextsWithoutTranscoding = 1 << 2,
TextIteratorIgnoresStyleVisibility = 1 << 3,
TextIteratorEmitsObjectReplacementCharacters = 1 << 4
};
inline bool isCollapsibleWhitespace(UChar c)
{
switch (c) {
case ' ':
case '\n':
return true;
default:
return false;
}
}
String plainText(const Range*, TextIteratorBehavior defaultBehavior = TextIteratorDefaultBehavior);
UChar* plainTextToMallocAllocatedBuffer(const Range*, unsigned& bufferLength, bool isDisplayString, TextIteratorBehavior = TextIteratorDefaultBehavior);
PassRefPtr<Range> findPlainText(const Range*, const String&, FindOptions);
class BitStack {
public:
BitStack();
~BitStack();
void push(bool);
void pop();
bool top() const;
unsigned size() const;
private:
unsigned m_size;
Vector<unsigned, 1> m_words;
};
class TextIterator {
public:
TextIterator();
~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 bool locationAndLengthFromRange(const Range*, size_t& location, size_t& length);
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 handleTextNodeFirstLetter(RenderTextFragment*);
bool hasVisibleTextNode(RenderText*);
void emitCharacter(UChar, Node* textNode, Node* offsetBaseNode, int textStartOffset, int textEndOffset);
void emitText(Node* textNode, RenderObject* renderObject, 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;
InlineTextBox* m_remainingTextBox;
RenderText *m_firstLetterText;
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;
bool m_handledFirstLetter;
bool m_ignoresStyleVisibility;
bool m_emitsObjectReplacementCharacters;
};
class SimplifiedBackwardsTextIterator {
public:
SimplifiedBackwardsTextIterator();
explicit SimplifiedBackwardsTextIterator(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;
private:
void exitNode();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void emitCharacter(UChar, Node*, int startOffset, int endOffset);
bool advanceRespectingRange(Node*);
TextIteratorBehavior m_behavior;
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;
bool m_havePassedStartNode;
};
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*, TextIteratorBehavior = TextIteratorDefaultBehavior);
void advance(int);
bool atEnd() const { return m_textIterator.atEnd(); }
PassRefPtr<Range> range() const;
private:
TextIteratorBehavior m_behavior;
int m_offset;
int m_runOffset;
bool m_atBreak;
SimplifiedBackwardsTextIterator m_textIterator;
};
class WordAwareIterator {
public:
WordAwareIterator();
explicit WordAwareIterator(const Range*);
~WordAwareIterator();
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