2
* Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "VisibleSelection.h"
31
#include "htmlediting.h"
32
#include "TextIterator.h"
33
#include "VisiblePosition.h"
34
#include "visible_units.h"
37
#include <wtf/Assertions.h>
38
#include <wtf/text/CString.h>
39
#include <wtf/text/StringBuilder.h>
40
#include <wtf/unicode/CharacterNames.h>
44
VisibleSelection::VisibleSelection()
45
: m_affinity(DOWNSTREAM)
46
, m_selectionType(NoSelection)
48
, m_isDirectional(false)
52
VisibleSelection::VisibleSelection(const Position& pos, EAffinity affinity, bool isDirectional)
55
, m_affinity(affinity)
56
, m_isDirectional(isDirectional)
61
VisibleSelection::VisibleSelection(const Position& base, const Position& extent, EAffinity affinity, bool isDirectional)
64
, m_affinity(affinity)
65
, m_isDirectional(isDirectional)
70
VisibleSelection::VisibleSelection(const VisiblePosition& pos, bool isDirectional)
71
: m_base(pos.deepEquivalent())
72
, m_extent(pos.deepEquivalent())
73
, m_affinity(pos.affinity())
74
, m_isDirectional(isDirectional)
79
VisibleSelection::VisibleSelection(const VisiblePosition& base, const VisiblePosition& extent, bool isDirectional)
80
: m_base(base.deepEquivalent())
81
, m_extent(extent.deepEquivalent())
82
, m_affinity(base.affinity())
83
, m_isDirectional(isDirectional)
88
VisibleSelection::VisibleSelection(const Range* range, EAffinity affinity, bool isDirectional)
89
: m_base(range->startPosition())
90
, m_extent(range->endPosition())
91
, m_affinity(affinity)
92
, m_isDirectional(isDirectional)
97
VisibleSelection VisibleSelection::selectionFromContentsOfNode(Node* node)
99
ASSERT(!editingIgnoresContent(node));
100
return VisibleSelection(firstPositionInNode(node), lastPositionInNode(node), DOWNSTREAM);
103
void VisibleSelection::setBase(const Position& position)
109
void VisibleSelection::setBase(const VisiblePosition& visiblePosition)
111
m_base = visiblePosition.deepEquivalent();
115
void VisibleSelection::setExtent(const Position& position)
121
void VisibleSelection::setExtent(const VisiblePosition& visiblePosition)
123
m_extent = visiblePosition.deepEquivalent();
127
PassRefPtr<Range> VisibleSelection::firstRange() const
131
Position start = m_start.parentAnchoredEquivalent();
132
Position end = m_end.parentAnchoredEquivalent();
133
return Range::create(start.anchorNode()->document(), start, end);
136
PassRefPtr<Range> VisibleSelection::toNormalizedRange() const
141
// Make sure we have an updated layout since this function is called
142
// in the course of running edit commands which modify the DOM.
143
// Failing to call this can result in equivalentXXXPosition calls returning
144
// incorrect results.
145
m_start.anchorNode()->document()->updateLayout();
147
// Check again, because updating layout can clear the selection.
153
// If the selection is a caret, move the range start upstream. This helps us match
154
// the conventions of text editors tested, which make style determinations based
155
// on the character before the caret, if any.
156
s = m_start.upstream().parentAnchoredEquivalent();
159
// If the selection is a range, select the minimum range that encompasses the selection.
160
// Again, this is to match the conventions of text editors tested, which make style
161
// determinations based on the first character of the selection.
162
// For instance, this operation helps to make sure that the "X" selected below is the
163
// only thing selected. The range should not be allowed to "leak" out to the end of the
164
// previous text node, or to the beginning of the next text node, each of which has a
167
// On a treasure map, <b>X</b> marks the spot.
171
s = m_start.downstream();
172
e = m_end.upstream();
173
if (comparePositions(s, e) > 0) {
174
// Make sure the start is before the end.
175
// The end can wind up before the start if collapsed whitespace is the only thing selected.
180
s = s.parentAnchoredEquivalent();
181
e = e.parentAnchoredEquivalent();
184
if (!s.containerNode() || !e.containerNode())
187
// VisibleSelections are supposed to always be valid. This constructor will ASSERT
188
// if a valid range could not be created, which is fine for this callsite.
189
return Range::create(s.anchorNode()->document(), s, e);
192
bool VisibleSelection::expandUsingGranularity(TextGranularity granularity)
197
validate(granularity);
201
static PassRefPtr<Range> makeSearchRange(const Position& pos)
203
Node* n = pos.deprecatedNode();
206
Document* d = n->document();
207
Node* de = d->documentElement();
210
Node* boundary = n->enclosingBlockFlowElement();
214
RefPtr<Range> searchRange(Range::create(d));
215
ExceptionCode ec = 0;
217
Position start(pos.parentAnchoredEquivalent());
218
searchRange->selectNodeContents(boundary, ec);
219
searchRange->setStart(start.containerNode(), start.offsetInContainerNode(), ec);
225
return searchRange.release();
228
bool VisibleSelection::isAll(EditingBoundaryCrossingRule rule) const
230
return !nonBoundaryShadowTreeRootNode() && visibleStart().previous(rule).isNull() && visibleEnd().next(rule).isNull();
233
void VisibleSelection::appendTrailingWhitespace()
235
RefPtr<Range> searchRange = makeSearchRange(m_end);
239
CharacterIterator charIt(searchRange.get(), TextIteratorEmitsCharactersBetweenAllVisiblePositions);
241
for (; charIt.length(); charIt.advance(1)) {
242
UChar c = charIt.characters()[0];
243
if ((!isSpaceOrNewline(c) && c != noBreakSpace) || c == '\n')
245
m_end = charIt.range()->endPosition();
249
void VisibleSelection::setBaseAndExtentToDeepEquivalents()
251
// Move the selection to rendered positions, if possible.
252
bool baseAndExtentEqual = m_base == m_extent;
253
if (m_base.isNotNull()) {
254
m_base = VisiblePosition(m_base, m_affinity).deepEquivalent();
255
if (baseAndExtentEqual)
258
if (m_extent.isNotNull() && !baseAndExtentEqual)
259
m_extent = VisiblePosition(m_extent, m_affinity).deepEquivalent();
261
// Make sure we do not have a dangling base or extent.
262
if (m_base.isNull() && m_extent.isNull())
263
m_baseIsFirst = true;
264
else if (m_base.isNull()) {
266
m_baseIsFirst = true;
267
} else if (m_extent.isNull()) {
269
m_baseIsFirst = true;
271
m_baseIsFirst = comparePositions(m_base, m_extent) <= 0;
274
void VisibleSelection::setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity granularity)
284
switch (granularity) {
285
case CharacterGranularity:
286
// Don't do any expansion.
288
case WordGranularity: {
289
// General case: Select the word the caret is positioned inside of, or at the start of (RightWordIfOnBoundary).
290
// Edge case: If the caret is after the last word in a soft-wrapped line or the last word in
291
// the document, select that last word (LeftWordIfOnBoundary).
292
// Edge case: If the caret is after the last word in a paragraph, select from the the end of the
293
// last word to the line break (also RightWordIfOnBoundary);
294
VisiblePosition start = VisiblePosition(m_start, m_affinity);
295
VisiblePosition originalEnd(m_end, m_affinity);
296
EWordSide side = RightWordIfOnBoundary;
297
if (isEndOfEditableOrNonEditableContent(start) || (isEndOfLine(start) && !isStartOfLine(start) && !isEndOfParagraph(start)))
298
side = LeftWordIfOnBoundary;
299
m_start = startOfWord(start, side).deepEquivalent();
300
side = RightWordIfOnBoundary;
301
if (isEndOfEditableOrNonEditableContent(originalEnd) || (isEndOfLine(originalEnd) && !isStartOfLine(originalEnd) && !isEndOfParagraph(originalEnd)))
302
side = LeftWordIfOnBoundary;
304
VisiblePosition wordEnd(endOfWord(originalEnd, side));
305
VisiblePosition end(wordEnd);
307
if (isEndOfParagraph(originalEnd) && !isEmptyTableCell(m_start.deprecatedNode())) {
308
// Select the paragraph break (the space from the end of a paragraph to the start of
309
// the next one) to match TextEdit.
310
end = wordEnd.next();
312
if (Node* table = isFirstPositionAfterTable(end)) {
313
// The paragraph break after the last paragraph in the last cell of a block table ends
314
// at the start of the paragraph after the table.
316
end = end.next(CannotCrossEditingBoundary);
326
m_end = end.deepEquivalent();
329
case SentenceGranularity: {
330
m_start = startOfSentence(VisiblePosition(m_start, m_affinity)).deepEquivalent();
331
m_end = endOfSentence(VisiblePosition(m_end, m_affinity)).deepEquivalent();
334
case LineGranularity: {
335
m_start = startOfLine(VisiblePosition(m_start, m_affinity)).deepEquivalent();
336
VisiblePosition end = endOfLine(VisiblePosition(m_end, m_affinity));
337
// If the end of this line is at the end of a paragraph, include the space
338
// after the end of the line in the selection.
339
if (isEndOfParagraph(end)) {
340
VisiblePosition next = end.next();
341
if (next.isNotNull())
344
m_end = end.deepEquivalent();
348
m_start = startOfLine(VisiblePosition(m_start, m_affinity)).deepEquivalent();
349
m_end = endOfLine(VisiblePosition(m_end, m_affinity)).deepEquivalent();
351
case ParagraphGranularity: {
352
VisiblePosition pos(m_start, m_affinity);
353
if (isStartOfLine(pos) && isEndOfEditableOrNonEditableContent(pos))
354
pos = pos.previous();
355
m_start = startOfParagraph(pos).deepEquivalent();
356
VisiblePosition visibleParagraphEnd = endOfParagraph(VisiblePosition(m_end, m_affinity));
358
// Include the "paragraph break" (the space from the end of this paragraph to the start
359
// of the next one) in the selection.
360
VisiblePosition end(visibleParagraphEnd.next());
362
if (Node* table = isFirstPositionAfterTable(end)) {
363
// The paragraph break after the last paragraph in the last cell of a block table ends
364
// at the start of the paragraph after the table, not at the position just after the table.
366
end = end.next(CannotCrossEditingBoundary);
367
// There is no parargraph break after the last paragraph in the last cell of an inline table.
369
end = visibleParagraphEnd;
373
end = visibleParagraphEnd;
375
m_end = end.deepEquivalent();
378
case DocumentBoundary:
379
m_start = startOfDocument(VisiblePosition(m_start, m_affinity)).deepEquivalent();
380
m_end = endOfDocument(VisiblePosition(m_end, m_affinity)).deepEquivalent();
382
case ParagraphBoundary:
383
m_start = startOfParagraph(VisiblePosition(m_start, m_affinity)).deepEquivalent();
384
m_end = endOfParagraph(VisiblePosition(m_end, m_affinity)).deepEquivalent();
386
case SentenceBoundary:
387
m_start = startOfSentence(VisiblePosition(m_start, m_affinity)).deepEquivalent();
388
m_end = endOfSentence(VisiblePosition(m_end, m_affinity)).deepEquivalent();
392
// Make sure we do not have a dangling start or end.
393
if (m_start.isNull())
399
void VisibleSelection::updateSelectionType()
401
if (m_start.isNull()) {
402
ASSERT(m_end.isNull());
403
m_selectionType = NoSelection;
404
} else if (m_start == m_end || m_start.upstream() == m_end.upstream()) {
405
m_selectionType = CaretSelection;
407
m_selectionType = RangeSelection;
409
// Affinity only makes sense for a caret
410
if (m_selectionType != CaretSelection)
411
m_affinity = DOWNSTREAM;
414
void VisibleSelection::validate(TextGranularity granularity)
416
setBaseAndExtentToDeepEquivalents();
417
setStartAndEndFromBaseAndExtentRespectingGranularity(granularity);
418
adjustSelectionToAvoidCrossingShadowBoundaries();
419
adjustSelectionToAvoidCrossingEditingBoundaries();
420
updateSelectionType();
422
if (selectionType() == RangeSelection) {
423
// "Constrain" the selection to be the smallest equivalent range of nodes.
424
// This is a somewhat arbitrary choice, but experience shows that it is
425
// useful to make to make the selection "canonical" (if only for
426
// purposes of comparing selections). This is an ideal point of the code
427
// to do this operation, since all selection changes that result in a RANGE
428
// come through here before anyone uses it.
429
// FIXME: Canonicalizing is good, but haven't we already done it (when we
430
// set these two positions to VisiblePosition deepEquivalent()s above)?
431
m_start = m_start.downstream();
432
m_end = m_end.upstream();
434
// FIXME: Position::downstream() or Position::upStream() might violate editing boundaries
435
// if an anchor node has a Shadow DOM. So we adjust selection to avoid crossing editing
436
// boundaries again. See https://bugs.webkit.org/show_bug.cgi?id=87463
437
adjustSelectionToAvoidCrossingEditingBoundaries();
441
// FIXME: This function breaks the invariant of this class.
442
// But because we use VisibleSelection to store values in editing commands for use when
443
// undoing the command, we need to be able to create a selection that while currently
444
// invalid, will be valid once the changes are undone. This is a design problem.
445
// To fix it we either need to change the invariants of VisibleSelection or create a new
446
// class for editing to use that can manipulate selections that are not currently valid.
447
void VisibleSelection::setWithoutValidation(const Position& base, const Position& extent)
449
ASSERT(!base.isNull());
450
ASSERT(!extent.isNull());
451
ASSERT(m_affinity == DOWNSTREAM);
454
m_baseIsFirst = comparePositions(base, extent) <= 0;
462
m_selectionType = base == extent ? CaretSelection : RangeSelection;
465
static Position adjustPositionForEnd(const Position& currentPosition, Node* startContainerNode)
467
TreeScope* treeScope = startContainerNode->treeScope();
469
ASSERT(currentPosition.containerNode()->treeScope() != treeScope);
471
if (Node* ancestor = treeScope->ancestorInThisScope(currentPosition.containerNode())) {
472
if (ancestor->contains(startContainerNode))
473
return positionAfterNode(ancestor);
474
return positionBeforeNode(ancestor);
477
if (Node* lastChild = treeScope->rootNode()->lastChild())
478
return positionAfterNode(lastChild);
483
static Position adjustPositionForStart(const Position& currentPosition, Node* endContainerNode)
485
TreeScope* treeScope = endContainerNode->treeScope();
487
ASSERT(currentPosition.containerNode()->treeScope() != treeScope);
489
if (Node* ancestor = treeScope->ancestorInThisScope(currentPosition.containerNode())) {
490
if (ancestor->contains(endContainerNode))
491
return positionBeforeNode(ancestor);
492
return positionAfterNode(ancestor);
495
if (Node* firstChild = treeScope->rootNode()->firstChild())
496
return positionBeforeNode(firstChild);
501
void VisibleSelection::adjustSelectionToAvoidCrossingShadowBoundaries()
503
if (m_base.isNull() || m_start.isNull() || m_end.isNull())
506
if (m_start.anchorNode()->treeScope() == m_end.anchorNode()->treeScope())
510
m_extent = adjustPositionForEnd(m_end, m_start.containerNode());
513
m_extent = adjustPositionForStart(m_start, m_end.containerNode());
517
ASSERT(m_start.anchorNode()->treeScope() == m_end.anchorNode()->treeScope());
520
void VisibleSelection::adjustSelectionToAvoidCrossingEditingBoundaries()
522
if (m_base.isNull() || m_start.isNull() || m_end.isNull())
525
Node* baseRoot = highestEditableRoot(m_base);
526
Node* startRoot = highestEditableRoot(m_start);
527
Node* endRoot = highestEditableRoot(m_end);
529
Node* baseEditableAncestor = lowestEditableAncestor(m_base.containerNode());
531
// The base, start and end are all in the same region. No adjustment necessary.
532
if (baseRoot == startRoot && baseRoot == endRoot)
535
// The selection is based in editable content.
537
// If the start is outside the base's editable root, cap it at the start of that root.
538
// If the start is in non-editable content that is inside the base's editable root, put it
539
// at the first editable position after start inside the base's editable root.
540
if (startRoot != baseRoot) {
541
VisiblePosition first = firstEditablePositionAfterPositionInRoot(m_start, baseRoot);
542
m_start = first.deepEquivalent();
543
if (m_start.isNull()) {
544
ASSERT_NOT_REACHED();
548
// If the end is outside the base's editable root, cap it at the end of that root.
549
// If the end is in non-editable content that is inside the base's root, put it
550
// at the last editable position before the end inside the base's root.
551
if (endRoot != baseRoot) {
552
VisiblePosition last = lastEditablePositionBeforePositionInRoot(m_end, baseRoot);
553
m_end = last.deepEquivalent();
557
// The selection is based in non-editable content.
559
// FIXME: Non-editable pieces inside editable content should be atomic, in the same way that editable
560
// pieces in non-editable content are atomic.
562
// The selection ends in editable content or non-editable content inside a different editable ancestor,
563
// move backward until non-editable content inside the same lowest editable ancestor is reached.
564
Node* endEditableAncestor = lowestEditableAncestor(m_end.containerNode());
565
if (endRoot || endEditableAncestor != baseEditableAncestor) {
567
Position p = previousVisuallyDistinctCandidate(m_end);
568
Node* shadowAncestor = endRoot ? endRoot->shadowHost() : 0;
569
if (p.isNull() && shadowAncestor)
570
p = positionAfterNode(shadowAncestor);
571
while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) {
572
Node* root = editableRootForPosition(p);
573
shadowAncestor = root ? root->shadowHost() : 0;
574
p = isAtomicNode(p.containerNode()) ? positionInParentBeforeNode(p.containerNode()) : previousVisuallyDistinctCandidate(p);
575
if (p.isNull() && shadowAncestor)
576
p = positionAfterNode(shadowAncestor);
578
VisiblePosition previous(p);
580
if (previous.isNull()) {
581
// The selection crosses an Editing boundary. This is a
582
// programmer error in the editing code. Happy debugging!
583
ASSERT_NOT_REACHED();
585
m_extent = Position();
589
m_end = previous.deepEquivalent();
592
// The selection starts in editable content or non-editable content inside a different editable ancestor,
593
// move forward until non-editable content inside the same lowest editable ancestor is reached.
594
Node* startEditableAncestor = lowestEditableAncestor(m_start.containerNode());
595
if (startRoot || startEditableAncestor != baseEditableAncestor) {
596
Position p = nextVisuallyDistinctCandidate(m_start);
597
Node* shadowAncestor = startRoot ? startRoot->shadowHost() : 0;
598
if (p.isNull() && shadowAncestor)
599
p = positionBeforeNode(shadowAncestor);
600
while (p.isNotNull() && !(lowestEditableAncestor(p.containerNode()) == baseEditableAncestor && !isEditablePosition(p))) {
601
Node* root = editableRootForPosition(p);
602
shadowAncestor = root ? root->shadowHost() : 0;
603
p = isAtomicNode(p.containerNode()) ? positionInParentAfterNode(p.containerNode()) : nextVisuallyDistinctCandidate(p);
604
if (p.isNull() && shadowAncestor)
605
p = positionBeforeNode(shadowAncestor);
607
VisiblePosition next(p);
610
// The selection crosses an Editing boundary. This is a
611
// programmer error in the editing code. Happy debugging!
612
ASSERT_NOT_REACHED();
614
m_extent = Position();
618
m_start = next.deepEquivalent();
622
// Correct the extent if necessary.
623
if (baseEditableAncestor != lowestEditableAncestor(m_extent.containerNode()))
624
m_extent = m_baseIsFirst ? m_end : m_start;
627
bool VisibleSelection::isContentEditable() const
629
return isEditablePosition(start());
632
bool VisibleSelection::rendererIsEditable() const
634
return isEditablePosition(start(), ContentIsEditable, DoNotUpdateStyle);
637
bool VisibleSelection::isContentRichlyEditable() const
639
return isRichlyEditablePosition(start());
642
Element* VisibleSelection::rootEditableElement() const
644
return editableRootForPosition(start());
647
Node* VisibleSelection::nonBoundaryShadowTreeRootNode() const
649
return start().deprecatedNode() ? start().deprecatedNode()->nonBoundaryShadowTreeRootNode() : 0;
654
void VisibleSelection::debugPosition() const
656
fprintf(stderr, "VisibleSelection ===============\n");
658
if (!m_start.anchorNode())
659
fputs("pos: null", stderr);
660
else if (m_start == m_end) {
661
fprintf(stderr, "pos: %s ", m_start.anchorNode()->nodeName().utf8().data());
662
m_start.showAnchorTypeAndOffset();
664
fprintf(stderr, "start: %s ", m_start.anchorNode()->nodeName().utf8().data());
665
m_start.showAnchorTypeAndOffset();
666
fprintf(stderr, "end: %s ", m_end.anchorNode()->nodeName().utf8().data());
667
m_end.showAnchorTypeAndOffset();
670
fprintf(stderr, "================================\n");
673
void VisibleSelection::formatForDebugger(char* buffer, unsigned length) const
675
StringBuilder result;
679
result.appendLiteral("<none>");
681
const int FormatBufferSize = 1024;
682
char s[FormatBufferSize];
683
result.appendLiteral("from ");
684
start().formatForDebugger(s, FormatBufferSize);
686
result.appendLiteral(" to ");
687
end().formatForDebugger(s, FormatBufferSize);
691
strncpy(buffer, result.toString().utf8().data(), length - 1);
694
void VisibleSelection::showTreeForThis() const
696
if (start().anchorNode()) {
697
start().anchorNode()->showTreeAndMark(start().anchorNode(), "S", end().anchorNode(), "E");
698
fputs("start: ", stderr);
699
start().showAnchorTypeAndOffset();
700
fputs("end: ", stderr);
701
end().showAnchorTypeAndOffset();
707
} // namespace WebCore
711
void showTree(const WebCore::VisibleSelection& sel)
713
sel.showTreeForThis();
716
void showTree(const WebCore::VisibleSelection* sel)
719
sel->showTreeForThis();