Changeset 40738 in webkit


Ignore:
Timestamp:
Feb 6, 2009 4:04:48 PM (15 years ago)
Author:
eric@webkit.org
Message:

Reviewed by Justin Garcia.

Minor refactoring and cleanup to Selection code
https://bugs.webkit.org/show_bug.cgi?id=23774

No functional changes, thus no tests.

  • editing/Editor.cpp: (WebCore::Editor::applyStyle): (WebCore::Editor::applyParagraphStyle):
  • editing/Selection.cpp: (WebCore::Selection::Selection): (WebCore::Selection::setBaseAndExtentToDeepEquivalents): (WebCore::Selection::setStartAndEndFromBaseAndExtentRespectingGranularity): (WebCore::Selection::updateSelectionType): (WebCore::Selection::validate): (WebCore::Selection::setWithoutValidation): (WebCore::Selection::adjustSelectionToAvoidCrossingEditingBoundaries):
  • editing/Selection.h: (WebCore::Selection::): (WebCore::Selection::selectionType): (WebCore::Selection::extent): (WebCore::Selection::isNone): (WebCore::Selection::isCaret): (WebCore::Selection::isRange): (WebCore::Selection::isCaretOrRange):
  • editing/SelectionController.h: (WebCore::SelectionController::selectionType):
  • editing/TypingCommand.cpp: (WebCore::TypingCommand::deleteKeyPressed): (WebCore::TypingCommand::forwardDeleteKeyPressed):
  • page/Frame.cpp: (WebCore::Frame::revealSelection):
