Changeset 42264 in webkit


Ignore:
Timestamp:
Apr 7, 2009 7:25:57 AM (15 years ago)
Author:
eric@webkit.org
Message:

Reviewed by Darin Adler.

Move RangeBoundaryPoint off of Position, per Darin's suggestion
https://bugs.webkit.org/show_bug.cgi?id=24966

Rename setToChild to setToBeforeChild (since that's what the method does)

  • dom/Position.h: (WebCore::Position::offsetInContainerNode):
  • dom/Range.cpp: (WebCore::Range::insertNode): (WebCore::Range::selectNodeContents): (WebCore::boundaryNodeWillBeRemoved):
  • dom/Range.h: (WebCore::Range::startPosition): (WebCore::Range::endPosition):
  • dom/RangeBoundaryPoint.h: (WebCore::RangeBoundaryPoint::RangeBoundaryPoint): (WebCore::RangeBoundaryPoint::container): (WebCore::RangeBoundaryPoint::childBefore): (WebCore::RangeBoundaryPoint::position): (WebCore::RangeBoundaryPoint::offset): (WebCore::RangeBoundaryPoint::clear): (WebCore::RangeBoundaryPoint::set): (WebCore::RangeBoundaryPoint::setOffset): (WebCore::RangeBoundaryPoint::setToBeforeChild): (WebCore::RangeBoundaryPoint::setToStartOfNode): (WebCore::RangeBoundaryPoint::setToEndOfNode): (WebCore::RangeBoundaryPoint::childBeforeWillBeRemoved): (WebCore::RangeBoundaryPoint::invalidateOffset):
Location:
trunk/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r42263 r42264  
     12009-04-07  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by Darin Adler.
     4
     5        Move RangeBoundaryPoint off of Position, per Darin's suggestion
     6        https://bugs.webkit.org/show_bug.cgi?id=24966
     7
     8        Rename setToChild to setToBeforeChild (since that's what the method does)
     9
     10        * dom/Position.h:
     11        (WebCore::Position::offsetInContainerNode):
     12        * dom/Range.cpp:
     13        (WebCore::Range::insertNode):
     14        (WebCore::Range::selectNodeContents):
     15        (WebCore::boundaryNodeWillBeRemoved):
     16        * dom/Range.h:
     17        (WebCore::Range::startPosition):
     18        (WebCore::Range::endPosition):
     19        * dom/RangeBoundaryPoint.h:
     20        (WebCore::RangeBoundaryPoint::RangeBoundaryPoint):
     21        (WebCore::RangeBoundaryPoint::container):
     22        (WebCore::RangeBoundaryPoint::childBefore):
     23        (WebCore::RangeBoundaryPoint::position):
     24        (WebCore::RangeBoundaryPoint::offset):
     25        (WebCore::RangeBoundaryPoint::clear):
     26        (WebCore::RangeBoundaryPoint::set):
     27        (WebCore::RangeBoundaryPoint::setOffset):
     28        (WebCore::RangeBoundaryPoint::setToBeforeChild):
     29        (WebCore::RangeBoundaryPoint::setToStartOfNode):
     30        (WebCore::RangeBoundaryPoint::setToEndOfNode):
     31        (WebCore::RangeBoundaryPoint::childBeforeWillBeRemoved):
     32        (WebCore::RangeBoundaryPoint::invalidateOffset):
     33
    1342009-04-07  Eric Seidel  <eric@webkit.org>
    235
  • trunk/WebCore/dom/Position.h

    r42263 r42264  
    2929#include "TextAffinity.h"
    3030#include "TextDirection.h"
     31#include <wtf/Assertions.h>
    3132#include <wtf/PassRefPtr.h>
    3233#include <wtf/RefPtr.h>
     
    4748};
    4849
    49 // FIXME: Reduce the number of operations we have on a Position.
    50 // This should be more like a humble struct, without so many different
    51 // member functions. We should find better homes for these functions.
    52 
    5350class Position {
    5451public:
     
    6562    // These are always DOM compliant values.  Editing positions like [img, 0] (aka [img, before])
    6663    // will return img->parentNode() and img->nodeIndex() from these functions.
    67     Node* containerNode() const;
    68     int computeOffsetInContainerNode() const;
     64    Node* containerNode() const; // NULL for a before/after position anchored to a node with no parent
     65    int computeOffsetInContainerNode() const;  // O(n) for before/after-anchored positions, O(1) for parent-anchored positions
     66
     67    // Inline O(1) access for Positions which callers know to be parent-anchored
     68    int offsetInContainerNode() const
     69    {
     70        ASSERT(anchorType() == PositionIsOffsetInAnchor);
     71        return m_offset;
     72    }
    6973
    7074    // These are convenience methods which are smart about whether the position is neighbor anchored or parent anchored
  • trunk/WebCore/dom/Range.cpp

    r42084 r42264  
    992992        // to pass Acid3. We might later decide to remove this.
    993993        if (collapsed)
    994             m_end.setToChild(newText.get());
     994            m_end.setToBeforeChild(newText.get());
    995995    } else {
    996996        RefPtr<Node> lastChild;
     
    13561356    }
    13571357
    1358     m_start.setToStart(refNode);
    1359     m_end.setToEnd(refNode);
     1358    m_start.setToStartOfNode(refNode);
     1359    m_end.setToEndOfNode(refNode);
    13601360}
    13611361
     
    17091709    for (Node* n = boundary.container(); n; n = n->parentNode()) {
    17101710        if (n == nodeToBeRemoved) {
    1711             boundary.setToChild(nodeToBeRemoved);
     1711            boundary.setToBeforeChild(nodeToBeRemoved);
    17121712            return;
    17131713        }
  • trunk/WebCore/dom/Range.h

    r34900 r42264  
    9292    void setStartBefore(Node*, ExceptionCode&);
    9393
    94     const Position& startPosition() const { return m_start.position(); }
    95     const Position& endPosition() const { return m_end.position(); }
     94    const Position startPosition() const { return m_start.toPosition(); }
     95    const Position endPosition() const { return m_end.toPosition(); }
    9696
    9797    Node* firstNode() const;
  • trunk/WebCore/dom/RangeBoundaryPoint.h

    r41933 r42264  
    3737    explicit RangeBoundaryPoint(PassRefPtr<Node> container);
    3838
    39     const Position& position() const;
     39    const Position toPosition() const;
     40
    4041    Node* container() const;
    4142    int offset() const;
     
    4647    void set(PassRefPtr<Node> container, int offset, Node* childBefore);
    4748    void setOffset(int offset);
    48     void setToChild(Node* child);
    49     void setToStart(PassRefPtr<Node> container);
    50     void setToEnd(PassRefPtr<Node> container);
     49
     50    void setToBeforeChild(Node*);
     51    void setToStartOfNode(PassRefPtr<Node>);
     52    void setToEndOfNode(PassRefPtr<Node>);
    5153
    5254    void childBeforeWillBeRemoved();
    5355    void invalidateOffset() const;
     56    void ensureOffsetIsValid() const;
    5457
    5558private:
    5659    static const int invalidOffset = -1;
    57 
    58     // FIXME: RangeBoundaryPoint is the only file to ever use -1 as am expected offset for Position
    59     // RangeBoundaryPoint currently needs to store a Position object to make the
    60     // position() function be able to return a const& (and thus avoid ref-churn).
    6160   
    62     mutable Position m_position;
    63     Node* m_childBefore;
     61    RefPtr<Node> m_containerNode;
     62    mutable int m_offsetInContainer;
     63    Node* m_childBeforeBoundary;
    6464};
    6565
    6666inline RangeBoundaryPoint::RangeBoundaryPoint()
    67     : m_childBefore(0)
     67    : m_offsetInContainer(0)
     68    , m_childBeforeBoundary(0)
    6869{
    6970}
    7071
    7172inline RangeBoundaryPoint::RangeBoundaryPoint(PassRefPtr<Node> container)
    72     : m_position(container, 0)
    73     , m_childBefore(0)
     73    : m_containerNode(container)
     74    , m_offsetInContainer(0)
     75    , m_childBeforeBoundary(0)
    7476{
    7577}
     
    7779inline Node* RangeBoundaryPoint::container() const
    7880{
    79     return m_position.node();
     81    return m_containerNode.get();
    8082}
    8183
    8284inline Node* RangeBoundaryPoint::childBefore() const
    8385{
    84     return m_childBefore;
     86    return m_childBeforeBoundary;
    8587}
    8688
    87 inline const Position& RangeBoundaryPoint::position() const
     89inline void RangeBoundaryPoint::ensureOffsetIsValid() const
    8890{
    89     if (m_position.m_offset >= 0)
    90         return m_position;
    91     ASSERT(m_childBefore);
    92     m_position.m_offset = m_childBefore->nodeIndex() + 1;
    93     return m_position;
     91    if (m_offsetInContainer >= 0)
     92        return;
     93
     94    ASSERT(m_childBeforeBoundary);
     95    m_offsetInContainer = m_childBeforeBoundary->nodeIndex() + 1;
     96}
     97
     98inline const Position RangeBoundaryPoint::toPosition() const
     99{
     100    ensureOffsetIsValid();
     101    return Position(m_containerNode.get(), m_offsetInContainer);
    94102}
    95103
    96104inline int RangeBoundaryPoint::offset() const
    97105{
    98     return position().m_offset;
     106    ensureOffsetIsValid();
     107    return m_offsetInContainer;
    99108}
    100109
    101110inline void RangeBoundaryPoint::clear()
    102111{
    103     m_position.clear();
    104     m_childBefore = 0;
     112    m_containerNode.clear();
     113    m_offsetInContainer = 0;
     114    m_childBeforeBoundary = 0;
    105115}
    106116
     
    109119    ASSERT(offset >= 0);
    110120    ASSERT(childBefore == (offset ? container->childNode(offset - 1) : 0));
    111     m_position.moveToPosition(container, offset);
    112     m_childBefore = childBefore;
     121    m_containerNode = container;
     122    m_offsetInContainer = offset;
     123    m_childBeforeBoundary = childBefore;
    113124}
    114125
    115126inline void RangeBoundaryPoint::setOffset(int offset)
    116127{
    117     ASSERT(m_position.node());
    118     ASSERT(m_position.node()->offsetInCharacters());
    119     ASSERT(m_position.m_offset >= 0);
    120     ASSERT(!m_childBefore);
    121     m_position.moveToOffset(offset);
     128    ASSERT(m_containerNode);
     129    ASSERT(m_containerNode->offsetInCharacters());
     130    ASSERT(m_offsetInContainer >= 0);
     131    ASSERT(!m_childBeforeBoundary);
     132    m_offsetInContainer = offset;
    122133}
    123134
    124 inline void RangeBoundaryPoint::setToChild(Node* child)
     135inline void RangeBoundaryPoint::setToBeforeChild(Node* child)
    125136{
    126137    ASSERT(child);
    127138    ASSERT(child->parentNode());
    128     m_childBefore = child->previousSibling();
    129     m_position.moveToPosition(child->parentNode(), m_childBefore ? invalidOffset : 0);
     139    m_childBeforeBoundary = child->previousSibling();
     140    m_containerNode = child->parentNode();
     141    m_offsetInContainer = m_childBeforeBoundary ? invalidOffset : 0;
    130142}
    131143
    132 inline void RangeBoundaryPoint::setToStart(PassRefPtr<Node> container)
     144inline void RangeBoundaryPoint::setToStartOfNode(PassRefPtr<Node> container)
    133145{
    134146    ASSERT(container);
    135     m_position.moveToPosition(container, 0);
    136     m_childBefore = 0;
     147    m_containerNode = container;
     148    m_offsetInContainer = 0;
     149    m_childBeforeBoundary = 0;
    137150}
    138151
    139 inline void RangeBoundaryPoint::setToEnd(PassRefPtr<Node> container)
     152inline void RangeBoundaryPoint::setToEndOfNode(PassRefPtr<Node> container)
    140153{
    141154    ASSERT(container);
    142     if (container->offsetInCharacters()) {
    143         m_position.moveToPosition(container, container->maxCharacterOffset());
    144         m_childBefore = 0;
     155    m_containerNode = container;
     156    if (m_containerNode->offsetInCharacters()) {
     157        m_offsetInContainer = m_containerNode->maxCharacterOffset();
     158        m_childBeforeBoundary = 0;
    145159    } else {
    146         m_childBefore = container->lastChild();
    147         m_position.moveToPosition(container, m_childBefore ? invalidOffset : 0);
     160        m_childBeforeBoundary = m_containerNode->lastChild();
     161        m_offsetInContainer = m_childBeforeBoundary ? invalidOffset : 0;
    148162    }
    149163}
     
    151165inline void RangeBoundaryPoint::childBeforeWillBeRemoved()
    152166{
    153     ASSERT(m_position.m_offset);
    154     m_childBefore = m_childBefore->previousSibling();
    155     if (!m_childBefore)
    156         m_position.m_offset = 0;
    157     else if (m_position.m_offset > 0)
    158         --m_position.m_offset;
     167    ASSERT(m_offsetInContainer);
     168    m_childBeforeBoundary = m_childBeforeBoundary->previousSibling();
     169    if (!m_childBeforeBoundary)
     170        m_offsetInContainer = 0;
     171    else if (m_offsetInContainer > 0)
     172        --m_offsetInContainer;
    159173}
    160174
    161175inline void RangeBoundaryPoint::invalidateOffset() const
    162176{
    163     m_position.m_offset = invalidOffset;
     177    m_offsetInContainer = invalidOffset;
    164178}
    165179
Note: See TracChangeset for help on using the changeset viewer.