• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #include "config.h"
27 #include "VisiblePosition.h"
28 
29 #include "CString.h"
30 #include "Document.h"
31 #include "Element.h"
32 #include "FloatQuad.h"
33 #include "HTMLNames.h"
34 #include "InlineTextBox.h"
35 #include "Logging.h"
36 #include "Range.h"
37 #include "Text.h"
38 #include "htmlediting.h"
39 #include "visible_units.h"
40 #include <stdio.h>
41 
42 namespace WebCore {
43 
44 using namespace HTMLNames;
45 
VisiblePosition(const Position & pos,EAffinity affinity)46 VisiblePosition::VisiblePosition(const Position &pos, EAffinity affinity)
47 {
48     init(pos, affinity);
49 }
50 
VisiblePosition(Node * node,int offset,EAffinity affinity)51 VisiblePosition::VisiblePosition(Node *node, int offset, EAffinity affinity)
52 {
53     ASSERT(offset >= 0);
54     init(Position(node, offset), affinity);
55 }
56 
init(const Position & position,EAffinity affinity)57 void VisiblePosition::init(const Position& position, EAffinity affinity)
58 {
59     m_affinity = affinity;
60 
61     m_deepPosition = canonicalPosition(position);
62 
63     // When not at a line wrap, make sure to end up with DOWNSTREAM affinity.
64     if (m_affinity == UPSTREAM && (isNull() || inSameLine(VisiblePosition(position, DOWNSTREAM), *this)))
65         m_affinity = DOWNSTREAM;
66 }
67 
next(bool stayInEditableContent) const68 VisiblePosition VisiblePosition::next(bool stayInEditableContent) const
69 {
70     VisiblePosition next(nextVisuallyDistinctCandidate(m_deepPosition), m_affinity);
71 
72     if (!stayInEditableContent)
73         return next;
74 
75     return honorEditableBoundaryAtOrAfter(next);
76 }
77 
previous(bool stayInEditableContent) const78 VisiblePosition VisiblePosition::previous(bool stayInEditableContent) const
79 {
80     // find first previous DOM position that is visible
81     Position pos = previousVisuallyDistinctCandidate(m_deepPosition);
82 
83     // return null visible position if there is no previous visible position
84     if (pos.atStart())
85         return VisiblePosition();
86 
87     VisiblePosition prev = VisiblePosition(pos, DOWNSTREAM);
88     ASSERT(prev != *this);
89 
90 #ifndef NDEBUG
91     // we should always be able to make the affinity DOWNSTREAM, because going previous from an
92     // UPSTREAM position can never yield another UPSTREAM position (unless line wrap length is 0!).
93     if (prev.isNotNull() && m_affinity == UPSTREAM) {
94         VisiblePosition temp = prev;
95         temp.setAffinity(UPSTREAM);
96         ASSERT(inSameLine(temp, prev));
97     }
98 #endif
99 
100     if (!stayInEditableContent)
101         return prev;
102 
103     return honorEditableBoundaryAtOrBefore(prev);
104 }
105 
leftVisuallyDistinctCandidate() const106 Position VisiblePosition::leftVisuallyDistinctCandidate() const
107 {
108     Position p = m_deepPosition;
109     if (!p.node())
110         return Position();
111 
112     Position downstreamStart = p.downstream();
113     TextDirection primaryDirection = LTR;
114     for (RenderObject* r = p.node()->renderer(); r; r = r->parent()) {
115         if (r->isBlockFlow()) {
116             primaryDirection = r->style()->direction();
117             break;
118         }
119     }
120 
121     while (true) {
122         InlineBox* box;
123         int offset;
124         p.getInlineBoxAndOffset(m_affinity, primaryDirection, box, offset);
125         if (!box)
126             return primaryDirection == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
127 
128         RenderObject* renderer = box->object();
129 
130         while (true) {
131             if ((renderer->isReplaced() || renderer->isBR()) && offset == box->caretRightmostOffset())
132                 return box->direction() == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
133 
134             offset = box->direction() == LTR ? renderer->previousOffset(offset) : renderer->nextOffset(offset);
135 
136             int caretMinOffset = box->caretMinOffset();
137             int caretMaxOffset = box->caretMaxOffset();
138 
139             if (offset > caretMinOffset && offset < caretMaxOffset)
140                 break;
141 
142             if (box->direction() == LTR ? offset < caretMinOffset : offset > caretMaxOffset) {
143                 // Overshot to the left.
144                 InlineBox* prevBox = box->prevLeafChild();
145                 if (!prevBox)
146                     return primaryDirection == LTR ? previousVisuallyDistinctCandidate(m_deepPosition) : nextVisuallyDistinctCandidate(m_deepPosition);
147 
148                 // Reposition at the other logical position corresponding to our edge's visual position and go for another round.
149                 box = prevBox;
150                 renderer = box->object();
151                 offset = prevBox->caretRightmostOffset();
152                 continue;
153             }
154 
155             ASSERT(offset == box->caretLeftmostOffset());
156 
157             unsigned char level = box->bidiLevel();
158             InlineBox* prevBox = box->prevLeafChild();
159 
160             if (box->direction() == primaryDirection) {
161                 if (!prevBox || prevBox->bidiLevel() >= level)
162                     break;
163 
164                 level = prevBox->bidiLevel();
165 
166                 InlineBox* nextBox = box;
167                 do {
168                     nextBox = nextBox->nextLeafChild();
169                 } while (nextBox && nextBox->bidiLevel() > level);
170 
171                 if (nextBox && nextBox->bidiLevel() == level)
172                     break;
173 
174                 while (InlineBox* prevBox = box->prevLeafChild()) {
175                     if (prevBox->bidiLevel() < level)
176                         break;
177                     box = prevBox;
178                 }
179                 renderer = box->object();
180                 offset = box->caretRightmostOffset();
181                 if (box->direction() == primaryDirection)
182                     break;
183                 continue;
184             }
185 
186             if (prevBox) {
187                 box = prevBox;
188                 renderer = box->object();
189                 offset = box->caretRightmostOffset();
190                 if (box->bidiLevel() > level) {
191                     do {
192                         prevBox = box->prevLeafChild();
193                     } while (prevBox && prevBox->bidiLevel() > level);
194 
195                     if (!prevBox || prevBox->bidiLevel() < level)
196                         continue;
197                 }
198             } else {
199                 // Trailing edge of a secondary run. Set to the leading edge of the entire run.
200                 while (true) {
201                     while (InlineBox* nextBox = box->nextLeafChild()) {
202                         if (nextBox->bidiLevel() < level)
203                             break;
204                         box = nextBox;
205                     }
206                     if (box->bidiLevel() == level)
207                         break;
208                     level = box->bidiLevel();
209                     while (InlineBox* prevBox = box->prevLeafChild()) {
210                         if (prevBox->bidiLevel() < level)
211                             break;
212                         box = prevBox;
213                     }
214                     if (box->bidiLevel() == level)
215                         break;
216                     level = box->bidiLevel();
217                 }
218                 renderer = box->object();
219                 offset = primaryDirection == LTR ? box->caretMinOffset() : box->caretMaxOffset();
220             }
221             break;
222         }
223 
224         p = Position(renderer->element(), offset);
225 
226         if (p.isCandidate() && p.downstream() != downstreamStart || p.atStart() || p.atEnd())
227             return p;
228     }
229 }
230 
left(bool stayInEditableContent) const231 VisiblePosition VisiblePosition::left(bool stayInEditableContent) const
232 {
233     Position pos = leftVisuallyDistinctCandidate();
234     if (pos.atStart() || pos.atEnd())
235         return VisiblePosition();
236 
237     VisiblePosition left = VisiblePosition(pos, DOWNSTREAM);
238     ASSERT(left != *this);
239 
240     if (!stayInEditableContent)
241         return left;
242 
243     // FIXME: This may need to do something different from "before".
244     return honorEditableBoundaryAtOrBefore(left);
245 }
246 
rightVisuallyDistinctCandidate() const247 Position VisiblePosition::rightVisuallyDistinctCandidate() const
248 {
249     Position p = m_deepPosition;
250     if (!p.node())
251         return Position();
252 
253     Position downstreamStart = p.downstream();
254     TextDirection primaryDirection = LTR;
255     for (RenderObject* r = p.node()->renderer(); r; r = r->parent()) {
256         if (r->isBlockFlow()) {
257             primaryDirection = r->style()->direction();
258             break;
259         }
260     }
261 
262     while (true) {
263         InlineBox* box;
264         int offset;
265         p.getInlineBoxAndOffset(m_affinity, primaryDirection, box, offset);
266         if (!box)
267             return primaryDirection == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
268 
269         RenderObject* renderer = box->object();
270 
271         while (true) {
272             if ((renderer->isReplaced() || renderer->isBR()) && offset == box->caretLeftmostOffset())
273                 return box->direction() == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
274 
275             offset = box->direction() == LTR ? renderer->nextOffset(offset) : renderer->previousOffset(offset);
276 
277             int caretMinOffset = box->caretMinOffset();
278             int caretMaxOffset = box->caretMaxOffset();
279 
280             if (offset > caretMinOffset && offset < caretMaxOffset)
281                 break;
282 
283             if (box->direction() == LTR ? offset > caretMaxOffset : offset < caretMinOffset) {
284                 // Overshot to the right.
285                 InlineBox* nextBox = box->nextLeafChild();
286                 if (!nextBox)
287                     return primaryDirection == LTR ? nextVisuallyDistinctCandidate(m_deepPosition) : previousVisuallyDistinctCandidate(m_deepPosition);
288 
289                 // Reposition at the other logical position corresponding to our edge's visual position and go for another round.
290                 box = nextBox;
291                 renderer = box->object();
292                 offset = nextBox->caretLeftmostOffset();
293                 continue;
294             }
295 
296             ASSERT(offset == box->caretRightmostOffset());
297 
298             unsigned char level = box->bidiLevel();
299             InlineBox* nextBox = box->nextLeafChild();
300 
301             if (box->direction() == primaryDirection) {
302                 if (!nextBox || nextBox->bidiLevel() >= level)
303                     break;
304 
305                 level = nextBox->bidiLevel();
306 
307                 InlineBox* prevBox = box;
308                 do {
309                     prevBox = prevBox->prevLeafChild();
310                 } while (prevBox && prevBox->bidiLevel() > level);
311 
312                 if (prevBox && prevBox->bidiLevel() == level)   // For example, abc FED 123 ^ CBA
313                     break;
314 
315                 // For example, abc 123 ^ CBA
316                 while (InlineBox* nextBox = box->nextLeafChild()) {
317                     if (nextBox->bidiLevel() < level)
318                         break;
319                     box = nextBox;
320                 }
321                 renderer = box->object();
322                 offset = box->caretLeftmostOffset();
323                 if (box->direction() == primaryDirection)
324                     break;
325                 continue;
326             }
327 
328             if (nextBox) {
329                 box = nextBox;
330                 renderer = box->object();
331                 offset = box->caretLeftmostOffset();
332                 if (box->bidiLevel() > level) {
333                     do {
334                         nextBox = box->nextLeafChild();
335                     } while (nextBox && nextBox->bidiLevel() > level);
336 
337                     if (!nextBox || nextBox->bidiLevel() < level)
338                         continue;
339                 }
340             } else {
341                 // Trailing edge of a secondary run. Set to the leading edge of the entire run.
342                 while (true) {
343                     while (InlineBox* prevBox = box->prevLeafChild()) {
344                         if (prevBox->bidiLevel() < level)
345                             break;
346                         box = prevBox;
347                     }
348                     if (box->bidiLevel() == level)
349                         break;
350                     level = box->bidiLevel();
351                     while (InlineBox* nextBox = box->nextLeafChild()) {
352                         if (nextBox->bidiLevel() < level)
353                             break;
354                         box = nextBox;
355                     }
356                     if (box->bidiLevel() == level)
357                         break;
358                     level = box->bidiLevel();
359                 }
360                 renderer = box->object();
361                 offset = primaryDirection == LTR ? box->caretMaxOffset() : box->caretMinOffset();
362             }
363             break;
364         }
365 
366         p = Position(renderer->element(), offset);
367 
368         if (p.isCandidate() && p.downstream() != downstreamStart || p.atStart() || p.atEnd())
369             return p;
370     }
371 }
372 
right(bool stayInEditableContent) const373 VisiblePosition VisiblePosition::right(bool stayInEditableContent) const
374 {
375     Position pos = rightVisuallyDistinctCandidate();
376     if (pos.atStart() || pos.atEnd())
377         return VisiblePosition();
378 
379     VisiblePosition right = VisiblePosition(pos, DOWNSTREAM);
380     ASSERT(right != *this);
381 
382     if (!stayInEditableContent)
383         return right;
384 
385     // FIXME: This may need to do something different from "after".
386     return honorEditableBoundaryAtOrAfter(right);
387 }
388 
honorEditableBoundaryAtOrBefore(const VisiblePosition & pos) const389 VisiblePosition VisiblePosition::honorEditableBoundaryAtOrBefore(const VisiblePosition &pos) const
390 {
391     if (pos.isNull())
392         return pos;
393 
394     Node* highestRoot = highestEditableRoot(deepEquivalent());
395 
396     // Return empty position if pos is not somewhere inside the editable region containing this position
397     if (highestRoot && !pos.deepEquivalent().node()->isDescendantOf(highestRoot))
398         return VisiblePosition();
399 
400     // Return pos itself if the two are from the very same editable region, or both are non-editable
401     // FIXME: In the non-editable case, just because the new position is non-editable doesn't mean movement
402     // to it is allowed.  Selection::adjustForEditableContent has this problem too.
403     if (highestEditableRoot(pos.deepEquivalent()) == highestRoot)
404         return pos;
405 
406     // Return empty position if this position is non-editable, but pos is editable
407     // FIXME: Move to the previous non-editable region.
408     if (!highestRoot)
409         return VisiblePosition();
410 
411     // Return the last position before pos that is in the same editable region as this position
412     return lastEditablePositionBeforePositionInRoot(pos.deepEquivalent(), highestRoot);
413 }
414 
honorEditableBoundaryAtOrAfter(const VisiblePosition & pos) const415 VisiblePosition VisiblePosition::honorEditableBoundaryAtOrAfter(const VisiblePosition &pos) const
416 {
417     if (pos.isNull())
418         return pos;
419 
420     Node* highestRoot = highestEditableRoot(deepEquivalent());
421 
422     // Return empty position if pos is not somewhere inside the editable region containing this position
423     if (highestRoot && !pos.deepEquivalent().node()->isDescendantOf(highestRoot))
424         return VisiblePosition();
425 
426     // Return pos itself if the two are from the very same editable region, or both are non-editable
427     // FIXME: In the non-editable case, just because the new position is non-editable doesn't mean movement
428     // to it is allowed.  Selection::adjustForEditableContent has this problem too.
429     if (highestEditableRoot(pos.deepEquivalent()) == highestRoot)
430         return pos;
431 
432     // Return empty position if this position is non-editable, but pos is editable
433     // FIXME: Move to the next non-editable region.
434     if (!highestRoot)
435         return VisiblePosition();
436 
437     // Return the next position after pos that is in the same editable region as this position
438     return firstEditablePositionAfterPositionInRoot(pos.deepEquivalent(), highestRoot);
439 }
440 
canonicalizeCandidate(const Position & candidate)441 static Position canonicalizeCandidate(const Position& candidate)
442 {
443     if (candidate.isNull())
444         return Position();
445     ASSERT(candidate.isCandidate());
446     Position upstream = candidate.upstream();
447     if (upstream.isCandidate())
448         return upstream;
449     return candidate;
450 }
451 
canonicalPosition(const Position & position)452 Position VisiblePosition::canonicalPosition(const Position& position)
453 {
454     // FIXME (9535):  Canonicalizing to the leftmost candidate means that if we're at a line wrap, we will
455     // ask renderers to paint downstream carets for other renderers.
456     // To fix this, we need to either a) add code to all paintCarets to pass the responsibility off to
457     // the appropriate renderer for VisiblePosition's like these, or b) canonicalize to the rightmost candidate
458     // unless the affinity is upstream.
459     Node* node = position.node();
460     if (!node)
461         return Position();
462 
463     node->document()->updateLayoutIgnorePendingStylesheets();
464 
465     Position candidate = position.upstream();
466     if (candidate.isCandidate())
467         return candidate;
468     candidate = position.downstream();
469     if (candidate.isCandidate())
470         return candidate;
471 
472     // When neither upstream or downstream gets us to a candidate (upstream/downstream won't leave
473     // blocks or enter new ones), we search forward and backward until we find one.
474     Position next = canonicalizeCandidate(nextCandidate(position));
475     Position prev = canonicalizeCandidate(previousCandidate(position));
476     Node* nextNode = next.node();
477     Node* prevNode = prev.node();
478 
479     // The new position must be in the same editable element. Enforce that first.
480     // Unless the descent is from a non-editable html element to an editable body.
481     if (node->hasTagName(htmlTag) && !node->isContentEditable())
482         return next.isNotNull() ? next : prev;
483 
484     Node* editingRoot = editableRootForPosition(position);
485 
486     // If the html element is editable, descending into its body will look like a descent
487     // from non-editable to editable content since rootEditableElement() always stops at the body.
488     if (editingRoot && editingRoot->hasTagName(htmlTag) || position.node()->isDocumentNode())
489         return next.isNotNull() ? next : prev;
490 
491     bool prevIsInSameEditableElement = prevNode && editableRootForPosition(prev) == editingRoot;
492     bool nextIsInSameEditableElement = nextNode && editableRootForPosition(next) == editingRoot;
493     if (prevIsInSameEditableElement && !nextIsInSameEditableElement)
494         return prev;
495 
496     if (nextIsInSameEditableElement && !prevIsInSameEditableElement)
497         return next;
498 
499     if (!nextIsInSameEditableElement && !prevIsInSameEditableElement)
500         return Position();
501 
502     // The new position should be in the same block flow element. Favor that.
503     Node *originalBlock = node->enclosingBlockFlowElement();
504     bool nextIsOutsideOriginalBlock = !nextNode->isDescendantOf(originalBlock) && nextNode != originalBlock;
505     bool prevIsOutsideOriginalBlock = !prevNode->isDescendantOf(originalBlock) && prevNode != originalBlock;
506     if (nextIsOutsideOriginalBlock && !prevIsOutsideOriginalBlock)
507         return prev;
508 
509     return next;
510 }
511 
characterAfter() const512 UChar VisiblePosition::characterAfter() const
513 {
514     // We canonicalize to the first of two equivalent candidates, but the second of the two candidates
515     // is the one that will be inside the text node containing the character after this visible position.
516     Position pos = m_deepPosition.downstream();
517     Node* node = pos.node();
518     if (!node || !node->isTextNode())
519         return 0;
520     Text* textNode = static_cast<Text*>(pos.node());
521     int offset = pos.offset();
522     if ((unsigned)offset >= textNode->length())
523         return 0;
524     return textNode->data()[offset];
525 }
526 
localCaretRect(RenderObject * & renderer) const527 IntRect VisiblePosition::localCaretRect(RenderObject*& renderer) const
528 {
529     Node* node = m_deepPosition.node();
530     if (!node) {
531         renderer = 0;
532         return IntRect();
533     }
534 
535     renderer = node->renderer();
536     if (!renderer)
537         return IntRect();
538 
539     InlineBox* inlineBox;
540     int caretOffset;
541     getInlineBoxAndOffset(inlineBox, caretOffset);
542 
543     if (inlineBox)
544         renderer = inlineBox->object();
545 
546     return renderer->localCaretRect(inlineBox, caretOffset);
547 }
548 
absoluteCaretBounds() const549 IntRect VisiblePosition::absoluteCaretBounds() const
550 {
551     RenderObject* renderer;
552     IntRect localRect = localCaretRect(renderer);
553     if (localRect.isEmpty() || !renderer)
554         return IntRect();
555 
556     return renderer->localToAbsoluteQuad(FloatRect(localRect)).enclosingBoundingBox();
557 }
558 
xOffsetForVerticalNavigation() const559 int VisiblePosition::xOffsetForVerticalNavigation() const
560 {
561     RenderObject* renderer;
562     IntRect localRect = localCaretRect(renderer);
563     if (localRect.isEmpty() || !renderer)
564         return 0;
565 
566     // This ignores transforms on purpose, for now. Vertical navigation is done
567     // without consulting transforms, so that 'up' in transformed text is 'up'
568     // relative to the text, not absolute 'up'.
569     return renderer->localToAbsolute(localRect.location()).x();
570 }
571 
debugPosition(const char * msg) const572 void VisiblePosition::debugPosition(const char* msg) const
573 {
574     if (isNull())
575         fprintf(stderr, "Position [%s]: null\n", msg);
576     else
577         fprintf(stderr, "Position [%s]: %s [%p] at %d\n", msg, m_deepPosition.node()->nodeName().utf8().data(), m_deepPosition.node(), m_deepPosition.offset());
578 }
579 
580 #ifndef NDEBUG
581 
formatForDebugger(char * buffer,unsigned length) const582 void VisiblePosition::formatForDebugger(char* buffer, unsigned length) const
583 {
584     m_deepPosition.formatForDebugger(buffer, length);
585 }
586 
showTreeForThis() const587 void VisiblePosition::showTreeForThis() const
588 {
589     m_deepPosition.showTreeForThis();
590 }
591 
592 #endif
593 
makeRange(const VisiblePosition & start,const VisiblePosition & end)594 PassRefPtr<Range> makeRange(const VisiblePosition &start, const VisiblePosition &end)
595 {
596     if (start.isNull() || end.isNull())
597         return 0;
598 
599     Position s = rangeCompliantEquivalent(start);
600     Position e = rangeCompliantEquivalent(end);
601     return Range::create(s.node()->document(), s.node(), s.offset(), e.node(), e.offset());
602 }
603 
startVisiblePosition(const Range * r,EAffinity affinity)604 VisiblePosition startVisiblePosition(const Range *r, EAffinity affinity)
605 {
606     int exception = 0;
607     return VisiblePosition(r->startContainer(exception), r->startOffset(exception), affinity);
608 }
609 
endVisiblePosition(const Range * r,EAffinity affinity)610 VisiblePosition endVisiblePosition(const Range *r, EAffinity affinity)
611 {
612     int exception = 0;
613     return VisiblePosition(r->endContainer(exception), r->endOffset(exception), affinity);
614 }
615 
setStart(Range * r,const VisiblePosition & visiblePosition)616 bool setStart(Range *r, const VisiblePosition &visiblePosition)
617 {
618     if (!r)
619         return false;
620     Position p = rangeCompliantEquivalent(visiblePosition);
621     int code = 0;
622     r->setStart(p.node(), p.offset(), code);
623     return code == 0;
624 }
625 
setEnd(Range * r,const VisiblePosition & visiblePosition)626 bool setEnd(Range *r, const VisiblePosition &visiblePosition)
627 {
628     if (!r)
629         return false;
630     Position p = rangeCompliantEquivalent(visiblePosition);
631     int code = 0;
632     r->setEnd(p.node(), p.offset(), code);
633     return code == 0;
634 }
635 
enclosingBlockFlowElement(const VisiblePosition & visiblePosition)636 Node *enclosingBlockFlowElement(const VisiblePosition &visiblePosition)
637 {
638     if (visiblePosition.isNull())
639         return NULL;
640 
641     return visiblePosition.deepEquivalent().node()->enclosingBlockFlowElement();
642 }
643 
isFirstVisiblePositionInNode(const VisiblePosition & visiblePosition,const Node * node)644 bool isFirstVisiblePositionInNode(const VisiblePosition &visiblePosition, const Node *node)
645 {
646     if (visiblePosition.isNull())
647         return false;
648 
649     if (!visiblePosition.deepEquivalent().node()->isDescendantOf(node))
650         return false;
651 
652     VisiblePosition previous = visiblePosition.previous();
653     return previous.isNull() || !previous.deepEquivalent().node()->isDescendantOf(node);
654 }
655 
isLastVisiblePositionInNode(const VisiblePosition & visiblePosition,const Node * node)656 bool isLastVisiblePositionInNode(const VisiblePosition &visiblePosition, const Node *node)
657 {
658     if (visiblePosition.isNull())
659         return false;
660 
661     if (!visiblePosition.deepEquivalent().node()->isDescendantOf(node))
662         return false;
663 
664     VisiblePosition next = visiblePosition.next();
665     return next.isNull() || !next.deepEquivalent().node()->isDescendantOf(node);
666 }
667 
668 }  // namespace WebCore
669 
670 #ifndef NDEBUG
671 
showTree(const WebCore::VisiblePosition * vpos)672 void showTree(const WebCore::VisiblePosition* vpos)
673 {
674     if (vpos)
675         vpos->showTreeForThis();
676 }
677 
showTree(const WebCore::VisiblePosition & vpos)678 void showTree(const WebCore::VisiblePosition& vpos)
679 {
680     vpos.showTreeForThis();
681 }
682 
683 #endif
684