Location:
trunk/WebCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r40737 r40738  
     12009-02-06  Eric Seidel  <eric@webkit.org>
     2
     3        Reviewed by Justin Garcia.
     4
     5        Minor refactoring and cleanup to Selection code
     6        https://bugs.webkit.org/show_bug.cgi?id=23774
     7
     8        No functional changes, thus no tests.
     9
     10        * editing/Editor.cpp:
     11        (WebCore::Editor::applyStyle):
     12        (WebCore::Editor::applyParagraphStyle):
     13        * editing/Selection.cpp:
     14        (WebCore::Selection::Selection):
     15        (WebCore::Selection::setBaseAndExtentToDeepEquivalents):
     16        (WebCore::Selection::setStartAndEndFromBaseAndExtentRespectingGranularity):
     17        (WebCore::Selection::updateSelectionType):
     18        (WebCore::Selection::validate):
     19        (WebCore::Selection::setWithoutValidation):
     20        (WebCore::Selection::adjustSelectionToAvoidCrossingEditingBoundaries):
     21        * editing/Selection.h:
     22        (WebCore::Selection::):
     23        (WebCore::Selection::selectionType):
     24        (WebCore::Selection::extent):
     25        (WebCore::Selection::isNone):
     26        (WebCore::Selection::isCaret):
     27        (WebCore::Selection::isRange):
     28        (WebCore::Selection::isCaretOrRange):
     29        * editing/SelectionController.h:
     30        (WebCore::SelectionController::selectionType):
     31        * editing/TypingCommand.cpp:
     32        (WebCore::TypingCommand::deleteKeyPressed):
     33        (WebCore::TypingCommand::forwardDeleteKeyPressed):
     34        * page/Frame.cpp:
     35        (WebCore::Frame::revealSelection):
     36
    1372009-02-06  Dimitri Glazkov  <dglazkov@chromium.org>
    238
  • trunk/WebCore/editing/Editor.cpp

    r40675 r40738  
    711711void Editor::applyStyle(CSSStyleDeclaration* style, EditAction editingAction)
    712712{
    713     switch (m_frame->selection()->state()) {
    714         case Selection::NONE:
     713    switch (m_frame->selection()->selectionType()) {
     714        case Selection::NoSelection:
    715715            // do nothing
    716716            break;
    717         case Selection::CARET:
     717        case Selection::CaretSelection:
    718718            m_frame->computeAndSetTypingStyle(style, editingAction);
    719719            break;
    720         case Selection::RANGE:
     720        case Selection::RangeSelection:
    721721            if (m_frame->document() && style)
    722722                applyCommand(ApplyStyleCommand::create(m_frame->document(), style, editingAction));
     
    732732void Editor::applyParagraphStyle(CSSStyleDeclaration* style, EditAction editingAction)
    733733{
    734     switch (m_frame->selection()->state()) {
    735         case Selection::NONE:
     734    switch (m_frame->selection()->selectionType()) {
     735        case Selection::NoSelection:
    736736            // do nothing
    737737            break;
    738         case Selection::CARET:
    739         case Selection::RANGE:
     738        case Selection::CaretSelection:
     739        case Selection::RangeSelection:
    740740            if (m_frame->document() && style)
    741741                applyCommand(ApplyStyleCommand::create(m_frame->document(), style, editingAction, ApplyStyleCommand::ForceBlockProperties));
  • trunk/WebCore/editing/Selection.cpp

    r39831 r40738  
    4444    : m_affinity(DOWNSTREAM)
    4545    , m_granularity(CharacterGranularity)
    46     , m_state(NONE)
     46    , m_selectionType(NoSelection)
    4747    , m_baseIsFirst(true)
    4848{
     
    239239}
    240240
    241 void Selection::validate()
     241void Selection::setBaseAndExtentToDeepEquivalents()
    242242{
    243243    // Move the selection to rendered positions, if possible.
     
    260260        m_extent = m_base;
    261261        m_baseIsFirst = true;
    262     } else {
     262    } else
    263263        m_baseIsFirst = comparePositions(m_base, m_extent) <= 0;
    264     }
    265 
     264}
     265
     266void Selection::setStartAndEndFromBaseAndExtentRespectingGranularity()
     267{
    266268    if (m_baseIsFirst) {
    267269        m_start = m_base;
     
    272274    }
    273275
    274     // Expand the selection if requested.
    275276    switch (m_granularity) {
    276277        case CharacterGranularity:
     
    386387    if (m_end.isNull())
    387388        m_end = m_start;
    388    
    389     adjustForEditableContent();
    390 
    391     // adjust the state
     389}
     390
     391void Selection::updateSelectionType()
     392{
    392393    if (m_start.isNull()) {
    393394        ASSERT(m_end.isNull());
    394         m_state = NONE;
    395 
    396         // enforce downstream affinity if not caret, as affinity only
    397         // makes sense for caret
     395        m_selectionType = NoSelection;
     396    } else if (m_start == m_end || m_start.upstream() == m_end.upstream()) {
     397        m_selectionType = CaretSelection;
     398    } else
     399        m_selectionType = RangeSelection;
     400
     401    // Affinity only makes sense for a caret
     402    if (m_selectionType != CaretSelection)
    398403        m_affinity = DOWNSTREAM;
    399     } else if (m_start == m_end || m_start.upstream() == m_end.upstream()) {
    400         m_state = CARET;
    401     } else {
    402         m_state = RANGE;
    403 
    404         // enforce downstream affinity if not caret, as affinity only
    405         // makes sense for caret
    406         m_affinity = DOWNSTREAM;
    407 
     404}
     405
     406void Selection::validate()
     407{
     408    setBaseAndExtentToDeepEquivalents();
     409    setStartAndEndFromBaseAndExtentRespectingGranularity();
     410    adjustSelectionToAvoidCrossingEditingBoundaries();
     411    updateSelectionType();
     412
     413    if (selectionType() == RangeSelection) {
    408414        // "Constrain" the selection to be the smallest equivalent range of nodes.
    409415        // This is a somewhat arbitrary choice, but experience shows that it is
     
    442448        m_end = base;
    443449    }
    444     m_state = RANGE;
    445 }
    446 
    447 void Selection::adjustForEditableContent()
     450    m_selectionType = RangeSelection;
     451}
     452
     453void Selection::adjustSelectionToAvoidCrossingEditingBoundaries()
    448454{
    449455    if (m_base.isNull() || m_start.isNull() || m_end.isNull())
     
    506512            }
    507513            VisiblePosition previous(p);
    508            
     514
    509515            if (previous.isNull()) {
     516                // The selection crosses an Editing boundary.  This is a
     517                // programmer error in the editing code.  Happy debugging!
    510518                ASSERT_NOT_REACHED();
    511519                m_base = Position();
     
    535543           
    536544            if (next.isNull()) {
     545                // The selection crosses an Editing boundary.  This is a
     546                // programmer error in the editing code.  Happy debugging!
    537547                ASSERT_NOT_REACHED();
    538548                m_base = Position();
  • trunk/WebCore/editing/Selection.h

    r38735 r40738  
    3838class Selection {
    3939public:
    40     enum EState { NONE, CARET, RANGE };
    41     enum EDirection { FORWARD, BACKWARD, RIGHT, LEFT };
     40    enum SelectionType { NoSelection, CaretSelection, RangeSelection };
    4241
    4342    Selection();
     
    5352    static Selection selectionFromContentsOfNode(Node*);
    5453
    55     EState state() const { return m_state; }
     54    SelectionType selectionType() const { return m_selectionType; }
    5655
    5756    void setAffinity(EAffinity affinity) { m_affinity = affinity; }
     
    6463   
    6564    Position base() const { return m_base; }
    66     Position extent() const { return m_extent; }   
     65    Position extent() const { return m_extent; }
    6766    Position start() const { return m_start; }
    6867    Position end() const { return m_end; }
     
    7170    VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
    7271
    73     bool isNone() const { return state() == NONE; }
    74     bool isCaret() const { return state() == CARET; }
    75     bool isRange() const { return state() == RANGE; }
    76     bool isCaretOrRange() const { return state() != NONE; }
     72    bool isNone() const { return selectionType() == NoSelection; }
     73    bool isCaret() const { return selectionType() == CaretSelection; }
     74    bool isRange() const { return selectionType() == RangeSelection; }
     75    bool isCaretOrRange() const { return selectionType() != NoSelection; }
    7776
    7877    bool isBaseFirst() const { return m_baseIsFirst; }
     
    101100private:
    102101    void validate();
    103     void adjustForEditableContent();
    104102
    105     Position m_base;                  // base position for the selection
    106     Position m_extent;                // extent position for the selection
    107     Position m_start;                 // start position for the selection
    108     Position m_end;                   // end position for the selection
     103    // Support methods for validate()
     104    void setBaseAndExtentToDeepEquivalents();
     105    void setStartAndEndFromBaseAndExtentRespectingGranularity();
     106    void adjustSelectionToAvoidCrossingEditingBoundaries();
     107    void updateSelectionType();
    109108
    110     EAffinity m_affinity;             // the upstream/downstream affinity of the caret
    111     TextGranularity m_granularity;   // granularity of start/end selection
     109    Position m_base;   // Where the first click happened
     110    Position m_extent; // Where the end click happened
     111    Position m_start;  // Leftmost position when expanded to respect granularity
     112    Position m_end;    // Rightmost position when expanded to respect granularity
     113
     114    EAffinity m_affinity;           // the upstream/downstream affinity of the caret
     115    TextGranularity m_granularity;  // granularity of start/end selection
    112116
    113117    // these are cached, can be recalculated by validate()
    114     EState m_state;                   // the state of the selection
     118    SelectionType m_selectionType;    // None, Caret, Range
    115119    bool m_baseIsFirst;               // true if base is before the extent
    116120};
  • trunk/WebCore/editing/SelectionController.h

    r39069 r40738  
    6868    bool contains(const IntPoint&);
    6969
    70     Selection::EState state() const { return m_sel.state(); }
     70    Selection::SelectionType selectionType() const { return m_sel.selectionType(); }
    7171
    7272    EAffinity affinity() const { return m_sel.affinity(); }
  • trunk/WebCore/editing/TypingCommand.cpp

    r40710 r40738  
    374374    Selection selectionAfterUndo;
    375375   
    376     switch (endingSelection().state()) {
    377         case Selection::RANGE:
     376    switch (endingSelection().selectionType()) {
     377        case Selection::RangeSelection:
    378378            selectionToDelete = endingSelection();
    379379            selectionAfterUndo = selectionToDelete;
    380380            break;
    381         case Selection::CARET: {
     381        case Selection::CaretSelection: {
    382382            if (breakOutOfEmptyMailBlockquotedParagraph()) {
    383383                typingAddedToOpenCommand();
     
    432432            break;
    433433        }
    434         case Selection::NONE:
     434        case Selection::NoSelection:
    435435            ASSERT_NOT_REACHED();
    436436            break;
     
    456456    Selection selectionAfterUndo;
    457457
    458     switch (endingSelection().state()) {
    459         case Selection::RANGE:
     458    switch (endingSelection().selectionType()) {
     459        case Selection::RangeSelection:
    460460            selectionToDelete = endingSelection();
    461461            selectionAfterUndo = selectionToDelete;
    462462            break;
    463         case Selection::CARET: {
     463        case Selection::CaretSelection: {
    464464            m_smartDelete = false;
    465465
     
    510510            break;
    511511        }
    512         case Selection::NONE:
     512        case Selection::NoSelection:
    513513            ASSERT_NOT_REACHED();
    514514            break;
  • trunk/WebCore/page/Frame.cpp

    r40473 r40738  
    12511251}
    12521252
    1253 // FIXME: should this go in SelectionController?
    12541253void Frame::revealSelection(const RenderLayer::ScrollAlignment& alignment) const
    12551254{
    12561255    IntRect rect;
    1257    
    1258     switch (selection()->state()) {
    1259         case Selection::NONE:
     1256
     1257    switch (selection()->selectionType()) {
     1258        case Selection::NoSelection:
    12601259            return;
    1261            
    1262         case Selection::CARET:
     1260        case Selection::CaretSelection:
    12631261            rect = selection()->absoluteCaretBounds();
    12641262            break;
    1265            
    1266         case Selection::RANGE:
     1263        case Selection::RangeSelection:
    12671264            rect = enclosingIntRect(selectionBounds(false));
    12681265            break;
     
    12701267
    12711268    Position start = selection()->start();
    1272 
    12731269    ASSERT(start.node());
    12741270    if (start.node() && start.node()->renderer()) {
     
    12761272        // the selection rect could intersect more than just that.
    12771273        // See <rdar://problem/4799899>.
    1278         if (RenderLayer *layer = start.node()->renderer()->enclosingLayer())
     1274        if (RenderLayer* layer = start.node()->renderer()->enclosingLayer())
    12791275            layer->scrollRectToVisible(rect, false, alignment, alignment);
    12801276    }
Note: See TracChangeset for help on using the changeset viewer.