VisiblePosition.cpp [plain text]
#include "config.h"
#include "VisiblePosition.h"
#include "Document.h"
#include "Editing.h"
#include "FloatQuad.h"
#include "HTMLElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLNames.h"
#include "InlineTextBox.h"
#include "Logging.h"
#include "Range.h"
#include "RenderBlock.h"
#include "RootInlineBox.h"
#include "Text.h"
#include "VisibleUnits.h"
#include <stdio.h>
#include <wtf/text/CString.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
using namespace HTMLNames;
VisiblePosition::VisiblePosition(const Position &pos, EAffinity affinity)
{
init(pos, affinity);
}
void VisiblePosition::init(const Position& position, EAffinity affinity)
{
m_affinity = affinity;
m_deepPosition = canonicalPosition(position);
if (m_affinity == UPSTREAM && (isNull() || inSameLine(VisiblePosition(position, DOWNSTREAM), *this)))
m_affinity = DOWNSTREAM;
}
VisiblePosition VisiblePosition::next(EditingBoundaryCrossingRule rule, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
ASSERT(rule == CanCrossEditingBoundary || rule == CannotCrossEditingBoundary);
VisiblePosition next(nextVisuallyDistinctCandidate(m_deepPosition), m_affinity);
if (rule == CanCrossEditingBoundary)
return next;
return honorEditingBoundaryAtOrAfter(next, reachedBoundary);
}
VisiblePosition VisiblePosition::previous(EditingBoundaryCrossingRule rule, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
ASSERT(rule == CanCrossEditingBoundary || rule == CannotCrossEditingBoundary);
Position pos = previousVisuallyDistinctCandidate(m_deepPosition);
if (pos.atStartOfTree()) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
VisiblePosition prev = VisiblePosition(pos, DOWNSTREAM);
ASSERT(prev != *this);
#ifndef NDEBUG
if (prev.isNotNull() && m_affinity == UPSTREAM) {
VisiblePosition temp = prev;
temp.setAffinity(UPSTREAM);
ASSERT(inSameLine(temp, prev));
}
#endif
if (rule == CanCrossEditingBoundary)
return prev;
return honorEditingBoundaryAtOrBefore(prev, reachedBoundary);
}
Position VisiblePosition::leftVisuallyDistinctCandidate() const
{
Position p = m_deepPosition;
if (p.isNull())
return Position();
Position downstreamStart = p.downstream();
TextDirection primaryDirection = p.primaryDirection();
while (true) {
InlineBox* box;
int offset;
p.getInlineBoxAndOffset(m_affinity, primaryDirection, box, offset);
if (!box)
return primaryDirection == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
RenderObject* renderer = &box->renderer();
while (true) {
if ((renderer->isReplaced() || renderer->isBR()) && offset == box->caretRightmostOffset())
return box->isLeftToRightDirection() ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
if (!renderer->node()) {
box = box->prevLeafChild();
if (!box)
return primaryDirection == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
renderer = &box->renderer();
offset = box->caretRightmostOffset();
continue;
}
offset = box->isLeftToRightDirection() ? renderer->previousOffset(offset) : renderer->nextOffset(offset);
int caretMinOffset = box->caretMinOffset();
int caretMaxOffset = box->caretMaxOffset();
if (offset > caretMinOffset && offset < caretMaxOffset)
break;
if (box->isLeftToRightDirection() ? offset < caretMinOffset : offset > caretMaxOffset) {
InlineBox* prevBox = box->prevLeafChildIgnoringLineBreak();
if (!prevBox) {
Position positionOnLeft = primaryDirection == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
if (positionOnLeft.isNull())
return Position();
InlineBox* boxOnLeft;
int offsetOnLeft;
positionOnLeft.getInlineBoxAndOffset(m_affinity, primaryDirection, boxOnLeft, offsetOnLeft);
if (boxOnLeft && &boxOnLeft->root() == &box->root())
return Position();
return positionOnLeft;
}
box = prevBox;
renderer = &box->renderer();
offset = prevBox->caretRightmostOffset();
continue;
}
ASSERT(offset == box->caretLeftmostOffset());
unsigned char level = box->bidiLevel();
InlineBox* prevBox = box->prevLeafChild();
if (box->direction() == primaryDirection) {
if (!prevBox) {
InlineBox* logicalStart = nullptr;
if (primaryDirection == LTR ? box->root().getLogicalStartBoxWithNode(logicalStart) : box->root().getLogicalEndBoxWithNode(logicalStart)) {
box = logicalStart;
renderer = &box->renderer();
offset = primaryDirection == LTR ? box->caretMinOffset() : box->caretMaxOffset();
}
break;
}
if (prevBox->bidiLevel() >= level)
break;
level = prevBox->bidiLevel();
InlineBox* nextBox = box;
do {
nextBox = nextBox->nextLeafChild();
} while (nextBox && nextBox->bidiLevel() > level);
if (nextBox && nextBox->bidiLevel() == level)
break;
box = prevBox;
renderer = &box->renderer();
offset = box->caretRightmostOffset();
if (box->direction() == primaryDirection)
break;
continue;
}
while (prevBox && !prevBox->renderer().node())
prevBox = prevBox->prevLeafChild();
if (prevBox) {
box = prevBox;
renderer = &box->renderer();
offset = box->caretRightmostOffset();
if (box->bidiLevel() > level) {
do {
prevBox = prevBox->prevLeafChild();
} while (prevBox && prevBox->bidiLevel() > level);
if (!prevBox || prevBox->bidiLevel() < level)
continue;
}
} else {
while (true) {
while (InlineBox* nextBox = box->nextLeafChild()) {
if (nextBox->bidiLevel() < level)
break;
box = nextBox;
}
if (box->bidiLevel() == level)
break;
level = box->bidiLevel();
while (InlineBox* prevBox = box->prevLeafChild()) {
if (prevBox->bidiLevel() < level)
break;
box = prevBox;
}
if (box->bidiLevel() == level)
break;
level = box->bidiLevel();
}
renderer = &box->renderer();
offset = primaryDirection == LTR ? box->caretMinOffset() : box->caretMaxOffset();
}
break;
}
p = createLegacyEditingPosition(renderer->node(), offset);
if ((p.isCandidate() && p.downstream() != downstreamStart) || p.atStartOfTree() || p.atEndOfTree())
return p;
ASSERT(p != m_deepPosition);
}
}
VisiblePosition VisiblePosition::left(bool stayInEditableContent, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
Position pos = leftVisuallyDistinctCandidate();
if (pos.atStartOfTree() || pos.atEndOfTree()) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
VisiblePosition left = VisiblePosition(pos, DOWNSTREAM);
ASSERT(left != *this);
if (!stayInEditableContent)
return left;
return honorEditingBoundaryAtOrBefore(left, reachedBoundary);
}
Position VisiblePosition::rightVisuallyDistinctCandidate() const
{
Position p = m_deepPosition;
if (p.isNull())
return Position();
Position downstreamStart = p.downstream();
TextDirection primaryDirection = p.primaryDirection();
while (true) {
InlineBox* box;
int offset;
p.getInlineBoxAndOffset(m_affinity, primaryDirection, box, offset);
if (!box)
return primaryDirection == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
RenderObject* renderer = &box->renderer();
while (true) {
if ((renderer->isReplaced() || renderer->isBR()) && offset == box->caretLeftmostOffset())
return box->isLeftToRightDirection() ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
if (!renderer->node()) {
box = box->nextLeafChild();
if (!box)
return primaryDirection == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
renderer = &box->renderer();
offset = box->caretLeftmostOffset();
continue;
}
offset = box->isLeftToRightDirection() ? renderer->nextOffset(offset) : renderer->previousOffset(offset);
int caretMinOffset = box->caretMinOffset();
int caretMaxOffset = box->caretMaxOffset();
if (offset > caretMinOffset && offset < caretMaxOffset)
break;
if (box->isLeftToRightDirection() ? offset > caretMaxOffset : offset < caretMinOffset) {
InlineBox* nextBox = box->nextLeafChildIgnoringLineBreak();
if (!nextBox) {
Position positionOnRight = primaryDirection == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
if (positionOnRight.isNull())
return Position();
InlineBox* boxOnRight;
int offsetOnRight;
positionOnRight.getInlineBoxAndOffset(m_affinity, primaryDirection, boxOnRight, offsetOnRight);
if (boxOnRight && &boxOnRight->root() == &box->root())
return Position();
return positionOnRight;
}
box = nextBox;
renderer = &box->renderer();
offset = nextBox->caretLeftmostOffset();
continue;
}
ASSERT(offset == box->caretRightmostOffset());
unsigned char level = box->bidiLevel();
InlineBox* nextBox = box->nextLeafChild();
if (box->direction() == primaryDirection) {
if (!nextBox) {
InlineBox* logicalEnd = nullptr;
if (primaryDirection == LTR ? box->root().getLogicalEndBoxWithNode(logicalEnd) : box->root().getLogicalStartBoxWithNode(logicalEnd)) {
box = logicalEnd;
renderer = &box->renderer();
offset = primaryDirection == LTR ? box->caretMaxOffset() : box->caretMinOffset();
}
break;
}
if (nextBox->bidiLevel() >= level)
break;
level = nextBox->bidiLevel();
InlineBox* prevBox = box;
do {
prevBox = prevBox->prevLeafChild();
} while (prevBox && prevBox->bidiLevel() > level);
if (prevBox && prevBox->bidiLevel() == level) break;
box = nextBox;
renderer = &box->renderer();
offset = box->caretLeftmostOffset();
if (box->direction() == primaryDirection)
break;
continue;
}
while (nextBox && !nextBox->renderer().node())
nextBox = nextBox->nextLeafChild();
if (nextBox) {
box = nextBox;
renderer = &box->renderer();
offset = box->caretLeftmostOffset();
if (box->bidiLevel() > level) {
do {
nextBox = nextBox->nextLeafChild();
} while (nextBox && nextBox->bidiLevel() > level);
if (!nextBox || nextBox->bidiLevel() < level)
continue;
}
} else {
while (true) {
while (InlineBox* prevBox = box->prevLeafChild()) {
if (prevBox->bidiLevel() < level)
break;
box = prevBox;
}
if (box->bidiLevel() == level)
break;
level = box->bidiLevel();
while (InlineBox* nextBox = box->nextLeafChild()) {
if (nextBox->bidiLevel() < level)
break;
box = nextBox;
}
if (box->bidiLevel() == level)
break;
level = box->bidiLevel();
}
renderer = &box->renderer();
offset = primaryDirection == LTR ? box->caretMaxOffset() : box->caretMinOffset();
}
break;
}
p = createLegacyEditingPosition(renderer->node(), offset);
if ((p.isCandidate() && p.downstream() != downstreamStart) || p.atStartOfTree() || p.atEndOfTree())
return p;
ASSERT(p != m_deepPosition);
}
}
VisiblePosition VisiblePosition::right(bool stayInEditableContent, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
Position pos = rightVisuallyDistinctCandidate();
if (pos.atStartOfTree() || pos.atEndOfTree()) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
VisiblePosition right = VisiblePosition(pos, DOWNSTREAM);
ASSERT(right != *this);
if (!stayInEditableContent)
return right;
return honorEditingBoundaryAtOrAfter(right, reachedBoundary);
}
VisiblePosition VisiblePosition::honorEditingBoundaryAtOrBefore(const VisiblePosition& position, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
if (position.isNull())
return position;
auto* highestRoot = highestEditableRoot(deepEquivalent());
if (highestRoot && !position.deepEquivalent().deprecatedNode()->isDescendantOf(*highestRoot)) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
if (highestEditableRoot(position.deepEquivalent()) == highestRoot) {
if (reachedBoundary)
*reachedBoundary = *this == position;
return position;
}
if (!highestRoot) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
return lastEditablePositionBeforePositionInRoot(position.deepEquivalent(), highestRoot);
}
VisiblePosition VisiblePosition::honorEditingBoundaryAtOrAfter(const VisiblePosition &pos, bool* reachedBoundary) const
{
if (reachedBoundary)
*reachedBoundary = false;
if (pos.isNull())
return pos;
auto* highestRoot = highestEditableRoot(deepEquivalent());
if (highestRoot && !pos.deepEquivalent().deprecatedNode()->isDescendantOf(*highestRoot)) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
if (highestEditableRoot(pos.deepEquivalent()) == highestRoot) {
if (reachedBoundary)
*reachedBoundary = *this == pos;
return pos;
}
if (!highestRoot) {
if (reachedBoundary)
*reachedBoundary = true;
return VisiblePosition();
}
return firstEditablePositionAfterPositionInRoot(pos.deepEquivalent(), highestRoot);
}
static Position canonicalizeCandidate(const Position& candidate)
{
if (candidate.isNull())
return Position();
ASSERT(candidate.isCandidate());
Position upstream = candidate.upstream();
if (upstream.isCandidate())
return upstream;
return candidate;
}
Position VisiblePosition::canonicalPosition(const Position& passedPosition)
{
Position position = passedPosition;
if (position.isNull())
return Position();
ASSERT(position.document());
position.document()->updateLayoutIgnorePendingStylesheets();
Node* node = position.containerNode();
Position candidate = position.upstream();
if (candidate.isCandidate())
return candidate;
candidate = position.downstream();
if (candidate.isCandidate())
return candidate;
Position next = canonicalizeCandidate(nextCandidate(position));
Position prev = canonicalizeCandidate(previousCandidate(position));
Node* nextNode = next.deprecatedNode();
Node* prevNode = prev.deprecatedNode();
if (is<HTMLHtmlElement>(node) && !node->hasEditableStyle()) {
auto* body = node->document().bodyOrFrameset();
if (body && body->hasEditableStyle())
return next.isNotNull() ? next : prev;
}
Node* editingRoot = editableRootForPosition(position);
if ((editingRoot && editingRoot->hasTagName(htmlTag)) || (node && (node->isDocumentNode() || node->isShadowRoot())))
return next.isNotNull() ? next : prev;
bool prevIsInSameEditableElement = prevNode && editableRootForPosition(prev) == editingRoot;
bool nextIsInSameEditableElement = nextNode && editableRootForPosition(next) == editingRoot;
if (prevIsInSameEditableElement && !nextIsInSameEditableElement)
return prev;
if (nextIsInSameEditableElement && !prevIsInSameEditableElement)
return next;
if (!nextIsInSameEditableElement && !prevIsInSameEditableElement)
return Position();
Element* originalBlock = deprecatedEnclosingBlockFlowElement(node);
bool nextIsOutsideOriginalBlock = !nextNode->isDescendantOf(originalBlock) && nextNode != originalBlock;
bool prevIsOutsideOriginalBlock = !prevNode->isDescendantOf(originalBlock) && prevNode != originalBlock;
if (nextIsOutsideOriginalBlock && !prevIsOutsideOriginalBlock)
return prev;
return next;
}
UChar32 VisiblePosition::characterAfter() const
{
Position pos = m_deepPosition.downstream();
if (!pos.containerNode() || !pos.containerNode()->isTextNode())
return 0;
switch (pos.anchorType()) {
case Position::PositionIsAfterChildren:
case Position::PositionIsAfterAnchor:
case Position::PositionIsBeforeAnchor:
case Position::PositionIsBeforeChildren:
return 0;
case Position::PositionIsOffsetInAnchor:
break;
}
unsigned offset = static_cast<unsigned>(pos.offsetInContainerNode());
Text* textNode = pos.containerText();
unsigned length = textNode->length();
if (offset >= length)
return 0;
UChar32 ch;
U16_NEXT(textNode->data(), offset, length, ch);
return ch;
}
LayoutRect VisiblePosition::localCaretRect(RenderObject*& renderer) const
{
if (m_deepPosition.isNull()) {
renderer = nullptr;
return IntRect();
}
Node* node = m_deepPosition.anchorNode();
renderer = node->renderer();
if (!renderer)
return LayoutRect();
InlineBox* inlineBox;
int caretOffset;
getInlineBoxAndOffset(inlineBox, caretOffset);
if (inlineBox)
renderer = &inlineBox->renderer();
return renderer->localCaretRect(inlineBox, caretOffset);
}
IntRect VisiblePosition::absoluteCaretBounds(bool* insideFixed) const
{
RenderBlock* renderer = nullptr;
LayoutRect localRect = localCaretRectInRendererForCaretPainting(*this, renderer);
return absoluteBoundsForLocalCaretRect(renderer, localRect, insideFixed);
}
int VisiblePosition::lineDirectionPointForBlockDirectionNavigation() const
{
RenderObject* renderer;
LayoutRect localRect = localCaretRect(renderer);
if (localRect.isEmpty() || !renderer)
return 0;
FloatPoint caretPoint = renderer->localToAbsolute(localRect.location());
RenderObject* containingBlock = renderer->containingBlock();
if (!containingBlock)
containingBlock = renderer; return containingBlock->isHorizontalWritingMode() ? caretPoint.x() : caretPoint.y();
}
#if ENABLE(TREE_DEBUGGING)
void VisiblePosition::debugPosition(const char* msg) const
{
if (isNull())
fprintf(stderr, "Position [%s]: null\n", msg);
else {
fprintf(stderr, "Position [%s]: %s, ", msg, m_deepPosition.deprecatedNode()->nodeName().utf8().data());
m_deepPosition.showAnchorTypeAndOffset();
}
}
void VisiblePosition::formatForDebugger(char* buffer, unsigned length) const
{
m_deepPosition.formatForDebugger(buffer, length);
}
void VisiblePosition::showTreeForThis() const
{
m_deepPosition.showTreeForThis();
}
#endif
RefPtr<Range> makeRange(const VisiblePosition& start, const VisiblePosition& end)
{
if (start.isNull() || end.isNull())
return nullptr;
Position s = start.deepEquivalent().parentAnchoredEquivalent();
Position e = end.deepEquivalent().parentAnchoredEquivalent();
if (s.isNull() || e.isNull())
return nullptr;
return Range::create(s.containerNode()->document(), s.containerNode(), s.offsetInContainerNode(), e.containerNode(), e.offsetInContainerNode());
}
VisiblePosition startVisiblePosition(const Range *r, EAffinity affinity)
{
return VisiblePosition(r->startPosition(), affinity);
}
VisiblePosition endVisiblePosition(const Range *r, EAffinity affinity)
{
return VisiblePosition(r->endPosition(), affinity);
}
bool setStart(Range* range, const VisiblePosition& visiblePosition)
{
if (!range)
return false;
Position p = visiblePosition.deepEquivalent().parentAnchoredEquivalent();
if (!p.containerNode())
return false;
return !range->setStart(*p.containerNode(), p.offsetInContainerNode()).hasException();
}
bool setEnd(Range* range, const VisiblePosition& visiblePosition)
{
if (!range)
return false;
Position p = visiblePosition.deepEquivalent().parentAnchoredEquivalent();
if (!p.containerNode())
return false;
return !range->setEnd(*p.containerNode(), p.offsetInContainerNode()).hasException();
}
Element* enclosingBlockFlowElement(const VisiblePosition& visiblePosition)
{
if (visiblePosition.isNull())
return nullptr;
return deprecatedEnclosingBlockFlowElement(visiblePosition.deepEquivalent().deprecatedNode());
}
bool isFirstVisiblePositionInNode(const VisiblePosition &visiblePosition, const Node *node)
{
if (visiblePosition.isNull())
return false;
if (!visiblePosition.deepEquivalent().containerNode()->isDescendantOf(node))
return false;
VisiblePosition previous = visiblePosition.previous();
return previous.isNull() || !previous.deepEquivalent().deprecatedNode()->isDescendantOf(node);
}
bool isLastVisiblePositionInNode(const VisiblePosition &visiblePosition, const Node *node)
{
if (visiblePosition.isNull())
return false;
if (!visiblePosition.deepEquivalent().containerNode()->isDescendantOf(node))
return false;
VisiblePosition next = visiblePosition.next();
return next.isNull() || !next.deepEquivalent().deprecatedNode()->isDescendantOf(node);
}
bool VisiblePosition::equals(const VisiblePosition& other) const
{
return m_affinity == other.m_affinity && m_deepPosition.equals(other.m_deepPosition);
}
TextStream& operator<<(TextStream& stream, EAffinity affinity)
{
switch (affinity) {
case UPSTREAM:
stream << "upstream";
break;
case DOWNSTREAM:
stream << "downstream";
break;
}
return stream;
}
TextStream& operator<<(TextStream& stream, const VisiblePosition& visiblePosition)
{
TextStream::GroupScope scope(stream);
stream << "VisiblePosition " << &visiblePosition;
stream.dumpProperty("position", visiblePosition.deepEquivalent());
stream.dumpProperty("affinity", visiblePosition.affinity());
return stream;
}
}
#if ENABLE(TREE_DEBUGGING)
void showTree(const WebCore::VisiblePosition* vpos)
{
if (vpos)
vpos->showTreeForThis();
}
void showTree(const WebCore::VisiblePosition& vpos)
{
vpos.showTreeForThis();
}
#endif