Changeset 89505 in webkit


Ignore:
Timestamp:
Jun 22, 2011, 5:45:26 PM (14 years ago)
Author:
rniwa@webkit.org
Message:

2011-06-22 Ryosuke Niwa <rniwa@webkit.org>

Reviewed by Darin Adler.

Add a Position constructor that takes (Text*, unsigned offset)
https://bugs.webkit.org/show_bug.cgi?id=63181

Added Position::Position(PassRefPtr<Text*>, unsigned offset) and deployed in a couple of places
by replacing the calls to the old constructor.

  • dom/Position.cpp: (WebCore::Position::Position): Added.
  • dom/Position.h:
  • editing/CompositeEditCommand.cpp: (WebCore::CompositeEditCommand::replaceSelectedTextInNode): Calls new constructor; extracted from InsertTextCommand::performTrivialReplace and ReplaceSelectionCommand::performTrivialReplace. (WebCore::CompositeEditCommand::rebalanceWhitespaceOnTextSubstring): Calls new constructor
  • editing/CompositeEditCommand.h:
  • editing/InsertTextCommand.cpp: (WebCore::InsertTextCommand::performTrivialReplace): Calls replaceSelectedTextInNode. (WebCore::InsertTextCommand::input): Calls new constructor. (WebCore::InsertTextCommand::insertTab): Use RefPtr instead of a raw pointer.
  • editing/ReplaceSelectionCommand.cpp: (WebCore::ReplaceSelectionCommand::performTrivialReplace): Calls replaceSelectedTextInNode.
  • editing/visible_units.cpp: (WebCore::startPositionForLine): Calls new constructor.
  • rendering/RenderTextControl.cpp: (WebCore::RenderTextControl::visiblePositionForIndex): Calls new constructor; calls endPosition on Range instead of avoid manually constructing a VisiblePosition out of endContainer and endOffset.
