#ifndef Position_h
#define Position_h
#include "core/dom/ContainerNode.h"
#include "core/editing/EditingBoundary.h"
#include "core/editing/TextAffinity.h"
#include "platform/text/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 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; }
bool inDocument() const { return m_anchorNode && m_anchorNode->inDocument(); }
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;
PassRefPtr<CSSComputedStyleDeclaration> computedStyle() 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 inRenderedText() 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 bool hasRenderedNonAnonymousDescendantsWithHeight(RenderObject*);
static bool nodeIsUserSelectNone(Node*);
static bool nodeIsUserSelectAll(const Node*);
static Node* rootUserSelectAllForNode(Node*);
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;
int renderedOffset() 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 Position positionInParentBeforeNode(const Node& node)
{
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->countChildren());
}
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;
}
class PositionWithAffinity {
public:
PositionWithAffinity()
: m_affinity(DOWNSTREAM)
{
}
PositionWithAffinity(const Position& position, EAffinity affinity = DOWNSTREAM)
: m_position(position)
, m_affinity(affinity)
{
}
EAffinity affinity() const { return m_affinity; }
const Position& position() const { return m_position; }
private:
Position m_position;
EAffinity m_affinity;
};
}
#ifndef NDEBUG
void showTree(const WebCore::Position&);
void showTree(const WebCore::Position*);
#endif
#endif