#ifndef TextIterator_h
#define TextIterator_h
#include "FindOptions.h"
#include "Range.h"
#include "TextIteratorBehavior.h"
#include <wtf/Vector.h>
#include <wtf/text/StringView.h>
namespace WebCore {
class InlineTextBox;
class RenderText;
class RenderTextFragment;
String plainText(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
String plainTextReplacingNoBreakSpace(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
PassRefPtr<Range> findPlainText(const Range&, const String&, FindOptions);
bool isRendererReplacedElement(RenderObject*);
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 TextIteratorCopyableText {
public:
TextIteratorCopyableText()
: m_singleCharacter(0)
, m_offset(0)
, m_length(0)
{
}
StringView text() const { return m_singleCharacter ? StringView(&m_singleCharacter, 1) : StringView(m_string).substring(m_offset, m_length); }
void appendToStringBuilder(StringBuilder&) const;
void reset();
void set(String&&);
void set(String&&, unsigned offset, unsigned length);
void set(UChar);
private:
UChar m_singleCharacter;
String m_string;
unsigned m_offset;
unsigned m_length;
};
class TextIterator {
public:
explicit TextIterator(const Range*, TextIteratorBehavior = TextIteratorDefaultBehavior);
~TextIterator();
bool atEnd() const { return !m_positionNode; }
void advance();
StringView text() const { ASSERT(!atEnd()); return m_text; }
PassRefPtr<Range> range() const;
Node* node() const;
const TextIteratorCopyableText& copyableText() const { ASSERT(!atEnd()); return m_copyableText; }
void appendTextToStringBuilder(StringBuilder& builder) const { copyableText().appendToStringBuilder(builder); }
static int rangeLength(const Range*, bool spacesForReplacedElements = false);
static PassRefPtr<Range> rangeFromLocationAndLength(ContainerNode* scope, int rangeLocation, int rangeLength, bool spacesForReplacedElements = false);
static bool getLocationAndLengthFromRange(Node* scope, 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&);
void emitCharacter(UChar, Node& characterNode, Node* offsetBaseNode, int textStartOffset, int textEndOffset);
void emitText(Text& textNode, RenderText&, int textStartOffset, int textEndOffset);
const TextIteratorBehavior m_behavior;
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;
TextIteratorCopyableText m_copyableText;
StringView m_text;
bool m_needsAnotherNewline;
InlineTextBox* m_textBox;
InlineTextBox* m_remainingTextBox;
RenderText* m_firstLetterText;
Text* m_lastTextNode;
bool m_lastTextNodeEndedWithCollapsedSpace;
UChar m_lastCharacter;
Vector<InlineTextBox*> m_sortedTextBoxes;
size_t m_sortedTextBoxesPosition;
bool m_hasEmitted;
bool m_handledFirstLetter;
};
class SimplifiedBackwardsTextIterator {
public:
explicit SimplifiedBackwardsTextIterator(const Range&, TextIteratorBehavior = TextIteratorDefaultBehavior);
bool atEnd() const { return !m_positionNode; }
void advance();
StringView text() const { ASSERT(!atEnd()); return m_text; }
PassRefPtr<Range> range() const;
Node* node() const { ASSERT(!atEnd()); return m_node; }
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*);
const TextIteratorBehavior m_behavior;
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_positionNode;
int m_positionStartOffset;
int m_positionEndOffset;
TextIteratorCopyableText m_copyableText;
StringView m_text;
Text* m_lastTextNode;
UChar m_lastCharacter;
bool m_havePassedStartContainer;
bool m_shouldHandleFirstLetter;
};
class CharacterIterator {
public:
explicit CharacterIterator(const Range&, TextIteratorBehavior = TextIteratorDefaultBehavior);
bool atEnd() const { return m_underlyingIterator.atEnd(); }
void advance(int numCharacters);
StringView text() const { return m_underlyingIterator.text().substring(m_runOffset); }
PassRefPtr<Range> range() const;
bool atBreak() const { return m_atBreak; }
int characterOffset() const { return m_offset; }
private:
TextIterator m_underlyingIterator;
int m_offset;
int m_runOffset;
bool m_atBreak;
};
class BackwardsCharacterIterator {
public:
explicit BackwardsCharacterIterator(const Range&);
bool atEnd() const { return m_underlyingIterator.atEnd(); }
void advance(int numCharacters);
PassRefPtr<Range> range() const;
private:
SimplifiedBackwardsTextIterator m_underlyingIterator;
int m_offset;
int m_runOffset;
bool m_atBreak;
};
class WordAwareIterator {
public:
explicit WordAwareIterator(const Range&);
bool atEnd() const { return !m_didLookAhead && m_underlyingIterator.atEnd(); }
void advance();
StringView text() const;
private:
TextIterator m_underlyingIterator;
TextIteratorCopyableText m_previousText;
Vector<UChar> m_buffer;
bool m_didLookAhead;
};
}
#endif