Location:
trunk/Source/WebCore
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r89504 r89505  
     12011-06-22  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Add a Position constructor that takes (Text*, unsigned offset)
     6        https://bugs.webkit.org/show_bug.cgi?id=63181
     7
     8        Added Position::Position(PassRefPtr<Text*>, unsigned offset) and deployed in a couple of places
     9        by replacing the calls to the old constructor.
     10
     11        * dom/Position.cpp:
     12        (WebCore::Position::Position): Added.
     13        * dom/Position.h:
     14        * editing/CompositeEditCommand.cpp:
     15        (WebCore::CompositeEditCommand::replaceSelectedTextInNode): Calls new constructor; extracted
     16        from InsertTextCommand::performTrivialReplace and ReplaceSelectionCommand::performTrivialReplace.
     17        (WebCore::CompositeEditCommand::rebalanceWhitespaceOnTextSubstring): Calls new constructor
     18        * editing/CompositeEditCommand.h:
     19        * editing/InsertTextCommand.cpp:
     20        (WebCore::InsertTextCommand::performTrivialReplace): Calls replaceSelectedTextInNode.
     21        (WebCore::InsertTextCommand::input): Calls new constructor.
     22        (WebCore::InsertTextCommand::insertTab): Use RefPtr instead of a raw pointer.
     23        * editing/ReplaceSelectionCommand.cpp:
     24        (WebCore::ReplaceSelectionCommand::performTrivialReplace): Calls replaceSelectedTextInNode.
     25        * editing/visible_units.cpp:
     26        (WebCore::startPositionForLine): Calls new constructor.
     27        * rendering/RenderTextControl.cpp:
     28        (WebCore::RenderTextControl::visiblePositionForIndex): Calls new constructor; calls endPosition
     29        on Range instead of avoid manually constructing a VisiblePosition out of endContainer and endOffset.
     30
    1312011-06-22  Adam Barth  <abarth@webkit.org>
    232
  • trunk/Source/WebCore/dom/Position.cpp

    r89440 r89505  
    104104}
    105105
     106Position::Position(PassRefPtr<Text> textNode, unsigned offset)
     107    : m_anchorNode(textNode)
     108    , m_offset(static_cast<int>(offset))
     109    , m_anchorType(PositionIsOffsetInAnchor)
     110    , m_isLegacyEditingPosition(false)
     111{
     112    ASSERT(m_anchorNode);
     113}
     114
    106115void Position::moveToPosition(PassRefPtr<Node> node, int offset)
    107116{
  • trunk/Source/WebCore/dom/Position.h

    r89440 r89505  
    4343class Range;
    4444class RenderObject;
     45class Text;
    4546
    4647enum PositionMoveType {
     
    8182    // For creating before/after positions:
    8283    Position(PassRefPtr<Node> anchorNode, AnchorType);
     84    Position(PassRefPtr<Text> textNode, unsigned offset);
     85
    8386    // For creating offset positions:
     87    // FIXME: This constructor should eventually go away. See bug 63040.
    8488    Position(PassRefPtr<Node> anchorNode, int offset, AnchorType);
    8589
  • trunk/Source/WebCore/editing/CompositeEditCommand.cpp

    r89420 r89505  
    341341}
    342342
     343Position CompositeEditCommand::replaceSelectedTextInNode(const String& text)
     344{
     345    Position start = endingSelection().start();
     346    Position end = endingSelection().end();
     347    if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode() || isTabSpanTextNode(start.containerNode()))
     348        return Position();
     349
     350    RefPtr<Text> textNode = static_cast<Text*>(start.containerNode());
     351    replaceTextInNode(textNode, start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
     352
     353    return Position(textNode.release(), start.offsetInContainerNode() + text.length());
     354}
     355
    343356void CompositeEditCommand::replaceTextInNodePreservingMarkers(PassRefPtr<Text> prpNode, unsigned offset, unsigned count, const String& replacementText)
    344357{
     
    480493        return;
    481494
    482     VisiblePosition visibleUpstreamPos(Position(textNode, upstream, Position::PositionIsOffsetInAnchor));
    483     VisiblePosition visibleDownstreamPos(Position(textNode, downstream, Position::PositionIsOffsetInAnchor));
     495    VisiblePosition visibleUpstreamPos(Position(textNode, upstream));
     496    VisiblePosition visibleDownstreamPos(Position(textNode, downstream));
    484497   
    485498    String string = text.substring(upstream, length);
  • trunk/Source/WebCore/editing/CompositeEditCommand.h

    r89420 r89505  
    8383    void prune(PassRefPtr<Node>);
    8484    void replaceTextInNode(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
     85    Position replaceSelectedTextInNode(const String&);
    8586    void replaceTextInNodePreservingMarkers(PassRefPtr<Text>, unsigned offset, unsigned count, const String& replacementText);
    8687    Position positionOutsideTabSpan(const Position&);
  • trunk/Source/WebCore/editing/InsertTextCommand.cpp

    r89440 r89505  
    7777    if (text.contains('\t') || text.contains(' ') || text.contains('\n'))
    7878        return false;
    79    
    80     Position start = endingSelection().start().parentAnchoredEquivalent();
    81     Position end = endingSelection().end().parentAnchoredEquivalent();
    82     ASSERT(start.anchorType() == Position::PositionIsOffsetInAnchor);
    83     ASSERT(end.anchorType() == Position::PositionIsOffsetInAnchor);
    84 
    85     if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode() || isTabSpanTextNode(start.containerNode()))
     79
     80    Position start = endingSelection().start();
     81    Position endPosition = replaceSelectedTextInNode(text);
     82    if (endPosition.isNull())
    8683        return false;
    87 
    88     replaceTextInNode(static_cast<Text*>(start.containerNode()), start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
    89 
    90     Position endPosition(start.containerNode(), start.offsetInContainerNode() + text.length(), Position::PositionIsOffsetInAnchor);
    9184
    9285    // We could have inserted a part of composed character sequence,
     
    166159        if (placeholder.isNotNull())
    167160            removePlaceholderAt(placeholder);
    168         Text* textNode = static_cast<Text*>(startPosition.containerNode());
     161        RefPtr<Text> textNode = static_cast<Text*>(startPosition.containerNode());
    169162        const unsigned offset = startPosition.offsetInContainerNode();
    170163
    171164        insertTextIntoNode(textNode, offset, text);
    172         endPosition = Position(textNode, offset + text.length(), Position::PositionIsOffsetInAnchor);
     165        endPosition = Position(textNode, offset + text.length());
    173166
    174167        if (whitespaceRebalance == RebalanceLeadingAndTrailingWhitespaces) {
     
    212205    // keep tabs coalesced in tab span
    213206    if (isTabSpanTextNode(node)) {
    214         insertTextIntoNode(static_cast<Text *>(node), offset, "\t");
    215         return Position(node, offset + 1, Position::PositionIsOffsetInAnchor);
     207        RefPtr<Text> textNode = static_cast<Text*>(node);
     208        insertTextIntoNode(textNode, offset, "\t");
     209        return Position(textNode.release(), offset + 1);
    216210    }
    217211   
     
    223217        insertNodeAt(spanNode.get(), insertPos);
    224218    } else {
    225         Text *textNode = static_cast<Text *>(node);
    226         if (offset >= textNode->length()) {
    227             insertNodeAfter(spanNode.get(), textNode);
    228         } else {
     219        RefPtr<Text> textNode = static_cast<Text*>(node);
     220        if (offset >= textNode->length())
     221            insertNodeAfter(spanNode, textNode.release());
     222        else {
    229223            // split node to make room for the span
    230224            // NOTE: splitTextNode uses textNode for the
     
    233227            if (offset > 0)
    234228                splitTextNode(textNode, offset);
    235             insertNodeBefore(spanNode, textNode);
     229            insertNodeBefore(spanNode, textNode.release());
    236230        }
    237231    }
  • trunk/Source/WebCore/editing/ReplaceSelectionCommand.cpp

    r89283 r89505  
    13061306    Text* textNode = static_cast<Text*>(fragment.firstChild());
    13071307    // Our fragment creation code handles tabs, spaces, and newlines, so we don't have to worry about those here.
    1308     String text(textNode->data());
    1309    
    1310     Position start = endingSelection().start().parentAnchoredEquivalent();
    1311     Position end = endingSelection().end().parentAnchoredEquivalent();
    1312     ASSERT(start.anchorType() == Position::PositionIsOffsetInAnchor);
    1313     ASSERT(end.anchorType() == Position::PositionIsOffsetInAnchor);
    1314 
    1315     if (start.containerNode() != end.containerNode() || !start.containerNode()->isTextNode())
    1316         return false;
    1317 
    1318     replaceTextInNode(static_cast<Text*>(start.containerNode()), start.offsetInContainerNode(), end.offsetInContainerNode() - start.offsetInContainerNode(), text);
    1319 
    1320     end = Position(start.containerNode(), start.offsetInContainerNode() + text.length(), Position::PositionIsOffsetInAnchor);
     1308
     1309    Position start = endingSelection().start();
     1310    Position end = replaceSelectedTextInNode(textNode->data());
     1311    if (end.isNull())
     1312        return false;
    13211313
    13221314    VisibleSelection selectionAfterReplace(m_selectReplacement ? start : end, end);
  • trunk/Source/WebCore/editing/visible_units.cpp

    r89440 r89505  
    3535#include "RenderLayer.h"
    3636#include "RenderObject.h"
     37#include "Text.h"
    3738#include "TextBoundaries.h"
    3839#include "TextBreakIterator.h"
     
    387388    }
    388389   
    389     VisiblePosition visPos = startNode->isTextNode() ? VisiblePosition(Position(startNode, static_cast<InlineTextBox *>(startBox)->start(), Position::PositionIsOffsetInAnchor), DOWNSTREAM)
     390    VisiblePosition visPos = startNode->isTextNode() ? VisiblePosition(Position(static_cast<Text*>(startNode), static_cast<InlineTextBox*>(startBox)->start()), DOWNSTREAM)
    390391                                                     : VisiblePosition(positionBeforeNode(startNode), DOWNSTREAM);
    391392    return positionAvoidingFirstPositionInTable(visPos);
  • trunk/Source/WebCore/rendering/RenderTextControl.cpp

    r88456 r89505  
    305305{
    306306    if (index <= 0)
    307         return VisiblePosition(Position(innerTextElement(), 0, Position::PositionIsOffsetInAnchor), DOWNSTREAM);
     307        return VisiblePosition(firstPositionInNode(innerTextElement()), DOWNSTREAM);
    308308    ExceptionCode ec = 0;
    309309    RefPtr<Range> range = Range::create(document());
     
    312312    CharacterIterator it(range.get());
    313313    it.advance(index - 1);
    314     Node* endContainer = it.range()->endContainer(ec);
    315     ASSERT(!ec);
    316     int endOffset = it.range()->endOffset(ec);
    317     ASSERT(!ec);
    318     return VisiblePosition(Position(endContainer, endOffset, Position::PositionIsOffsetInAnchor), UPSTREAM);
     314    return VisiblePosition(it.range()->endPosition(), UPSTREAM);
    319315}
    320316
Note: See TracChangeset for help on using the changeset viewer.