webengine/osswebengine/WebCore/dom/Position.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2004, 2005, 2006 Apple Computer, 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 "Position.h"
       
    28 
       
    29 #include "CharacterNames.h"
       
    30 #include "Document.h"
       
    31 #include "Element.h"
       
    32 #include "Logging.h"
       
    33 #include "RenderBlock.h"
       
    34 #include "CSSComputedStyleDeclaration.h"
       
    35 #include "htmlediting.h"
       
    36 #include "HTMLNames.h"
       
    37 #include "PositionIterator.h"
       
    38 #include "Text.h"
       
    39 #include "TextIterator.h"
       
    40 #include "visible_units.h"
       
    41   
       
    42 namespace WebCore {
       
    43 
       
    44 using namespace HTMLNames;
       
    45 
       
    46 static Node *nextRenderedEditable(Node *node)
       
    47 {
       
    48     while (1) {
       
    49         node = node->nextEditable();
       
    50         if (!node)
       
    51             return 0;
       
    52         if (!node->renderer())
       
    53             continue;
       
    54         if (node->renderer()->inlineBox(0))
       
    55             return node;
       
    56     }
       
    57     return 0;
       
    58 }
       
    59 
       
    60 static Node *previousRenderedEditable(Node *node)
       
    61 {
       
    62     while (1) {
       
    63         node = node->previousEditable();
       
    64         if (!node)
       
    65             return 0;
       
    66         if (!node->renderer())
       
    67             continue;
       
    68         if (node->renderer()->inlineBox(0))
       
    69             return node;
       
    70     }
       
    71     return 0;
       
    72 }
       
    73 
       
    74 Position::Position(Node* node, int offset) 
       
    75     : m_node(node)
       
    76     , m_offset(offset) 
       
    77 {
       
    78 }
       
    79 
       
    80 Position::Position(const PositionIterator& it)
       
    81     : m_node(it.m_parent)
       
    82     , m_offset(it.m_child ? it.m_child->nodeIndex() : (it.m_parent->hasChildNodes() ? maxDeepOffset(it.m_parent) : it.m_offset))
       
    83 {
       
    84 }
       
    85 
       
    86 void Position::clear()
       
    87 {
       
    88     m_node = 0;
       
    89     m_offset = 0;
       
    90 }
       
    91 
       
    92 Element* Position::documentElement() const
       
    93 {
       
    94     if (Node* n = node())
       
    95         if (Element* e = n->document()->documentElement())
       
    96             return e;
       
    97     return 0;
       
    98 }
       
    99 
       
   100 Element *Position::element() const
       
   101 {
       
   102     Node *n;
       
   103     for (n = node(); n && !n->isElementNode(); n = n->parentNode())
       
   104         ; // empty loop body
       
   105     return static_cast<Element *>(n);
       
   106 }
       
   107 
       
   108 PassRefPtr<CSSComputedStyleDeclaration> Position::computedStyle() const
       
   109 {
       
   110     Element *elem = element();
       
   111     if (!elem)
       
   112         return 0;
       
   113     return new CSSComputedStyleDeclaration(elem);
       
   114 }
       
   115 
       
   116 Position Position::previous(EUsingComposedCharacters usingComposedCharacters) const
       
   117 {
       
   118     Node *n = node();
       
   119     if (!n)
       
   120         return *this;
       
   121     
       
   122     int o = offset();
       
   123     // FIXME: Negative offsets shouldn't be allowed. We should catch this earlier.
       
   124     ASSERT(o >= 0);
       
   125 
       
   126     if (o > 0) {
       
   127         Node *child = n->childNode(o - 1);
       
   128         if (child) {
       
   129             return Position(child, maxDeepOffset(child));
       
   130         }
       
   131         // There are two reasons child might be 0:
       
   132         //   1) The node is node like a text node that is not an element, and therefore has no children.
       
   133         //      Going backward one character at a time is correct.
       
   134         //   2) The old offset was a bogus offset like (<br>, 1), and there is no child.
       
   135         //      Going from 1 to 0 is correct.
       
   136         return Position(n, usingComposedCharacters ? n->previousOffset(o) : o - 1);
       
   137     }
       
   138 
       
   139     Node *parent = n->parentNode();
       
   140     if (!parent)
       
   141         return *this;
       
   142 
       
   143     return Position(parent, n->nodeIndex());
       
   144 }
       
   145 
       
   146 Position Position::next(EUsingComposedCharacters usingComposedCharacters) const
       
   147 {
       
   148     Node *n = node();
       
   149     if (!n)
       
   150         return *this;
       
   151     
       
   152     int o = offset();
       
   153     // FIXME: Negative offsets shouldn't be allowed. We should catch this earlier.
       
   154     ASSERT(o >= 0);
       
   155 
       
   156     Node* child = n->childNode(o);
       
   157     if (child || !n->hasChildNodes() && o < maxDeepOffset(n)) {
       
   158         if (child)
       
   159             return Position(child, 0);
       
   160             
       
   161         // There are two reasons child might be 0:
       
   162         //   1) The node is node like a text node that is not an element, and therefore has no children.
       
   163         //      Going forward one character at a time is correct.
       
   164         //   2) The new offset is a bogus offset like (<br>, 1), and there is no child.
       
   165         //      Going from 0 to 1 is correct.
       
   166         return Position(n, usingComposedCharacters ? n->nextOffset(o) : o + 1);
       
   167     }
       
   168 
       
   169     Node *parent = n->parentNode();
       
   170     if (!parent)
       
   171         return *this;
       
   172 
       
   173     return Position(parent, n->nodeIndex() + 1);
       
   174 }
       
   175 
       
   176 bool Position::atStart() const
       
   177 {
       
   178     Node *n = node();
       
   179     if (!n)
       
   180         return true;
       
   181     
       
   182     return offset() <= 0 && n->parent() == 0;
       
   183 }
       
   184 
       
   185 bool Position::atEnd() const
       
   186 {
       
   187     Node *n = node();
       
   188     if (!n)
       
   189         return true;
       
   190     
       
   191     return n->parent() == 0 && offset() >= maxDeepOffset(n);
       
   192 }
       
   193 
       
   194 int Position::renderedOffset() const
       
   195 {
       
   196     if (!node()->isTextNode())
       
   197         return offset();
       
   198    
       
   199     if (!node()->renderer())
       
   200         return offset();
       
   201                     
       
   202     int result = 0;
       
   203     RenderText *textRenderer = static_cast<RenderText *>(node()->renderer());
       
   204     for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
       
   205         int start = box->m_start;
       
   206         int end = box->m_start + box->m_len;
       
   207         if (offset() < start)
       
   208             return result;
       
   209         if (offset() <= end) {
       
   210             result += offset() - start;
       
   211             return result;
       
   212         }
       
   213         result += box->m_len;
       
   214     }
       
   215     return result;
       
   216 }
       
   217 
       
   218 // return first preceding DOM position rendered at a different location, or "this"
       
   219 Position Position::previousCharacterPosition(EAffinity affinity) const
       
   220 {
       
   221     if (isNull())
       
   222         return Position();
       
   223 
       
   224     Node *fromRootEditableElement = node()->rootEditableElement();
       
   225 
       
   226     bool atStartOfLine = isStartOfLine(VisiblePosition(*this, affinity));
       
   227     bool rendered = isCandidate();
       
   228     
       
   229     Position currentPos = *this;
       
   230     while (!currentPos.atStart()) {
       
   231         currentPos = currentPos.previous();
       
   232 
       
   233         if (currentPos.node()->rootEditableElement() != fromRootEditableElement)
       
   234             return *this;
       
   235 
       
   236         if (atStartOfLine || !rendered) {
       
   237             if (currentPos.isCandidate())
       
   238                 return currentPos;
       
   239         } else if (rendersInDifferentPosition(currentPos))
       
   240             return currentPos;
       
   241     }
       
   242     
       
   243     return *this;
       
   244 }
       
   245 
       
   246 // return first following position rendered at a different location, or "this"
       
   247 Position Position::nextCharacterPosition(EAffinity affinity) const
       
   248 {
       
   249     if (isNull())
       
   250         return Position();
       
   251 
       
   252     Node *fromRootEditableElement = node()->rootEditableElement();
       
   253 
       
   254     bool atEndOfLine = isEndOfLine(VisiblePosition(*this, affinity));
       
   255     bool rendered = isCandidate();
       
   256     
       
   257     Position currentPos = *this;
       
   258     while (!currentPos.atEnd()) {
       
   259         currentPos = currentPos.next();
       
   260 
       
   261         if (currentPos.node()->rootEditableElement() != fromRootEditableElement)
       
   262             return *this;
       
   263 
       
   264         if (atEndOfLine || !rendered) {
       
   265             if (currentPos.isCandidate())
       
   266                 return currentPos;
       
   267         } else if (rendersInDifferentPosition(currentPos))
       
   268             return currentPos;
       
   269     }
       
   270     
       
   271     return *this;
       
   272 }
       
   273 
       
   274 // upstream() and downstream() want to return positions that are either in a
       
   275 // text node or at just before a non-text node.  This method checks for that.
       
   276 static bool isStreamer(const PositionIterator& pos)
       
   277 {
       
   278     if (!pos.node())
       
   279         return true;
       
   280         
       
   281     if (isAtomicNode(pos.node()))
       
   282         return true;
       
   283         
       
   284     return pos.atStartOfNode();
       
   285 }
       
   286 
       
   287 // p.upstream() returns the start of the range of positions that map to the same VisiblePosition as P.
       
   288 Position Position::upstream() const
       
   289 {
       
   290     Node* startNode = node();
       
   291     if (!startNode)
       
   292         return Position();
       
   293     
       
   294     // iterate backward from there, looking for a qualified position
       
   295     Node* block = enclosingBlock(startNode);
       
   296     PositionIterator lastVisible = *this;
       
   297     PositionIterator currentPos = lastVisible;
       
   298     Node* originalRoot = node()->rootEditableElement();
       
   299     for (; !currentPos.atStart(); currentPos.decrement()) {
       
   300         Node* currentNode = currentPos.node();
       
   301         
       
   302         if (currentNode->rootEditableElement() != originalRoot)
       
   303             break;
       
   304 
       
   305         // Don't enter a new enclosing block flow or table element.  There is code below that
       
   306         // terminates early if we're about to leave an enclosing block flow or table element.
       
   307         if (block != enclosingBlock(currentNode))
       
   308             return lastVisible;
       
   309 
       
   310         // skip position in unrendered or invisible node
       
   311         RenderObject* renderer = currentNode->renderer();
       
   312         if (!renderer || renderer->style()->visibility() != VISIBLE)
       
   313             continue;
       
   314                  
       
   315         // track last visible streamer position
       
   316         if (isStreamer(currentPos))
       
   317             lastVisible = currentPos;
       
   318         
       
   319         // Don't leave a block flow or table element.  We could rely on code above to terminate and 
       
   320         // return lastVisible on the next iteration, but we terminate early.
       
   321         if (currentNode == enclosingBlock(currentNode) && currentPos.atStartOfNode())
       
   322             return lastVisible;
       
   323             
       
   324         // Return position after brs, tables, and nodes which have content that can be ignored.
       
   325         if (editingIgnoresContent(currentNode) || renderer->isBR() || isTableElement(currentNode)) {
       
   326             if (currentPos.atEndOfNode())
       
   327                 return Position(currentNode, maxDeepOffset(currentNode));
       
   328             continue;
       
   329         }
       
   330 
       
   331         // return current position if it is in rendered text
       
   332         if (renderer->isText() && static_cast<RenderText*>(renderer)->firstTextBox()) {
       
   333             if (currentNode != startNode) {
       
   334                 // This assertion fires in layout tests in the case-transform.html test because
       
   335                 // of a mix-up between offsets in the text in the DOM tree with text in the
       
   336                 // render tree which can have a different length due to case transformation.
       
   337                 // Until we resolve that, disable this so we can run the layout tests!
       
   338                 //ASSERT(currentOffset >= renderer->caretMaxOffset());
       
   339                 return Position(currentNode, renderer->caretMaxOffset());
       
   340             }
       
   341 
       
   342             unsigned textOffset = currentPos.offsetInLeafNode();
       
   343             RenderText* textRenderer = static_cast<RenderText*>(renderer);
       
   344             for (InlineTextBox* box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
       
   345                 if (textOffset > box->start() && textOffset <= box->start() + box->len())
       
   346                     return currentPos;
       
   347                     
       
   348                 if (box != textRenderer->lastTextBox() && 
       
   349                     !box->nextOnLine() && 
       
   350                     textOffset == box->start() + box->len() + 1)
       
   351                     return currentPos;
       
   352             }
       
   353         }
       
   354     }
       
   355 
       
   356     return lastVisible;
       
   357 }
       
   358 
       
   359 // P.downstream() returns the end of the range of positions that map to the same VisiblePosition as P.
       
   360 Position Position::downstream() const
       
   361 {
       
   362     Node* startNode = node();
       
   363     if (!startNode)
       
   364         return Position();
       
   365 
       
   366     // iterate forward from there, looking for a qualified position
       
   367     Node* block = enclosingBlock(startNode);
       
   368     PositionIterator lastVisible = *this;
       
   369     PositionIterator currentPos = lastVisible;
       
   370     Node* originalRoot = node()->rootEditableElement();
       
   371     for (; !currentPos.atEnd(); currentPos.increment()) {   
       
   372         Node* currentNode = currentPos.node();
       
   373         
       
   374         if (currentNode->rootEditableElement() != originalRoot)
       
   375             break;
       
   376 
       
   377         // stop before going above the body, up into the head
       
   378         // return the last visible streamer position
       
   379         if (currentNode->hasTagName(bodyTag) && currentPos.atEndOfNode())
       
   380             break;
       
   381             
       
   382         // Do not enter a new enclosing block flow or table element, and don't leave the original one.
       
   383         if (block != enclosingBlock(currentNode))
       
   384             return lastVisible;
       
   385 
       
   386         // skip position in unrendered or invisible node
       
   387         RenderObject* renderer = currentNode->renderer();
       
   388         if (!renderer || renderer->style()->visibility() != VISIBLE)
       
   389             continue;
       
   390             
       
   391         // track last visible streamer position
       
   392         if (isStreamer(currentPos))
       
   393             lastVisible = currentPos;
       
   394 
       
   395         // Return position before brs, tables, and nodes which have content that can be ignored.
       
   396         if (editingIgnoresContent(currentNode) || renderer->isBR() || isTableElement(currentNode)) {
       
   397             if (currentPos.offsetInLeafNode() <= renderer->caretMinOffset())
       
   398                 return Position(currentNode, renderer->caretMinOffset());
       
   399             continue;
       
   400         }
       
   401 
       
   402         // return current position if it is in rendered text
       
   403         if (renderer->isText() && static_cast<RenderText*>(renderer)->firstTextBox()) {
       
   404             if (currentNode != startNode) {
       
   405                 ASSERT(currentPos.atStartOfNode());
       
   406                 return Position(currentNode, renderer->caretMinOffset());
       
   407             }
       
   408 
       
   409             unsigned textOffset = currentPos.offsetInLeafNode();
       
   410 
       
   411             RenderText* textRenderer = static_cast<RenderText*>(renderer);
       
   412             for (InlineTextBox* box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
       
   413                 if (textOffset >= box->start() && textOffset <= box->end())
       
   414                     return currentPos;
       
   415                 
       
   416                 if (box != textRenderer->lastTextBox() && 
       
   417                      !box->nextOnLine() &&
       
   418                      textOffset == box->start() + box->len()) {
       
   419                     return currentPos;
       
   420                 }
       
   421             }
       
   422         }
       
   423     }
       
   424     
       
   425     return lastVisible;
       
   426 }
       
   427 
       
   428 bool Position::hasRenderedNonAnonymousDescendantsWithHeight(RenderObject* renderer)
       
   429 {
       
   430     RenderObject* stop = renderer->nextInPreOrderAfterChildren();
       
   431     for (RenderObject *o = renderer->firstChild(); o && o != stop; o = o->nextInPreOrder())
       
   432         if (o->element() && o->height())
       
   433             return true;
       
   434             
       
   435     return false;
       
   436 }
       
   437 
       
   438 bool Position::nodeIsUserSelectNone(Node* node)
       
   439 {
       
   440     return node && node->renderer() && node->renderer()->style()->userSelect() == SELECT_NONE;
       
   441 }
       
   442 
       
   443 bool Position::isCandidate() const
       
   444 {
       
   445     if (isNull())
       
   446         return false;
       
   447         
       
   448     RenderObject *renderer = node()->renderer();
       
   449     if (!renderer)
       
   450         return false;
       
   451     
       
   452     if (renderer->style()->visibility() != VISIBLE)
       
   453         return false;
       
   454 
       
   455     if (renderer->isBR())
       
   456         return offset() == 0 && !nodeIsUserSelectNone(node()->parent());
       
   457 
       
   458     if (renderer->isText())
       
   459         return inRenderedText() && !nodeIsUserSelectNone(node());
       
   460 
       
   461     if (isTableElement(node()) || editingIgnoresContent(node()))
       
   462         return (offset() == 0 || offset() == maxDeepOffset(node())) && !nodeIsUserSelectNone(node()->parent());
       
   463 
       
   464     if (!node()->hasTagName(htmlTag) && renderer->isBlockFlow() && !hasRenderedNonAnonymousDescendantsWithHeight(renderer) &&
       
   465        (renderer->height() || node()->hasTagName(bodyTag)))
       
   466         return offset() == 0 && !nodeIsUserSelectNone(node());
       
   467     
       
   468     return false;
       
   469 }
       
   470 
       
   471 bool Position::inRenderedText() const
       
   472 {
       
   473     if (isNull() || !node()->isTextNode())
       
   474         return false;
       
   475         
       
   476     RenderObject *renderer = node()->renderer();
       
   477     if (!renderer)
       
   478         return false;
       
   479     
       
   480     RenderText *textRenderer = static_cast<RenderText *>(renderer);
       
   481     for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
       
   482         if (offset() < box->m_start && !textRenderer->containsReversedText()) {
       
   483             // The offset we're looking for is before this node
       
   484             // this means the offset must be in content that is
       
   485             // not rendered. Return false.
       
   486             return false;
       
   487         }
       
   488         if (box->containsCaretOffset(offset()))
       
   489             // Return false for offsets inside composed characters.
       
   490             return offset() == 0 || offset() == textRenderer->nextOffset(textRenderer->previousOffset(offset()));
       
   491     }
       
   492     
       
   493     return false;
       
   494 }
       
   495 
       
   496 bool Position::isRenderedCharacter() const
       
   497 {
       
   498     if (isNull() || !node()->isTextNode())
       
   499         return false;
       
   500         
       
   501     RenderObject *renderer = node()->renderer();
       
   502     if (!renderer)
       
   503         return false;
       
   504     
       
   505     RenderText *textRenderer = static_cast<RenderText *>(renderer);
       
   506     for (InlineTextBox *box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
       
   507         if (offset() < box->m_start && !textRenderer->containsReversedText()) {
       
   508             // The offset we're looking for is before this node
       
   509             // this means the offset must be in content that is
       
   510             // not rendered. Return false.
       
   511             return false;
       
   512         }
       
   513         if (offset() >= box->m_start && offset() < box->m_start + box->m_len)
       
   514             return true;
       
   515     }
       
   516     
       
   517     return false;
       
   518 }
       
   519 
       
   520 bool Position::rendersInDifferentPosition(const Position &pos) const
       
   521 {
       
   522     if (isNull() || pos.isNull())
       
   523         return false;
       
   524 
       
   525     RenderObject *renderer = node()->renderer();
       
   526     if (!renderer)
       
   527         return false;
       
   528     
       
   529     RenderObject *posRenderer = pos.node()->renderer();
       
   530     if (!posRenderer)
       
   531         return false;
       
   532 
       
   533     if (renderer->style()->visibility() != VISIBLE ||
       
   534         posRenderer->style()->visibility() != VISIBLE)
       
   535         return false;
       
   536     
       
   537     if (node() == pos.node()) {
       
   538         if (node()->hasTagName(brTag))
       
   539             return false;
       
   540 
       
   541         if (offset() == pos.offset())
       
   542             return false;
       
   543             
       
   544         if (!node()->isTextNode() && !pos.node()->isTextNode()) {
       
   545             if (offset() != pos.offset())
       
   546                 return true;
       
   547         }
       
   548     }
       
   549     
       
   550     if (node()->hasTagName(brTag) && pos.isCandidate())
       
   551         return true;
       
   552                 
       
   553     if (pos.node()->hasTagName(brTag) && isCandidate())
       
   554         return true;
       
   555                 
       
   556     if (node()->enclosingBlockFlowElement() != pos.node()->enclosingBlockFlowElement())
       
   557         return true;
       
   558 
       
   559     if (node()->isTextNode() && !inRenderedText())
       
   560         return false;
       
   561 
       
   562     if (pos.node()->isTextNode() && !pos.inRenderedText())
       
   563         return false;
       
   564 
       
   565     int thisRenderedOffset = renderedOffset();
       
   566     int posRenderedOffset = pos.renderedOffset();
       
   567 
       
   568     if (renderer == posRenderer && thisRenderedOffset == posRenderedOffset)
       
   569         return false;
       
   570 
       
   571     LOG(Editing, "renderer:               %p [%p]\n", renderer, renderer ? renderer->inlineBox(offset()) : 0);
       
   572     LOG(Editing, "thisRenderedOffset:         %d\n", thisRenderedOffset);
       
   573     LOG(Editing, "posRenderer:            %p [%p]\n", posRenderer, posRenderer ? posRenderer->inlineBox(offset()) : 0);
       
   574     LOG(Editing, "posRenderedOffset:      %d\n", posRenderedOffset);
       
   575     LOG(Editing, "node min/max:           %d:%d\n", node()->caretMinOffset(), node()->caretMaxRenderedOffset());
       
   576     LOG(Editing, "pos node min/max:       %d:%d\n", pos.node()->caretMinOffset(), pos.node()->caretMaxRenderedOffset());
       
   577     LOG(Editing, "----------------------------------------------------------------------\n");
       
   578 
       
   579     InlineBox *b1 = renderer ? renderer->inlineBox(offset()) : 0;
       
   580     InlineBox *b2 = posRenderer ? posRenderer->inlineBox(pos.offset()) : 0;
       
   581 
       
   582     if (!b1 || !b2) {
       
   583         return false;
       
   584     }
       
   585 
       
   586     if (b1->root() != b2->root()) {
       
   587         return true;
       
   588     }
       
   589 
       
   590     if (nextRenderedEditable(node()) == pos.node() && 
       
   591         thisRenderedOffset == (int)node()->caretMaxRenderedOffset() && posRenderedOffset == 0) {
       
   592         return false;
       
   593     }
       
   594     
       
   595     if (previousRenderedEditable(node()) == pos.node() && 
       
   596         thisRenderedOffset == 0 && posRenderedOffset == (int)pos.node()->caretMaxRenderedOffset()) {
       
   597         return false;
       
   598     }
       
   599 
       
   600     return true;
       
   601 }
       
   602 
       
   603 // This is only called from DeleteSelectionCommand and assumes that it starts in editable content.
       
   604 Position Position::leadingWhitespacePosition(EAffinity affinity, bool considerNonCollapsibleWhitespace) const
       
   605 {
       
   606     ASSERT(isEditablePosition(*this));
       
   607     if (isNull())
       
   608         return Position();
       
   609     
       
   610     if (upstream().node()->hasTagName(brTag))
       
   611         return Position();
       
   612 
       
   613     Position prev = previousCharacterPosition(affinity);
       
   614     if (prev != *this && prev.node()->inSameContainingBlockFlowElement(node()) && prev.node()->isTextNode()) {
       
   615         String string = static_cast<Text *>(prev.node())->data();
       
   616         UChar c = string[prev.offset()];
       
   617         if (considerNonCollapsibleWhitespace ? (DeprecatedChar(c).isSpace() || c == noBreakSpace) : isCollapsibleWhitespace(c))
       
   618             if (isEditablePosition(prev))
       
   619                 return prev;
       
   620     }
       
   621 
       
   622     return Position();
       
   623 }
       
   624 
       
   625 // This is only called from DeleteSelectionCommand and assumes that it starts in editable content.
       
   626 Position Position::trailingWhitespacePosition(EAffinity affinity, bool considerNonCollapsibleWhitespace) const
       
   627 {
       
   628     ASSERT(isEditablePosition(*this));
       
   629     if (isNull())
       
   630         return Position();
       
   631     
       
   632     VisiblePosition v(*this);
       
   633     UChar c = v.characterAfter();
       
   634     // The space must not be in another paragraph and it must be editable.
       
   635     if (!isEndOfParagraph(v) && v.next(true).isNotNull())
       
   636         if (considerNonCollapsibleWhitespace ? (DeprecatedChar(c).isSpace() || c == noBreakSpace) : isCollapsibleWhitespace(c))
       
   637             return *this;
       
   638     
       
   639     return Position();
       
   640 }
       
   641 
       
   642 void Position::debugPosition(const char *msg) const
       
   643 {
       
   644     if (isNull())
       
   645         fprintf(stderr, "Position [%s]: null\n", msg);
       
   646     else
       
   647         fprintf(stderr, "Position [%s]: %s [%p] at %d\n", msg, node()->nodeName().deprecatedString().latin1(), node(), offset());
       
   648 }
       
   649 
       
   650 #ifndef NDEBUG
       
   651 
       
   652 void Position::formatForDebugger(char *buffer, unsigned length) const
       
   653 {
       
   654     String result;
       
   655     
       
   656     if (isNull())
       
   657         result = "<null>";
       
   658     else {
       
   659         char s[1024];
       
   660         result += "offset ";
       
   661         result += String::number(m_offset);
       
   662         result += " of ";
       
   663         m_node->formatForDebugger(s, sizeof(s));
       
   664         result += s;
       
   665     }
       
   666           
       
   667     strncpy(buffer, result.deprecatedString().latin1(), length - 1);
       
   668 }
       
   669 
       
   670 void Position::showTreeForThis() const
       
   671 {
       
   672     if (m_node)
       
   673         m_node->showTreeForThis();
       
   674 }
       
   675 
       
   676 #endif
       
   677 
       
   678 Position startPosition(const Range *r)
       
   679 {
       
   680     if (!r || r->isDetached())
       
   681         return Position();
       
   682     ExceptionCode ec;
       
   683     return Position(r->startContainer(ec), r->startOffset(ec));
       
   684 }
       
   685 
       
   686 Position endPosition(const Range *r)
       
   687 {
       
   688     if (!r || r->isDetached())
       
   689         return Position();
       
   690     ExceptionCode ec;
       
   691     return Position(r->endContainer(ec), r->endOffset(ec));
       
   692 }
       
   693 
       
   694 } // namespace WebCore
       
   695 
       
   696 #ifndef NDEBUG
       
   697 
       
   698 void showTree(const WebCore::Position& pos)
       
   699 {
       
   700     pos.showTreeForThis();
       
   701 }
       
   702 
       
   703 void showTree(const WebCore::Position* pos)
       
   704 {
       
   705     if (pos)
       
   706         pos->showTreeForThis();
       
   707 }
       
   708 
       
   709 #endif