#ifndef KHTML_EDITING_VISIBLE_TEXT_H
#define KHTML_EDITING_VISIBLE_TEXT_H
#include "dom/dom2_range.h"
#include "render_text.h"
#include "../kwq/KWQSortedList.h"
namespace khtml {
class InlineTextBox;
inline bool isCollapsibleWhitespace(const QChar &c)
{
switch (c.unicode()) {
case ' ':
case '\n':
return true;
default:
return false;
}
}
QString plainText(const DOM::Range &);
DOM::Range findPlainText(const DOM::Range &, const QString &, bool forward, bool caseSensitive);
enum IteratorKind { CONTENT = 0, RUNFINDER = 1 };
class TextIterator
{
public:
TextIterator();
explicit TextIterator(const DOM::Range &, IteratorKind kind = CONTENT );
bool atEnd() const { return !m_positionNode; }
void advance();
long length() const { return m_textLength; }
const QChar *characters() const { return m_textCharacters; }
DOM::Range range() const;
static long TextIterator::rangeLength(const DOM::Range &r);
static void TextIterator::setRangeFromLocationAndLength (const DOM::Range &range, DOM::Range &resultRange, long rangeLocation, long rangeLength);
private:
void exitNode();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void handleTextBox();
void emitCharacter(QChar, DOM::NodeImpl *textNode, DOM::NodeImpl *offsetBaseNode, long textStartOffset, long textEndOffset);
DOM::NodeImpl *m_node;
long m_offset;
bool m_handledNode;
bool m_handledChildren;
DOM::NodeImpl *m_endContainer;
long m_endOffset;
DOM::NodeImpl *m_pastEndNode;
DOM::NodeImpl *m_positionNode;
mutable DOM::NodeImpl *m_positionOffsetBaseNode;
mutable long m_positionStartOffset;
mutable long m_positionEndOffset;
const QChar *m_textCharacters;
long m_textLength;
bool m_needAnotherNewline;
InlineTextBox *m_textBox;
DOM::NodeImpl *m_lastTextNode;
bool m_lastTextNodeEndedWithCollapsedSpace;
QChar m_lastCharacter;
QChar m_singleCharacterBuffer;
QSortedList<InlineTextBox> m_sortedTextBoxes;
};
class SimplifiedBackwardsTextIterator
{
public:
SimplifiedBackwardsTextIterator();
explicit SimplifiedBackwardsTextIterator(const DOM::Range &);
bool atEnd() const { return !m_positionNode; }
void advance();
long length() const { return m_textLength; }
const QChar *characters() const { return m_textCharacters; }
DOM::Range range() const;
private:
void exitNode();
bool handleTextNode();
bool handleReplacedElement();
bool handleNonTextNode();
void emitCharacter(QChar, DOM::NodeImpl *Node, long startOffset, long endOffset);
void emitNewlineForBROrText();
DOM::NodeImpl *m_node;
long m_offset;
bool m_handledNode;
bool m_handledChildren;
DOM::NodeImpl *m_startNode;
long m_startOffset;
DOM::NodeImpl *m_positionNode;
long m_positionStartOffset;
long m_positionEndOffset;
const QChar *m_textCharacters;
long m_textLength;
DOM::NodeImpl *m_lastTextNode;
QChar m_lastCharacter;
QChar m_singleCharacterBuffer;
};
class CharacterIterator {
public:
CharacterIterator();
explicit CharacterIterator(const DOM::Range &r);
void advance(long numCharacters);
bool atBreak() const { return m_atBreak; }
bool atEnd() const { return m_textIterator.atEnd(); }
long length() const { return m_textIterator.length() - m_runOffset; }
const QChar *characters() const { return m_textIterator.characters() + m_runOffset; }
QString string(long numChars);
long characterOffset() const { return m_offset; }
DOM::Range range() const;
private:
long m_offset;
long m_runOffset;
bool m_atBreak;
TextIterator m_textIterator;
};
class WordAwareIterator {
public:
WordAwareIterator();
explicit WordAwareIterator(const DOM::Range &r);
bool atEnd() const { return !m_didLookAhead && m_textIterator.atEnd(); }
void advance();
long length() const;
const QChar *characters() const;
DOM::Range range() const { return m_range; }
private:
const QChar *m_previousText;
long m_previousLength;
QString m_buffer;
bool m_didLookAhead;
DOM::Range m_range;
TextIterator m_textIterator;
};
}
#endif