This source file includes following definitions.
- m_isDirectional
- m_isDirectional
- m_isDirectional
- m_isDirectional
- m_isDirectional
- m_isDirectional
- m_isDirectional
- selectionFromContentsOfNode
- setBase
- setBase
- setExtent
- setExtent
- firstRange
- toNormalizedRange
- expandUsingGranularity
- makeSearchRange
- appendTrailingWhitespace
- setBaseAndExtentToDeepEquivalents
- setStartAndEndFromBaseAndExtentRespectingGranularity
- updateSelectionType
- validate
- setWithoutValidation
- adjustPositionForEnd
- adjustPositionForStart
- adjustSelectionToAvoidCrossingShadowBoundaries
- adjustSelectionToAvoidCrossingEditingBoundaries
- visiblePositionRespectingEditingBoundary
- isContentEditable
- rendererIsEditable
- isContentRichlyEditable
- rootEditableElement
- nonBoundaryShadowTreeRootNode
- setChangeObserver
- clearChangeObserver
- didChange
- debugPosition
- formatForDebugger
- showTreeForThis
- showTree
- showTree
#include "config.h"
#include "core/editing/VisibleSelection.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/Range.h"
#include "core/editing/TextIterator.h"
#include "core/editing/VisibleUnits.h"
#include "core/editing/htmlediting.h"
#include "core/rendering/RenderObject.h"
#include "platform/geometry/LayoutPoint.h"
#include "wtf/Assertions.h"
#include "wtf/text/CString.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/unicode/CharacterNames.h"
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace WebCore {
VisibleSelection::VisibleSelection()
: m_affinity(DOWNSTREAM)
, m_changeObserver(0)
, m_selectionType(NoSelection)
, m_baseIsFirst(true)
, m_isDirectional(false)
{
}
VisibleSelection::VisibleSelection(const Position& pos, EAffinity affinity, bool isDirectional)
: m_base(pos)
, m_extent(pos)
, m_affinity(affinity)
, m_changeObserver(0)
, m_isDirectional(isDirectional)
{
validate();
}
VisibleSelection::VisibleSelection(const Position& base, const Position& extent, EAffinity affinity, bool isDirectional)
: m_base(base)
, m_extent(extent)
, m_affinity(affinity)
, m_changeObserver(0)
, m_isDirectional(isDirectional)
{
validate();
}
VisibleSelection::VisibleSelection(const VisiblePosition& pos, bool isDirectional)
: m_base(pos.deepEquivalent())
, m_extent(pos.deepEquivalent())
, m_affinity(pos.affinity())
, m_changeObserver(0)
, m_isDirectional(isDirectional)
{
validate();
}
VisibleSelection::VisibleSelection(const VisiblePosition& base, const VisiblePosition& extent, bool isDirectional)
: m_base(base.deepEquivalent())
, m_extent(extent.deepEquivalent())
, m_affinity(base.affinity())
, m_changeObserver(0)
, m_isDirectional(isDirectional)
{
validate();
}
VisibleSelection::VisibleSelection(const Range* range, EAffinity affinity, bool isDirectional)
: m_base(range->startPosition())
, m_extent(range->endPosition())
, m_affinity(affinity)
, m_changeObserver(0)
, m_isDirectional(isDirectional)
{
validate();
}
VisibleSelection::VisibleSelection(const VisibleSelection& other)
: m_base(other.m_base)
, m_extent(other.m_extent)
, m_start(other.m_start)
, m_end(other.m_end)
, m_affinity(other.m_affinity)
, m_changeObserver(0)
, m_selectionType(other.m_selectionType)
, m_baseIsFirst(other.m_baseIsFirst)
, m_isDirectional(other.m_isDirectional)
{
}
VisibleSelection& VisibleSelection::operator=(const VisibleSelection& other)
{
didChange();
m_base = other.m_base;
m_extent = other.m_extent;
m_start = other.m_start;
m_end = other.m_end;
m_affinity = other.m_affinity;
m_changeObserver = 0;
m_selectionType = other.m_selectionType;
m_baseIsFirst = other.m_baseIsFirst;
m_isDirectional = other.m_isDirectional;
return *this;
}
VisibleSelection::~VisibleSelection()
{
didChange();
}
VisibleSelection VisibleSelection::selectionFromContentsOfNode(Node* node)
{
ASSERT(!editingIgnoresContent(node));
return VisibleSelection(firstPositionInNode(node), lastPositionInNode(node), DOWNSTREAM);
}
void VisibleSelection::setBase(const Position& position)
{
Position oldBase = m_base;
m_base = position;
validate();
if (m_base != oldBase)
didChange();
}
void VisibleSelection::setBase(const VisiblePosition& visiblePosition)
{
Position oldBase = m_base;
m_base = visiblePosition.deepEquivalent();
validate();
if (m_base != oldBase)
didChange();
}
void VisibleSelection::setExtent(const Position& position)
{
Position oldExtent = m_extent;
m_extent = position;
validate();
if (m_extent != oldExtent)
didChange();
}
void VisibleSelection::setExtent(const VisiblePosition& visiblePosition)
{
Position oldExtent = m_extent;
m_extent = visiblePosition.deepEquivalent();
validate();
if (m_extent != oldExtent)
didChange();
}
PassRefPtrWillBeRawPtr<Range> VisibleSelection::firstRange() const
{
if (isNone())
return nullptr;
Position start = m_start.parentAnchoredEquivalent();
Position end = m_end.parentAnchoredEquivalent();
return Range::create(*start.document(), start, end);
}
PassRefPtrWillBeRawPtr<Range> VisibleSelection::toNormalizedRange() const
{
if (isNone())
return nullptr;
m_start.document()->updateLayout();
if (isNone())
return nullptr;
Position s, e;
if (isCaret()) {
s = m_start.upstream().parentAnchoredEquivalent();
e = s;
} else {
ASSERT(isRange());
s = m_start.downstream();
e = m_end.upstream();
if (comparePositions(s, e) > 0) {
Position tmp = s;
s = e;
e = tmp;
}
s = s.parentAnchoredEquivalent();
e = e.parentAnchoredEquivalent();
}
if (!s.containerNode() || !e.containerNode())
return nullptr;
return Range::create(*s.document(), s, e);
}
bool VisibleSelection::expandUsingGranularity(TextGranularity granularity)
{
if (isNone())
return false;
Position oldBase = m_base;
Position oldExtent = m_extent;
Position oldStart = m_start;
Position oldEnd = m_end;
validate(granularity);
if (m_base != oldBase || m_extent != oldExtent || m_start != oldStart || m_end != oldEnd)
didChange();
return true;
}
static PassRefPtrWillBeRawPtr<Range> makeSearchRange(const Position& pos)
{
Node* n = pos.deprecatedNode();
if (!n)
return nullptr;
Document& d = n->document();
Node* de = d.documentElement();
if (!de)
return nullptr;
Node* boundary = n->enclosingBlockFlowElement();
if (!boundary)
return nullptr;
RefPtrWillBeRawPtr<Range> searchRange(Range::create(d));
TrackExceptionState exceptionState;
Position start(pos.parentAnchoredEquivalent());
searchRange->selectNodeContents(boundary, exceptionState);
searchRange->setStart(start.containerNode(), start.offsetInContainerNode(), exceptionState);
ASSERT(!exceptionState.hadException());
if (exceptionState.hadException())
return nullptr;
return searchRange.release();
}
void VisibleSelection::appendTrailingWhitespace()
{
RefPtrWillBeRawPtr<Range> searchRange = makeSearchRange(m_end);
if (!searchRange)
return;
CharacterIterator charIt(searchRange.get(), TextIteratorEmitsCharactersBetweenAllVisiblePositions);
bool changed = false;
for (; charIt.length(); charIt.advance(1)) {
UChar c = charIt.characterAt(0);
if ((!isSpaceOrNewline(c) && c != noBreakSpace) || c == '\n')
break;
m_end = charIt.range()->endPosition();
changed = true;
}
if (changed)
didChange();
}
void VisibleSelection::setBaseAndExtentToDeepEquivalents()
{
bool baseAndExtentEqual = m_base == m_extent;
if (m_base.isNotNull()) {
m_base = VisiblePosition(m_base, m_affinity).deepEquivalent();
if (baseAndExtentEqual)
m_extent = m_base;
}
if (m_extent.isNotNull() && !baseAndExtentEqual)
m_extent = VisiblePosition(m_extent, m_affinity).deepEquivalent();
if (m_base.isNull() && m_extent.isNull())
m_baseIsFirst = true;
else if (m_base.isNull()) {
m_base = m_extent;
m_baseIsFirst = true;
} else if (m_extent.isNull()) {
m_extent = m_base;
m_baseIsFirst = true;
} else
m_baseIsFirst = comparePositions(m_base, m_extent) <= 0;
}
void VisibleSelection::setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity granularity)
{
if (m_baseIsFirst) {
m_start = m_base;
m_end = m_extent;
} else {
m_start = m_extent;
m_end = m_base;
}
switch (granularity) {
case CharacterGranularity:
break;
case WordGranularity: {
VisiblePosition start = VisiblePosition(m_start, m_affinity);
VisiblePosition originalEnd(m_end, m_affinity);
EWordSide side = RightWordIfOnBoundary;
if (isEndOfEditableOrNonEditableContent(start) || (isEndOfLine(start) && !isStartOfLine(start) && !isEndOfParagraph(start)))
side = LeftWordIfOnBoundary;
m_start = startOfWord(start, side).deepEquivalent();
side = RightWordIfOnBoundary;
if (isEndOfEditableOrNonEditableContent(originalEnd) || (isEndOfLine(originalEnd) && !isStartOfLine(originalEnd) && !isEndOfParagraph(originalEnd)))
side = LeftWordIfOnBoundary;
VisiblePosition wordEnd(endOfWord(originalEnd, side));
VisiblePosition end(wordEnd);
if (isEndOfParagraph(originalEnd) && !isEmptyTableCell(m_start.deprecatedNode())) {
end = wordEnd.next();
if (Node* table = isFirstPositionAfterTable(end)) {
if (isBlock(table))
end = end.next(CannotCrossEditingBoundary);
else
end = wordEnd;
}
if (end.isNull())
end = wordEnd;
}
m_end = end.deepEquivalent();
break;
}
case SentenceGranularity: {
m_start = startOfSentence(VisiblePosition(m_start, m_affinity)).deepEquivalent();
m_end = endOfSentence(VisiblePosition(m_end, m_affinity)).deepEquivalent();
break;
}
case LineGranularity: {
m_start = startOfLine(VisiblePosition(m_start, m_affinity)).deepEquivalent();
VisiblePosition end = endOfLine(VisiblePosition(m_end, m_affinity));
if (isEndOfParagraph(end)) {
VisiblePosition next = end.next();
if (next.isNotNull())
end = next;
}
m_end = end.deepEquivalent();
break;
}
case LineBoundary:
m_start = startOfLine(VisiblePosition(m_start, m_affinity)).deepEquivalent();
m_end = endOfLine(VisiblePosition(m_end, m_affinity)).deepEquivalent();
break;
case ParagraphGranularity: {
VisiblePosition pos(m_start, m_affinity);
if (isStartOfLine(pos) && isEndOfEditableOrNonEditableContent(pos))
pos = pos.previous();
m_start = startOfParagraph(pos).deepEquivalent();
VisiblePosition visibleParagraphEnd = endOfParagraph(VisiblePosition(m_end, m_affinity));
VisiblePosition end(visibleParagraphEnd.next());
if (Node* table = isFirstPositionAfterTable(end)) {
if (isBlock(table))
end = end.next(CannotCrossEditingBoundary);
else
end = visibleParagraphEnd;
}
if (end.isNull())
end = visibleParagraphEnd;
m_end = end.deepEquivalent();
break;
}
case DocumentBoundary:
m_start = startOfDocument(VisiblePosition(m_start, m_affinity)).deepEquivalent();
m_end = endOfDocument(VisiblePosition(m_end, m_affinity)).deepEquivalent();
break;
case ParagraphBoundary:
m_start = startOfParagraph(VisiblePosition(m_start, m_affinity)).deepEquivalent();
m_end = endOfParagraph(VisiblePosition(m_end, m_affinity)).deepEquivalent();
break;
case SentenceBoundary:
m_start = startOfSentence(VisiblePosition(m_start, m_affinity)).deepEquivalent();
m_end = endOfSentence(VisiblePosition(m_end, m_affinity)).deepEquivalent();
break;
}
if (m_start.isNull())
m_start = m_end;
if (m_end.isNull())
m_end = m_start;
}
void VisibleSelection::updateSelectionType()
{
if (m_start.isNull()) {
ASSERT(m_end.isNull());
m_selectionType = NoSelection;
} else if (m_start == m_end || m_start.upstream() == m_end.upstream()) {
m_selectionType = CaretSelection;
} else
m_selectionType = RangeSelection;
if (m_selectionType != CaretSelection)
m_affinity = DOWNSTREAM;
}
void VisibleSelection::validate(TextGranularity granularity)
{
setBaseAndExtentToDeepEquivalents();
setStartAndEndFromBaseAndExtentRespectingGranularity(granularity);
adjustSelectionToAvoidCrossingShadowBoundaries();
adjustSelectionToAvoidCrossingEditingBoundaries();
updateSelectionType();
if (selectionType() == RangeSelection) {
m_start = m_start.downstream();
m_end = m_end.upstream();
adjustSelectionToAvoidCrossingEditingBoundaries();
}
}
void VisibleSelection::setWithoutValidation(const Position& base, const Position& extent)
{
ASSERT(!base.isNull());
ASSERT(!extent.isNull());
ASSERT(m_affinity == DOWNSTREAM);
m_base = base;
m_extent = extent;
m_baseIsFirst = comparePositions(base, extent) <= 0;
if (m_baseIsFirst) {
m_start = base;
m_end = extent;
} else {
m_start = extent;
m_end = base;
}
m_selectionType = base == extent ? CaretSelection : RangeSelection;
didChange();
}
static Position adjustPositionForEnd(const Position& currentPosition, Node* startContainerNode)
{
TreeScope& treeScope = startContainerNode->treeScope();
ASSERT(currentPosition.containerNode()->treeScope() != treeScope);
if (Node* ancestor = treeScope.ancestorInThisScope(currentPosition.containerNode())) {
if (ancestor->contains(startContainerNode))
return positionAfterNode(ancestor);
return positionBeforeNode(ancestor);
}
if (Node* lastChild = treeScope.rootNode().lastChild())
return positionAfterNode(lastChild);
return Position();
}
static Position adjustPositionForStart(const Position& currentPosition, Node* endContainerNode)
{
TreeScope& treeScope = endContainerNode->treeScope();
ASSERT(currentPosition.containerNode()->treeScope() != treeScope);
if (Node* ancestor = treeScope.ancestorInThisScope(currentPosition.containerNode())) {
if (ancestor->contains(endContainerNode))
return positionBeforeNode(ancestor);
return positionAfterNode(ancestor);
}
if (Node* firstChild = treeScope.rootNode().firstChild())
return positionBeforeNode(firstChild);
return Position();
}
void VisibleSelection::adjustSelectionToAvoidCrossingShadowBoundaries()
{
if (m_base.isNull() || m_start.isNull() || m_end.isNull())
return;
if (m_start.anchorNode()->treeScope() == m_end.anchorNode()->treeScope())
return;
if (m_baseIsFirst) {
m_extent = adjustPositionForEnd(m_end, m_start.containerNode());
m_end = m_extent;
} else {
m_extent = adjustPositionForStart(m_start, m_end.containerNode());
m_start = m_extent;
}
ASSERT(m_start.anchorNode()->treeScope() == m_end.anchorNode()->treeScope());
}
void VisibleSelection::adjustSelectionToAvoidCrossingEditingBoundaries()
{
if (m_base.isNull() || m_start.isNull() || m_end.isNull())
return;
Node* baseRoot = highestEditableRoot(m_base);
Node* startRoot = highestEditableRoot(m_start);
Node* endRoot = highestEditableRoot(m_end);
Node* baseEditableAncestor = lowestEditableAncestor(m_base.containerNode());
if (baseRoot == startRoot && baseRoot == endRoot)
return;
if (baseRoot) {
if (startRoot != baseRoot) {
VisiblePosition first = firstEditablePositionAfterPositionInRoot(m_start, baseRoot);
m_start = first.deepEquivalent();
if (m_start.isNull()) {
ASSERT_NOT_REACHED();
m_start = m_end;
}
}
if (endRoot != baseRoot) {
VisiblePosition last = lastEditablePositionBeforePositionInRoot(m_end, baseRoot);
m_end = last.deepEquivalent();
if (m_end.isNull())
m_end = m_start;
}
} else {
Node* endEditableAncestor = lowestEditableAncestor(m_end.containerNode());
if (endRoot || endEditableAncestor != baseEditableAncestor) {
Position p = previousVisuallyDistinctCandidate(m_end);
Node* shadowAncestor = endRoot ? endRoot->shadowHost() : 0;
if (p.isNull() && shadowAncestor)
p = positionAfterNode(shadowAncestor);
while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) {
Node* root = editableRootForPosition(p);
shadowAncestor = root ? root->shadowHost() : 0;
p = isAtomicNode(p.containerNode()) ? positionInParentBeforeNode(*p.containerNode()) : previousVisuallyDistinctCandidate(p);
if (p.isNull() && shadowAncestor)
p = positionAfterNode(shadowAncestor);
}
VisiblePosition previous(p);
if (previous.isNull()) {
ASSERT_NOT_REACHED();
m_base = Position();
m_extent = Position();
validate();
return;
}
m_end = previous.deepEquivalent();
}
Node* startEditableAncestor = lowestEditableAncestor(m_start.containerNode());
if (startRoot || startEditableAncestor != baseEditableAncestor) {
Position p = nextVisuallyDistinctCandidate(m_start);
Node* shadowAncestor = startRoot ? startRoot->shadowHost() : 0;
if (p.isNull() && shadowAncestor)
p = positionBeforeNode(shadowAncestor);
while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) {
Node* root = editableRootForPosition(p);
shadowAncestor = root ? root->shadowHost() : 0;
p = isAtomicNode(p.containerNode()) ? positionInParentAfterNode(*p.containerNode()) : nextVisuallyDistinctCandidate(p);
if (p.isNull() && shadowAncestor)
p = positionBeforeNode(shadowAncestor);
}
VisiblePosition next(p);
if (next.isNull()) {
ASSERT_NOT_REACHED();
m_base = Position();
m_extent = Position();
validate();
return;
}
m_start = next.deepEquivalent();
}
}
if (baseEditableAncestor != lowestEditableAncestor(m_extent.containerNode()))
m_extent = m_baseIsFirst ? m_end : m_start;
}
VisiblePosition VisibleSelection::visiblePositionRespectingEditingBoundary(const LayoutPoint& localPoint, Node* targetNode) const
{
if (!targetNode->renderer())
return VisiblePosition();
LayoutPoint selectionEndPoint = localPoint;
Element* editableElement = rootEditableElement();
if (editableElement && !editableElement->contains(targetNode)) {
if (!editableElement->renderer())
return VisiblePosition();
FloatPoint absolutePoint = targetNode->renderer()->localToAbsolute(FloatPoint(selectionEndPoint));
selectionEndPoint = roundedLayoutPoint(editableElement->renderer()->absoluteToLocal(absolutePoint));
targetNode = editableElement;
}
return VisiblePosition(targetNode->renderer()->positionForPoint(selectionEndPoint));
}
bool VisibleSelection::isContentEditable() const
{
return isEditablePosition(start());
}
bool VisibleSelection::rendererIsEditable() const
{
return isEditablePosition(start(), ContentIsEditable, DoNotUpdateStyle);
}
bool VisibleSelection::isContentRichlyEditable() const
{
return isRichlyEditablePosition(start());
}
Element* VisibleSelection::rootEditableElement() const
{
return editableRootForPosition(start());
}
Node* VisibleSelection::nonBoundaryShadowTreeRootNode() const
{
return start().deprecatedNode() ? start().deprecatedNode()->nonBoundaryShadowTreeRootNode() : 0;
}
VisibleSelection::ChangeObserver::ChangeObserver()
{
}
VisibleSelection::ChangeObserver::~ChangeObserver()
{
}
void VisibleSelection::setChangeObserver(ChangeObserver& observer)
{
ASSERT(!m_changeObserver);
m_changeObserver = &observer;
}
void VisibleSelection::clearChangeObserver()
{
ASSERT(m_changeObserver);
m_changeObserver = 0;
}
void VisibleSelection::didChange()
{
if (m_changeObserver)
m_changeObserver->didChangeVisibleSelection();
}
#ifndef NDEBUG
void VisibleSelection::debugPosition() const
{
fprintf(stderr, "VisibleSelection ===============\n");
if (!m_start.anchorNode())
fputs("pos: null", stderr);
else if (m_start == m_end) {
fprintf(stderr, "pos: %s ", m_start.anchorNode()->nodeName().utf8().data());
m_start.showAnchorTypeAndOffset();
} else {
fprintf(stderr, "start: %s ", m_start.anchorNode()->nodeName().utf8().data());
m_start.showAnchorTypeAndOffset();
fprintf(stderr, "end: %s ", m_end.anchorNode()->nodeName().utf8().data());
m_end.showAnchorTypeAndOffset();
}
fprintf(stderr, "================================\n");
}
void VisibleSelection::formatForDebugger(char* buffer, unsigned length) const
{
StringBuilder result;
String s;
if (isNone()) {
result.appendLiteral("<none>");
} else {
const int FormatBufferSize = 1024;
char s[FormatBufferSize];
result.appendLiteral("from ");
start().formatForDebugger(s, FormatBufferSize);
result.append(s);
result.appendLiteral(" to ");
end().formatForDebugger(s, FormatBufferSize);
result.append(s);
}
strncpy(buffer, result.toString().utf8().data(), length - 1);
}
void VisibleSelection::showTreeForThis() const
{
if (start().anchorNode()) {
start().anchorNode()->showTreeAndMark(start().anchorNode(), "S", end().anchorNode(), "E");
fputs("start: ", stderr);
start().showAnchorTypeAndOffset();
fputs("end: ", stderr);
end().showAnchorTypeAndOffset();
}
}
#endif
}
#ifndef NDEBUG
void showTree(const WebCore::VisibleSelection& sel)
{
sel.showTreeForThis();
}
void showTree(const WebCore::VisibleSelection* sel)
{
if (sel)
sel->showTreeForThis();
}
#endif