#ifndef TextIterator_h
#define TextIterator_h
#include "core/dom/Range.h"
#include "core/editing/FindOptions.h"
#include "platform/heap/Handle.h"
#include "wtf/Vector.h"
namespace WebCore {
class InlineTextBox;
class RenderText;
class RenderTextFragment;
enum TextIteratorBehavior {
TextIteratorDefaultBehavior = 0,
TextIteratorEmitsCharactersBetweenAllVisiblePositions = 1 << 0,
TextIteratorEntersTextControls = 1 << 1,
TextIteratorIgnoresStyleVisibility = 1 << 2,
TextIteratorEmitsOriginalText = 1 << 3,
TextIteratorStopsOnFormControls = 1 << 4,
TextIteratorEmitsImageAltText = 1 << 5,
TextIteratorEntersAuthorShadowRoots = 1 << 6
};
typedef unsigned TextIteratorBehaviorFlags;
String plainText(const Range*, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
PassRefPtrWillBeRawPtr<Range> findPlainText(const Range*, const String&, FindOptions);
PassRefPtrWillBeRawPtr<Range> findPlainText(const Position& start, const Position& end, 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:
explicit TextIterator(const Range*, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
TextIterator(const Position& start, const Position& end, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
~TextIterator();
bool atEnd() const { return !m_positionNode || m_shouldStop; }
void advance();
int length() const { return m_textLength; }
UChar characterAt(unsigned index) const;
String substring(unsigned position, unsigned length) const;
void appendTextToStringBuilder(StringBuilder&, unsigned position = 0, unsigned maxLength = UINT_MAX) const;
template<typename BufferType>
void appendTextTo(BufferType& output, unsigned position = 0)
{
ASSERT_WITH_SECURITY_IMPLICATION(position <= static_cast<unsigned>(length()));
unsigned lengthToAppend = length() - position;
if (!lengthToAppend)
return;
if (m_singleCharacterBuffer) {
ASSERT(!position);
ASSERT(length() == 1);
output.append(&m_singleCharacterBuffer, 1);
} else {
string().appendTo(output, startOffset() + position, lengthToAppend);
}
}
PassRefPtrWillBeRawPtr<Range> range() const;
Node* node() const;
static int rangeLength(const Range*, bool spacesForReplacedElements = false);
static PassRefPtrWillBeRawPtr<Range> subrange(Range* entireRange, int characterOffset, int characterCount);
private:
enum IterationProgress {
HandledNone,
HandledAuthorShadowRoots,
HandledUserAgentShadowRoot,
HandledNode,
HandledChildren
};
void initialize(const Position& start, const Position& end);
int startOffset() const { return m_positionStartOffset; }
const String& string() const { return m_text; }
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;
IterationProgress m_iterationProgress;
BitStack m_fullyClippedStack;
int m_shadowDepth;
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;
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_emitsOriginalText;
bool m_handledFirstLetter;
bool m_ignoresStyleVisibility;
bool m_stopsOnFormControls;
bool m_shouldStop;
bool m_emitsImageAltText;
bool m_entersAuthorShadowRoots;
};
class SimplifiedBackwardsTextIterator {
public:
explicit SimplifiedBackwardsTextIterator(const Range*, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
bool atEnd() const { return !m_positionNode || m_shouldStop; }
void advance();
int length() const { return m_textLength; }
Node* node() const { return m_node; }
template<typename BufferType>
void prependTextTo(BufferType& output)
{
if (!m_textLength)
return;
if (m_singleCharacterBuffer)
output.prepend(&m_singleCharacterBuffer, 1);
else
m_textContainer.prependTo(output, m_textOffset, m_textLength);
}
PassRefPtrWillBeRawPtr<Range> range() const;
private:
void exitNode();
bool handleTextNode();
RenderText* handleFirstLetter(int& startOffset, int& offsetInNode);
bool handleReplacedElement();
bool handleNonTextNode();
void emitCharacter(UChar, Node*, int startOffset, int endOffset);
bool advanceRespectingRange(Node*);
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;
String m_textContainer;
int m_textOffset;
int m_textLength;
Node* m_lastTextNode;
UChar m_lastCharacter;
UChar m_singleCharacterBuffer;
bool m_havePassedStartNode;
bool m_shouldHandleFirstLetter;
bool m_stopsOnFormControls;
bool m_shouldStop;
bool m_emitsOriginalText;
};
class CharacterIterator {
public:
explicit CharacterIterator(const Range*, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
CharacterIterator(const Position& start, const Position& end, TextIteratorBehaviorFlags = 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; }
UChar characterAt(unsigned index) const { return m_textIterator.characterAt(m_runOffset + index); }
template<typename BufferType>
void appendTextTo(BufferType& output) { m_textIterator.appendTextTo(output, m_runOffset); }
int characterOffset() const { return m_offset; }
PassRefPtrWillBeRawPtr<Range> range() const;
private:
void initialize();
int m_offset;
int m_runOffset;
bool m_atBreak;
TextIterator m_textIterator;
};
class BackwardsCharacterIterator {
public:
explicit BackwardsCharacterIterator(const Range*, TextIteratorBehaviorFlags = TextIteratorDefaultBehavior);
void advance(int);
bool atEnd() const { return m_textIterator.atEnd(); }
PassRefPtrWillBeRawPtr<Range> range() const;
private:
int m_offset;
int m_runOffset;
bool m_atBreak;
SimplifiedBackwardsTextIterator m_textIterator;
};
class WordAwareIterator {
STACK_ALLOCATED();
public:
explicit WordAwareIterator(const Range*);
~WordAwareIterator();
bool atEnd() const { return !m_didLookAhead && m_textIterator.atEnd(); }
void advance();
String substring(unsigned position, unsigned length) const;
UChar characterAt(unsigned index) const;
int length() const;
private:
Vector<UChar> m_buffer;
bool m_didLookAhead;
RefPtrWillBeMember<Range> m_range;
TextIterator m_textIterator;
};
}
#endif