if (optionsDeselected) { CheckSelectSomething();
if (optionsDeselected && aStartIndex != -1) { optionsSelected = CheckSelectSomething() || optionsSelected;
nsHTMLSelectElement::SetOptionsSelectedByIndex(PRInt32 aStartIndex,                                               PRInt32 aEndIndex,                                               PRBool aIsSelected,                                               PRBool aClearAll,                                               PRBool aSetDisabled,                                               PRBool aNotify,                                               PRBool* aChangedSomething){#if 0  printf("SetOption(%d-%d, %c, ClearAll=%c)\n", aStartIndex, aEndIndex,                                       (aIsSelected ? 'Y' : 'N'),                                       (aClearAll ? 'Y' : 'N'));#endif  if (aChangedSomething) {    *aChangedSomething = PR_FALSE;  }  nsresult rv;  // Don't bother if the select is disabled  if (!aSetDisabled) {    PRBool selectIsDisabled = PR_FALSE;    rv = GetDisabled(&selectIsDisabled);    if (NS_SUCCEEDED(rv) && selectIsDisabled) {      return NS_OK;    }  }  // Don't bother if there are no options  PRUint32 numItems = 0;  GetLength(&numItems);  if (numItems == 0) {    return NS_OK;  }  // First, find out whether multiple items can be selected  PRBool isMultiple;  rv = GetMultiple(&isMultiple);  if (NS_FAILED(rv)) {    isMultiple = PR_FALSE;  }  // These variables tell us whether any options were selected  // or deselected.  PRBool optionsSelected = PR_FALSE;  PRBool optionsDeselected = PR_FALSE;  nsISelectControlFrame *selectFrame = nsnull;  PRBool did_get_frame = PR_FALSE;  nsCOMPtr<nsIPresContext> presContext;  GetPresContext(this, getter_AddRefs(presContext));  if (aIsSelected) {    // Only select the first value if it's not multiple    if (!isMultiple) {      aEndIndex = aStartIndex;    }    // This variable tells whether or not all of the options we attempted to    // select are disabled.  If ClearAll is passed in as true, and we do not    // select anything because the options are disabled, we will not clear the    // other options.  (This is to make the UI work the way one might expect.)    PRBool allDisabled = !aSetDisabled;    //    // Save a little time when clearing other options    //    PRInt32 previousSelectedIndex = mSelectedIndex;    //    // Select the requested indices    //    // If index is -1, everything will be deselected (bug 28143)    if (aStartIndex != -1) {      // Verify that the indices are within bounds      if (aStartIndex >= (PRInt32)numItems || aStartIndex < 0         || aEndIndex >= (PRInt32)numItems || aEndIndex < 0) {        return NS_ERROR_FAILURE;      }      // Loop through the options and select them (if they are not disabled and      // if they are not already selected).      for (PRInt32 optIndex = aStartIndex; optIndex <= aEndIndex; optIndex++) {        // Ignore disabled options.        if (!aSetDisabled) {          PRBool isDisabled;          IsOptionDisabled(optIndex, &isDisabled);          if (isDisabled) {            continue;          } else {            allDisabled = PR_FALSE;          }        }        nsCOMPtr<nsIDOMHTMLOptionElement> option;        mOptions->ItemAsOption(optIndex, getter_AddRefs(option));        if (option) {          // If the index is already selected, ignore it.          PRBool isSelected = PR_FALSE;          option->GetSelected(&isSelected);          if (!isSelected) {            // To notify the frame if anything gets changed. No need            // to flush here, if there's no frame yet we don't need to            // force it to be created just to notify it about a change            // in the select.            selectFrame = GetSelectFrame();            did_get_frame = PR_TRUE;            OnOptionSelected(selectFrame, presContext, optIndex, PR_TRUE, aNotify);            optionsSelected = PR_TRUE;          }        }      }    }    // Next remove all other options if single select or all is clear    // If index is -1, everything will be deselected (bug 28143)    if (((!isMultiple && optionsSelected)       || (aClearAll && !allDisabled)       || aStartIndex == -1)       && previousSelectedIndex != -1) {      for (PRInt32 optIndex = previousSelectedIndex;           optIndex < (PRInt32)numItems;           optIndex++) {        if (optIndex < aStartIndex || optIndex > aEndIndex) {          nsCOMPtr<nsIDOMHTMLOptionElement> option;          mOptions->ItemAsOption(optIndex, getter_AddRefs(option));          if (option) {            // If the index is already selected, ignore it.            PRBool isSelected = PR_FALSE;            option->GetSelected(&isSelected);            if (isSelected) {              if (!did_get_frame) {                // To notify the frame if anything gets changed, don't                // flush, if the frame doesn't exist we don't need to                // create it just to tell it about this change.                selectFrame = GetSelectFrame();                did_get_frame = PR_TRUE;              }              OnOptionSelected(selectFrame, presContext, optIndex, PR_FALSE, aNotify);              optionsDeselected = PR_TRUE;              // Only need to deselect one option if not multiple              if (!isMultiple) {                break;              }            }          }        }      }    }  } else {    // If we're deselecting, loop through all selected items and deselect    // any that are in the specified range.    for (PRInt32 optIndex = aStartIndex; optIndex <= aEndIndex; optIndex++) {      if (!aSetDisabled) {        PRBool isDisabled;        IsOptionDisabled(optIndex, &isDisabled);        if (isDisabled) {          continue;        }      }      nsCOMPtr<nsIDOMHTMLOptionElement> option;      mOptions->ItemAsOption(optIndex, getter_AddRefs(option));      if (option) {        // If the index is already selected, ignore it.        PRBool isSelected = PR_FALSE;        option->GetSelected(&isSelected);        if (isSelected) {          if (!did_get_frame) {            // To notify the frame if anything gets changed, don't            // flush, if the frame doesn't exist we don't need to            // create it just to tell it about this change.            selectFrame = GetSelectFrame();            did_get_frame = PR_TRUE;          }          OnOptionSelected(selectFrame, presContext, optIndex, PR_FALSE, aNotify);          optionsDeselected = PR_TRUE;        }      }    }  }  // Make sure something is selected  if (optionsDeselected) {    CheckSelectSomething();  }  // Let the caller know whether anything was changed  if (optionsSelected || optionsDeselected) {    if (aChangedSomething)      *aChangedSomething = PR_TRUE;    // Dispatch an event to notify the subcontent that the selected item has changed    DispatchDOMEvent(NS_LITERAL_STRING("selectedItemChanged"));  }  return NS_OK;}