#pragma once
#include "CharacterRange.h"
#include "FindOptions.h"
#include "LayoutIntegrationRunIterator.h"
#include "SimpleRange.h"
#include "TextIteratorBehavior.h"
#include <wtf/Vector.h>
namespace WebCore {
class RenderTextFragment;
WEBCORE_EXPORT uint64_t characterCount(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
CharacterRange characterRange(const BoundaryPoint& start, const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
CharacterRange characterRange(const SimpleRange& scope, const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
BoundaryPoint resolveCharacterLocation(const SimpleRange& scope, uint64_t, TextIteratorBehavior = TextIteratorDefaultBehavior);
WEBCORE_EXPORT SimpleRange resolveCharacterRange(const SimpleRange& scope, CharacterRange, TextIteratorBehavior = TextIteratorDefaultBehavior);
WEBCORE_EXPORT String plainText(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
WEBCORE_EXPORT bool hasAnyPlainText(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
WEBCORE_EXPORT String plainTextReplacingNoBreakSpace(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior, bool isDisplayString = false);
SimpleRange findPlainText(const SimpleRange&, const String&, FindOptions);
WEBCORE_EXPORT SimpleRange findClosestPlainText(const SimpleRange&, const String&, FindOptions, uint64_t targetCharacterOffset);
bool containsPlainText(const String& document, const String&, FindOptions); WEBCORE_EXPORT String foldQuoteMarks(const String&);
bool isRendererReplacedElement(RenderObject*);
class BitStack {
public:
void push(bool);
void pop();
bool top() const;
private:
unsigned m_size { 0 };
Vector<unsigned, 1> m_words;
};
class TextIteratorCopyableText {
public:
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 { 0 };
String m_string;
unsigned m_offset { 0 };
unsigned m_length { 0 };
};
class TextIterator {
WTF_MAKE_FAST_ALLOCATED;
public:
WEBCORE_EXPORT explicit TextIterator(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
WEBCORE_EXPORT ~TextIterator();
bool atEnd() const { return !m_positionNode; }
WEBCORE_EXPORT void advance();
StringView text() const { ASSERT(!atEnd()); return m_text; }
WEBCORE_EXPORT SimpleRange range() const;
WEBCORE_EXPORT Node* node() const;
const TextIteratorCopyableText& copyableText() const { ASSERT(!atEnd()); return m_copyableText; }
void appendTextToStringBuilder(StringBuilder& builder) const { copyableText().appendToStringBuilder(builder); }
private:
void init();
void exitNode(Node*);
bool shouldRepresentNodeOffsetZero();
bool shouldEmitSpaceBeforeAndAfterNode(Node&);
void representNodeOffsetZero();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void handleTextRun();
void handleTextNodeFirstLetter(RenderTextFragment&);
void emitCharacter(UChar, Node& characterNode, Node* offsetBaseNode, int textStartOffset, int textEndOffset);
void emitText(Text& textNode, RenderText&, int textStartOffset, int textEndOffset);
Node* baseNodeForEmittingNewLine() const;
const TextIteratorBehavior m_behavior { TextIteratorDefaultBehavior };
Node* m_node { nullptr };
int m_offset { 0 };
bool m_handledNode { false };
bool m_handledChildren { false };
BitStack m_fullyClippedStack;
Node* m_startContainer { nullptr };
int m_startOffset { 0 };
Node* m_endContainer { nullptr };
int m_endOffset { 0 };
Node* m_pastEndNode { nullptr };
Node* m_positionNode { nullptr };
mutable Node* m_positionOffsetBaseNode { nullptr };
mutable int m_positionStartOffset { 0 };
mutable int m_positionEndOffset { 0 };
TextIteratorCopyableText m_copyableText;
StringView m_text;
Node* m_nodeForAdditionalNewline { nullptr };
LayoutIntegration::TextRunIterator m_textRun;
LayoutIntegration::TextRunIterator m_remainingTextRun;
RenderText* m_firstLetterText { nullptr };
Text* m_lastTextNode { nullptr };
bool m_lastTextNodeEndedWithCollapsedSpace { false };
UChar m_lastCharacter { 0 };
bool m_hasEmitted { false };
bool m_handledFirstLetter { false };
};
class SimplifiedBackwardsTextIterator {
public:
WEBCORE_EXPORT explicit SimplifiedBackwardsTextIterator(const SimpleRange&);
bool atEnd() const { return !m_positionNode; }
WEBCORE_EXPORT void advance();
StringView text() const { ASSERT(!atEnd()); return m_text; }
WEBCORE_EXPORT SimpleRange 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 { TextIteratorDefaultBehavior };
Node* m_node { nullptr };
int m_offset { 0 };
bool m_handledNode { false };
bool m_handledChildren { false };
BitStack m_fullyClippedStack;
Node* m_startContainer { nullptr };
int m_startOffset { 0 };
Node* m_endContainer { nullptr };
int m_endOffset { 0 };
Node* m_positionNode { nullptr };
int m_positionStartOffset { 0 };
int m_positionEndOffset { 0 };
TextIteratorCopyableText m_copyableText;
StringView m_text;
Text* m_lastTextNode { nullptr };
UChar m_lastCharacter { 0 };
bool m_havePassedStartContainer { false };
bool m_shouldHandleFirstLetter { false };
};
class CharacterIterator {
public:
WEBCORE_EXPORT explicit CharacterIterator(const SimpleRange&, TextIteratorBehavior = TextIteratorDefaultBehavior);
bool atEnd() const { return m_underlyingIterator.atEnd(); }
WEBCORE_EXPORT void advance(int numCharacters);
StringView text() const { return m_underlyingIterator.text().substring(m_runOffset); }
WEBCORE_EXPORT SimpleRange range() const;
bool atBreak() const { return m_atBreak; }
unsigned characterOffset() const { return m_offset; }
private:
TextIterator m_underlyingIterator;
unsigned m_offset { 0 };
unsigned m_runOffset { 0 };
bool m_atBreak { true };
};
class BackwardsCharacterIterator {
public:
explicit BackwardsCharacterIterator(const SimpleRange&);
bool atEnd() const { return m_underlyingIterator.atEnd(); }
void advance(int numCharacters);
SimpleRange range() const;
private:
SimplifiedBackwardsTextIterator m_underlyingIterator;
unsigned m_offset { 0 };
unsigned m_runOffset { 0 };
bool m_atBreak { true };
};
class WordAwareIterator {
public:
explicit WordAwareIterator(const SimpleRange&);
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 { true };
};
inline CharacterRange characterRange(const BoundaryPoint& start, const SimpleRange& range, TextIteratorBehavior behavior)
{
return { characterCount({ start, range.start }, behavior), characterCount(range, behavior) };
}
inline CharacterRange characterRange(const SimpleRange& scope, const SimpleRange& range, TextIteratorBehavior behavior)
{
return characterRange(scope.start, range, behavior);
}
inline BoundaryPoint resolveCharacterLocation(const SimpleRange& scope, uint64_t location, TextIteratorBehavior behavior)
{
return resolveCharacterRange(scope, { location, 0 }, behavior).start;
}
}