Bug 1278014 part.3 Define 2 constants for number of SelectionType and number of SelectionType except "none" r?smaug draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 11 Jun 2016 11:12:26 +0900
changeset 377648 dd16a236ffd9fa152f2a985c63c4e7263845a994
parent 377647 99021b91157ba83400a4241eebb231a678628b82
child 377649 af4076e96f5a3559c8e05e95f6dbfceb7edbc559
push id20850
push usermasayuki@d-toybox.com
push dateSat, 11 Jun 2016 06:51:43 +0000
reviewerssmaug
bugs1278014
milestone50.0a1
Bug 1278014 part.3 Define 2 constants for number of SelectionType and number of SelectionType except "none" r?smaug This patch defines two constants kSelectionTypeCount and kPresentSelectionTypeCount. The former is same as nsISelectionController::NUM_SELECTIONTYPES. The latter is kSelectionTypeCount - 1 for excluding SELECTION_NONE. The latter is useful in some loops which handle all selection types except SELECTION_NONE. Note that this patch fixes a bug of nsFrameSelection. That doesn't treat SELECTION_NONE as a selection (see the definition of index), however, it defines redundant item and doesn't use it actually. Additionally, it computes invalid selection type in each loop. Therefore, without this patch, debug build hits MOZ_ASSERT() in ToSelectionType(RawSelectionType). Note that these constants are defined as anonymous enum because we cannot define as const (or static) even with extern. If we'd try to do it, it caused link error or not available in nsFrameSelection.cpp as constant value since they were not initialized if they were initialized in nsSelection.cpp. Therefore, these constants are defined as enum items but using "k" prefix. MozReview-Commit-ID: H6sH7NBEXlE
dom/base/nsISelectionController.idl
editor/libeditor/nsEditor.cpp
layout/generic/nsFrameSelection.h
layout/generic/nsSelection.cpp
layout/generic/nsTextFrame.cpp
--- a/dom/base/nsISelectionController.idl
+++ b/dom/base/nsISelectionController.idl
@@ -297,16 +297,29 @@ enum class SelectionType : RawSelectionT
     nsISelectionController::SELECTION_IME_SELECTEDCONVERTEDTEXT,
   SELECTION_ACCESSIBILITY =
     nsISelectionController::SELECTION_ACCESSIBILITY,
   SELECTION_FIND = nsISelectionController::SELECTION_FIND,
   SELECTION_URLSECONDARY = nsISelectionController::SELECTION_URLSECONDARY,
   SELECTION_URLSTRIKEOUT = nsISelectionController::SELECTION_URLSTRIKEOUT,
 };
 
+// Using anonymous enum to define constants because these constants may be
+// used at defining fixed size array in some header files (e.g.,
+// nsFrameSelection.h).  So, the values needs to be defined here, but we cannot
+// use static/const even with extern since it causes failing to link or
+// initializes them after such headers.
+enum : size_t
+{
+  // kSelectionTypeCount is number of SelectionType.
+  kSelectionTypeCount = nsISelectionController::NUM_SELECTIONTYPES,
+  // kPresentSelectionTypeCount is number of SelectionType except "none".
+  kPresentSelectionTypeCount = kSelectionTypeCount - 1
+};
+
 const char* ToChar(SelectionType aSelectionType);
 SelectionType ToSelectionType(RawSelectionType aRawSelectionType);
 RawSelectionType ToRawSelectionType(SelectionType aSelectionType);
 bool operator &(SelectionType aSelectionType,
                 RawSelectionType aRawSelectionTypes);
 
 } // namespace mozilla
 %}
