#ifndef Position_h
#define Position_h
#include "ContainerNode.h"
#include "EditingBoundary.h"
#include "TextAffinity.h"
#include "TextDirection.h"
#include <wtf/Assertions.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class CSSComputedStyleDeclaration;
class Element;
class InlineBox;
class Node;
class Range;
class RenderElement;
class RenderObject;
class Text;
enum PositionMoveType {
CodePoint, Character, BackwardDeletion };
class Position {
public:
enum AnchorType {
PositionIsOffsetInAnchor,
PositionIsBeforeAnchor,
PositionIsAfterAnchor,
PositionIsBeforeChildren,
PositionIsAfterChildren,
};
Position()
: m_offset(0)
, m_anchorType(PositionIsOffsetInAnchor)
, m_isLegacyEditingPosition(false)
{
}
class LegacyEditingOffset {
public:
int value() const { return m_offset; }
private:
explicit LegacyEditingOffset(int offset) : m_offset(offset) { }
friend Position createLegacyEditingPosition(PassRefPtr<Node>, int offset);
int m_offset;
};
Position(PassRefPtr<Node> anchorNode, LegacyEditingOffset);
Position(PassRefPtr<Node> anchorNode, AnchorType);
Position(PassRefPtr<Text> textNode, unsigned offset);
Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
AnchorType anchorType() const { return static_cast<AnchorType>(m_anchorType); }
void clear() { m_anchorNode.clear(); m_offset = 0; m_anchorType = PositionIsOffsetInAnchor; m_isLegacyEditingPosition = false; }
Node* containerNode() const; Text* containerText() const;
int computeOffsetInContainerNode() const; Position parentAnchoredEquivalent() const;
int offsetInContainerNode() const
{
ASSERT(anchorType() == PositionIsOffsetInAnchor);
return m_offset;
}
int deprecatedEditingOffset() const
{
if (m_isLegacyEditingPosition || (m_anchorType != PositionIsAfterAnchor && m_anchorType != PositionIsAfterChildren))
return m_offset;
return offsetForPositionAfterAnchor();
}
Node* computeNodeBeforePosition() const;
Node* computeNodeAfterPosition() const;
Node* anchorNode() const { return m_anchorNode.get(); }
Node* deprecatedNode() const { return m_anchorNode.get(); }
Document* document() const { return m_anchorNode ? &m_anchorNode->document() : 0; }
Element* rootEditableElement() const
{
Node* container = containerNode();
return container ? container->rootEditableElement() : 0;
}
void moveToPosition(PassRefPtr<Node> anchorNode, int offset);
void moveToOffset(int offset);
bool isNull() const { return !m_anchorNode; }
bool isNotNull() const { return m_anchorNode; }
bool isOrphan() const { return m_anchorNode && !m_anchorNode->inDocument(); }
Element* element() const;
Position previous(PositionMoveType = CodePoint) const;
Position next(PositionMoveType = CodePoint) const;
static int uncheckedPreviousOffset(const Node*, int current);
static int uncheckedPreviousOffsetForBackwardDeletion(const Node*, int current);
static int uncheckedNextOffset(const Node*, int current);
bool atFirstEditingPositionForNode() const;
bool atLastEditingPositionForNode() const;
bool atEditingBoundary() const;
Node* parentEditingBoundary() const;
bool atStartOfTree() const;
bool atEndOfTree() const;
Position leadingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
Position trailingWhitespacePosition(EAffinity, bool considerNonCollapsibleWhitespace = false) const;
Position upstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
Position downstream(EditingBoundaryCrossingRule = CannotCrossEditingBoundary) const;
bool isCandidate() const;
bool isRenderedCharacter() const;
bool rendersInDifferentPosition(const Position&) const;
void getInlineBoxAndOffset(EAffinity, InlineBox*&, int& caretOffset) const;
void getInlineBoxAndOffset(EAffinity, TextDirection primaryDirection, InlineBox*&, int& caretOffset) const;
TextDirection primaryDirection() const;
static unsigned positionCountBetweenPositions(const Position&, const Position&);
static bool hasRenderedNonAnonymousDescendantsWithHeight(const RenderElement&);
static bool nodeIsUserSelectNone(Node*);
#if ENABLE(USERSELECT_ALL)
static bool nodeIsUserSelectAll(const Node*);
static Node* rootUserSelectAllForNode(Node*);
#else
static bool nodeIsUserSelectAll(const Node*) { return false; }
static Node* rootUserSelectAllForNode(Node*) { return 0; }
#endif
static ContainerNode* findParent(const Node*);
void debugPosition(const char* msg = "") const;
#ifndef NDEBUG
void formatForDebugger(char* buffer, unsigned length) const;
void showAnchorTypeAndOffset() const;
void showTreeForThis() const;
#endif
private:
int offsetForPositionAfterAnchor() const;
Position previousCharacterPosition(EAffinity) const;
Position nextCharacterPosition(EAffinity) const;
static AnchorType anchorTypeForLegacyEditingPosition(Node* anchorNode, int offset);
RefPtr<Node> m_anchorNode;
int m_offset;
unsigned m_anchorType : 3;
bool m_isLegacyEditingPosition : 1;
};
inline Position createLegacyEditingPosition(PassRefPtr<Node> node, int offset)
{
return Position(node, Position::LegacyEditingOffset(offset));
}
inline bool operator==(const Position& a, const Position& b)
{
return a.anchorNode() == b.anchorNode() && a.deprecatedEditingOffset() == b.deprecatedEditingOffset() && a.anchorType() == b.anchorType();
}
inline bool operator!=(const Position& a, const Position& b)
{
return !(a == b);
}
inline bool operator<(const Position& a, const Position& b)
{
if (a.isNull() || b.isNull())
return false;
if (a.anchorNode() == b.anchorNode())
return a.deprecatedEditingOffset() < b.deprecatedEditingOffset();
return b.anchorNode()->compareDocumentPosition(a.anchorNode()) == Node::DOCUMENT_POSITION_PRECEDING;
}
inline bool operator>(const Position& a, const Position& b)
{
return !a.isNull() && !b.isNull() && a != b && b < a;
}
inline bool operator>=(const Position& a, const Position& b)
{
return !a.isNull() && !b.isNull() && (a == b || a > b);
}
inline bool operator<=(const Position& a, const Position& b)
{
return !a.isNull() && !b.isNull() && (a == b || a < b);
}
inline Position positionInParentBeforeNode(const Node* node)
{
ASSERT(node->parentNode());
return Position(node->parentNode(), node->nodeIndex(), Position::PositionIsOffsetInAnchor);
}
inline Position positionInParentAfterNode(const Node* node)
{
ASSERT(node->parentNode());
return Position(node->parentNode(), node->nodeIndex() + 1, Position::PositionIsOffsetInAnchor);
}
inline Position positionBeforeNode(Node* anchorNode)
{
ASSERT(anchorNode);
return Position(anchorNode, Position::PositionIsBeforeAnchor);
}
inline Position positionAfterNode(Node* anchorNode)
{
ASSERT(anchorNode);
return Position(anchorNode, Position::PositionIsAfterAnchor);
}
inline int lastOffsetInNode(Node* node)
{
return node->offsetInCharacters() ? node->maxCharacterOffset() : static_cast<int>(node->childNodeCount());
}
inline Position firstPositionInNode(Node* anchorNode)
{
if (anchorNode->isTextNode())
return Position(anchorNode, 0, Position::PositionIsOffsetInAnchor);
return Position(anchorNode, Position::PositionIsBeforeChildren);
}
inline Position lastPositionInNode(Node* anchorNode)
{
if (anchorNode->isTextNode())
return Position(anchorNode, lastOffsetInNode(anchorNode), Position::PositionIsOffsetInAnchor);
return Position(anchorNode, Position::PositionIsAfterChildren);
}
inline int minOffsetForNode(Node* anchorNode, int offset)
{
if (anchorNode->offsetInCharacters())
return std::min(offset, anchorNode->maxCharacterOffset());
int newOffset = 0;
for (Node* node = anchorNode->firstChild(); node && newOffset < offset; node = node->nextSibling())
newOffset++;
return newOffset;
}
inline bool offsetIsBeforeLastNodeOffset(int offset, Node* anchorNode)
{
if (anchorNode->offsetInCharacters())
return offset < anchorNode->maxCharacterOffset();
int currentOffset = 0;
for (Node* node = anchorNode->firstChild(); node && currentOffset < offset; node = node->nextSibling())
currentOffset++;
return offset < currentOffset;
}
}
#ifndef NDEBUG
void showTree(const WebCore::Position&);
void showTree(const WebCore::Position*);
#endif
#endif // Position_h