Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2004 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 #ifndef VisibleSelection_h
     27 #define VisibleSelection_h
     28 
     29 #include "core/editing/SelectionType.h"
     30 #include "core/editing/TextGranularity.h"
     31 #include "core/editing/VisiblePosition.h"
     32 
     33 namespace WebCore {
     34 
     35 class LayoutPoint;
     36 class Position;
     37 
     38 const EAffinity SEL_DEFAULT_AFFINITY = DOWNSTREAM;
     39 enum SelectionDirection { DirectionForward, DirectionBackward, DirectionRight, DirectionLeft };
     40 
     41 class VisibleSelection {
     42 public:
     43     VisibleSelection();
     44 
     45     VisibleSelection(const Position&, EAffinity, bool isDirectional = false);
     46     VisibleSelection(const Position&, const Position&, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
     47 
     48     VisibleSelection(const Range*, EAffinity = SEL_DEFAULT_AFFINITY, bool isDirectional = false);
     49 
     50     VisibleSelection(const VisiblePosition&, bool isDirectional = false);
     51     VisibleSelection(const VisiblePosition&, const VisiblePosition&, bool isDirectional = false);
     52 
     53     static VisibleSelection selectionFromContentsOfNode(Node*);
     54 
     55     SelectionType selectionType() const { return m_selectionType; }
     56 
     57     void setAffinity(EAffinity affinity) { m_affinity = affinity; }
     58     EAffinity affinity() const { return m_affinity; }
     59 
     60     void setBase(const Position&);
     61     void setBase(const VisiblePosition&);
     62     void setExtent(const Position&);
     63     void setExtent(const VisiblePosition&);
     64 
     65     Position base() const { return m_base; }
     66     Position extent() const { return m_extent; }
     67     Position start() const { return m_start; }
     68     Position end() const { return m_end; }
     69 
     70     VisiblePosition visibleStart() const { return VisiblePosition(m_start, isRange() ? DOWNSTREAM : affinity()); }
     71     VisiblePosition visibleEnd() const { return VisiblePosition(m_end, isRange() ? UPSTREAM : affinity()); }
     72     VisiblePosition visibleBase() const { return VisiblePosition(m_base, isRange() ? (isBaseFirst() ? UPSTREAM : DOWNSTREAM) : affinity()); }
     73     VisiblePosition visibleExtent() const { return VisiblePosition(m_extent, isRange() ? (isBaseFirst() ? DOWNSTREAM : UPSTREAM) : affinity()); }
     74 
     75     bool isNone() const { return selectionType() == NoSelection; }
     76     bool isCaret() const { return selectionType() == CaretSelection; }
     77     bool isRange() const { return selectionType() == RangeSelection; }
     78     bool isCaretOrRange() const { return selectionType() != NoSelection; }
     79     bool isNonOrphanedRange() const { return isRange() && !start().isOrphan() && !end().isOrphan(); }
     80     bool isNonOrphanedCaretOrRange() const { return isCaretOrRange() && !start().isOrphan() && !end().isOrphan(); }
     81 
     82     bool isBaseFirst() const { return m_baseIsFirst; }
     83     bool isDirectional() const { return m_isDirectional; }
     84     void setIsDirectional(bool isDirectional) { m_isDirectional = isDirectional; }
     85 
     86     bool isAll(EditingBoundaryCrossingRule) const;
     87 
     88     void appendTrailingWhitespace();
     89 
     90     bool expandUsingGranularity(TextGranularity granularity);
     91 
     92     // We don't yet support multi-range selections, so we only ever have one range to return.
     93     PassRefPtr<Range> firstRange() const;
     94 
     95     // FIXME: Most callers probably don't want this function, but are using it
     96     // for historical reasons.  toNormalizedRange contracts the range around
     97     // text, and moves the caret upstream before returning the range.
     98     PassRefPtr<Range> toNormalizedRange() const;
     99 
    100     Element* rootEditableElement() const;
    101     bool isContentEditable() const;
    102     bool rendererIsEditable() const;
    103     bool isContentRichlyEditable() const;
    104     // Returns a shadow tree node for legacy shadow trees, a child of the
    105     // ShadowRoot node for new shadow trees, or 0 for non-shadow trees.
    106     Node* nonBoundaryShadowTreeRootNode() const;
    107 
    108     VisiblePosition visiblePositionRespectingEditingBoundary(const LayoutPoint& localPoint, Node* targetNode) const;
    109 
    110 
    111 #ifndef NDEBUG
    112     void debugPosition() const;
    113     void formatForDebugger(char* buffer, unsigned length) const;
    114     void showTreeForThis() const;
    115 #endif
    116 
    117     void setWithoutValidation(const Position&, const Position&);
    118 
    119 private:
    120     void validate(TextGranularity = CharacterGranularity);
    121 
    122     // Support methods for validate()
    123     void setBaseAndExtentToDeepEquivalents();
    124     void setStartAndEndFromBaseAndExtentRespectingGranularity(TextGranularity);
    125     void adjustSelectionToAvoidCrossingShadowBoundaries();
    126     void adjustSelectionToAvoidCrossingEditingBoundaries();
    127     void updateSelectionType();
    128 
    129     // We need to store these as Positions because VisibleSelection is
    130     // used to store values in editing commands for use when
    131     // undoing the command. We need to be able to create a selection that, while currently
    132     // invalid, will be valid once the changes are undone.
    133 
    134     Position m_base;   // Where the first click happened
    135     Position m_extent; // Where the end click happened
    136     Position m_start;  // Leftmost position when expanded to respect granularity
    137     Position m_end;    // Rightmost position when expanded to respect granularity
    138 
    139     EAffinity m_affinity;           // the upstream/downstream affinity of the caret
    140 
    141     // these are cached, can be recalculated by validate()
    142     SelectionType m_selectionType; // None, Caret, Range
    143     bool m_baseIsFirst : 1; // True if base is before the extent
    144     bool m_isDirectional : 1; // Non-directional ignores m_baseIsFirst and selection always extends on shift + arrow key.
    145 };
    146 
    147 inline bool operator==(const VisibleSelection& a, const VisibleSelection& b)
    148 {
    149     return a.start() == b.start() && a.end() == b.end() && a.affinity() == b.affinity() && a.isBaseFirst() == b.isBaseFirst()
    150         && a.isDirectional() == b.isDirectional();
    151 }
    152 
    153 inline bool operator!=(const VisibleSelection& a, const VisibleSelection& b)
    154 {
    155     return !(a == b);
    156 }
    157 
    158 } // namespace WebCore
    159 
    160 #ifndef NDEBUG
    161 // Outside the WebCore namespace for ease of invocation from gdb.
    162 void showTree(const WebCore::VisibleSelection&);
    163 void showTree(const WebCore::VisibleSelection*);
    164 #endif
    165 
    166 #endif // VisibleSelection_h
    167