--- a/editor/libeditor/nsEditor.cpp
+++ b/editor/libeditor/nsEditor.cpp
@@ -2664,17 +2664,17 @@ struct SavedRange {
 
 nsresult
 nsEditor::SplitNodeImpl(nsIContent& aExistingRightNode,
                         int32_t aOffset,
                         nsIContent& aNewLeftNode)
 {
   // Remember all selection points.
   AutoTArray<SavedRange, 10> savedRanges;
-  for (size_t i = 0; i < nsISelectionController::NUM_SELECTIONTYPES - 1; ++i) {
+  for (size_t i = 0; i < kPresentSelectionTypeCount; ++i) {
     SelectionType selectionType(ToSelectionType(1 << i));
     SavedRange range;
     range.mSelection = GetSelection(selectionType);
     if (selectionType == SelectionType::SELECTION_NORMAL) {
       NS_ENSURE_TRUE(range.mSelection, NS_ERROR_NULL_POINTER);
     } else if (!range.mSelection) {
       // For non-normal selections, skip over the non-existing ones.
       continue;
@@ -2810,17 +2810,17 @@ nsEditor::JoinNodesImpl(nsINode* aNodeTo
 
   int32_t joinOffset;
   GetNodeLocation(aNodeToJoin, &joinOffset);
   int32_t keepOffset;
   nsINode* parent = GetNodeLocation(aNodeToKeep, &keepOffset);
 
   // Remember all selection points.
   AutoTArray<SavedRange, 10> savedRanges;
-  for (size_t i = 0; i < nsISelectionController::NUM_SELECTIONTYPES - 1; ++i) {
+  for (size_t i = 0; i < kPresentSelectionTypeCount; ++i) {
     SelectionType selectionType(ToSelectionType(1 << i));
     SavedRange range;
     range.mSelection = GetSelection(selectionType);
     if (selectionType == SelectionType::SELECTION_NORMAL) {
       NS_ENSURE_TRUE(range.mSelection, NS_ERROR_NULL_POINTER);
     } else if (!range.mSelection) {
       // For non-normal selections, skip over the non-existing ones.
       continue;
--- a/layout/generic/nsFrameSelection.h
+++ b/layout/generic/nsFrameSelection.h
@@ -677,17 +677,18 @@ private:
   uint32_t     GetBatching() const {return mBatching; }
   bool         GetNotifyFrames() const { return mNotifyFrames; }
   void         SetDirty(bool aDirty=true){if (mBatching) mChangesDuringBatching = aDirty;}
 
   // nsFrameSelection may get deleted when calling this,
   // so remember to use nsCOMPtr when needed.
   nsresult     NotifySelectionListeners(mozilla::SelectionType aSelectionType);
 
-  RefPtr<mozilla::dom::Selection> mDomSelections[nsISelectionController::NUM_SELECTIONTYPES];
+  RefPtr<mozilla::dom::Selection>
+    mDomSelections[mozilla::kPresentSelectionTypeCount];
 
   // Table selection support.
   nsITableCellLayout* GetCellLayout(nsIContent *aCellContent) const;
 
   nsresult SelectBlockOfCells(nsIContent *aStartNode, nsIContent *aEndNode);
   nsresult SelectRowOrColumn(nsIContent *aCellContent, uint32_t aTarget);
   nsresult UnselectCells(nsIContent *aTable,
                          int32_t aStartRowIndex, int32_t aStartColumnIndex,
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -524,18 +524,17 @@ struct MOZ_RAII AutoPrepareFocusRange
 };
 
 } // namespace mozilla
 
 ////////////BEGIN nsFrameSelection methods
 
 nsFrameSelection::nsFrameSelection()
 {
-  int32_t i;
-  for (i = 0;i<nsISelectionController::NUM_SELECTIONTYPES;i++){
+  for (size_t i = 0; i < kPresentSelectionTypeCount; i++){
     mDomSelections[i] = new Selection(this);
     mDomSelections[i]->SetType(GetSelectionTypeFromIndex(i));
   }
   mBatching = 0;
   mChangesDuringBatching = false;
   mNotifyFrames = true;
   
   mMouseDoubleDownState = false;
@@ -574,18 +573,17 @@ nsFrameSelection::nsFrameSelection()
 
 nsFrameSelection::~nsFrameSelection()
 {
 }
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsFrameSelection)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsFrameSelection)
-  int32_t i;
-  for (i = 0; i < nsISelectionController::NUM_SELECTIONTYPES; ++i) {
+  for (size_t i = 0; i < kPresentSelectionTypeCount; ++i) {
     tmp->mDomSelections[i] = nullptr;
   }
 
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mCellParent)
   tmp->mSelectingTableCellMode = 0;
   tmp->mDragSelectingCells = false;
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartSelectedCell)
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndSelectedCell)
@@ -597,18 +595,17 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ns
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsFrameSelection)
   if (tmp->mShell && tmp->mShell->GetDocument() &&
       nsCCUncollectableMarker::InGeneration(cb,
                                             tmp->mShell->GetDocument()->
                                               GetMarkedCCGeneration())) {
     return NS_SUCCESS_INTERRUPTED_TRAVERSE;
   }
-  int32_t i;
-  for (i = 0; i < nsISelectionController::NUM_SELECTIONTYPES; ++i) {
+  for (size_t i = 0; i < kPresentSelectionTypeCount; ++i) {
     NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDomSelections[i])
   }
 
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCellParent)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartSelectedCell)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndSelectedCell)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAppendStartSelectedCell)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mUnselectCellOnMouseUp)
@@ -1875,17 +1872,17 @@ nsFrameSelection::LookUpSelection(nsICon
                                   int32_t aContentLength,
                                   bool aSlowCheck) const
 {
   if (!aContent || !mShell)
     return nullptr;
 
   SelectionDetails* details = nullptr;
 
-  for (int32_t j = 0; j < nsISelectionController::NUM_SELECTIONTYPES; j++) {
+  for (size_t j = 0; j < kPresentSelectionTypeCount; j++) {
     if (mDomSelections[j]) {
       mDomSelections[j]->LookUpSelection(aContent, aContentOffset,
                                          aContentLength, &details,
                                          ToSelectionType(1 << j),
                                          aSlowCheck);
     }
   }
 
@@ -3417,17 +3414,17 @@ nsFrameSelection::DisconnectFromPresShel
 {
   RefPtr<AccessibleCaretEventHub> eventHub = mShell->GetAccessibleCaretEventHub();
   if (eventHub) {
     int8_t index = GetIndexFromSelectionType(SelectionType::SELECTION_NORMAL);
     mDomSelections[index]->RemoveSelectionListener(eventHub);
   }
 
   StopAutoScrollTimer();
-  for (int32_t i = 0; i < nsISelectionController::NUM_SELECTIONTYPES; i++) {
+  for (size_t i = 0; i < kPresentSelectionTypeCount; i++) {
     mDomSelections[i]->Clear(nullptr);
   }
   mShell = nullptr;
 }
 
 //END nsISelection interface implementations
 
 #if 0
--- a/layout/generic/nsTextFrame.cpp
+++ b/layout/generic/nsTextFrame.cpp
@@ -6195,18 +6195,17 @@ nsTextFrame::PaintTextWithSelection(
     DestroySelectionDetails(details);
     return false;
   }
   // Iterate through just the selection rawSelectionTypes that paint decorations
   // and paint decorations for any that actually occur in this frame. Paint
   // higher-numbered selection rawSelectionTypes below lower-numered ones on the
   // general principal that lower-numbered selections are higher priority.
   allRawSelectionTypes &= kRawSelectionTypesWithDecorations;
-  for (int32_t i = nsISelectionController::NUM_SELECTIONTYPES - 1;
-       i >= 1; --i) {
+  for (size_t i = kSelectionTypeCount - 1; i >= 1; --i) {
     SelectionType selectionType = ToSelectionType(1 << (i - 1));
     if (selectionType & allRawSelectionTypes) {
       // There is some selection of this selectionType. Try to paint its
       // decorations (there might not be any for this type but that's OK,
       // PaintTextSelectionDecorations will exit early).
       PaintTextSelectionDecorations(aParams, details, selectionType);
     }
   }