if (!(aFlags & NS_EVENT_FLAG_CAPTURE) && !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) && clickOrExtActivate) {
if (outerActivateEvent) {
nsHTMLInputElement::HandleDOMEvent(nsPresContext* aPresContext,                                   nsEvent* aEvent,                                   nsIDOMEvent** aDOMEvent,                                   PRUint32 aFlags,                                   nsEventStatus* aEventStatus){  NS_ENSURE_ARG_POINTER(aEventStatus);  // Do not process any DOM events if the element is disabled  PRBool disabled;  nsresult rv = GetDisabled(&disabled);  NS_ENSURE_SUCCESS(rv, rv);  if (disabled) {    return NS_OK;  }    // For some reason or another we also need to check if the style shows us  // as disabled.  nsIFormControlFrame* formControlFrame = GetFormControlFrame(PR_FALSE);  if (formControlFrame) {    nsIFrame* formFrame = nsnull;    CallQueryInterface(formControlFrame, &formFrame);    if (formFrame) {      const nsStyleUserInterface* uiStyle = formFrame->GetStyleUserInterface();      if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE ||          uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED) {        return NS_OK;      }    }  }  //  // Web pages expect the value of a radio button or checkbox to be set  // *before* onclick and DOMActivate fire, and they expect that if they set  // the value explicitly during onclick or DOMActivate it will not be toggled  // or any such nonsense.  // In order to support that (bug 57137 and 58460 are examples) we toggle  // the checked attribute *first*, and then fire onclick.  If the user  // returns false, we reset the control to the old checked value.  Otherwise,  // we dispatch DOMActivate.  If DOMActivate is cancelled, we also reset  // the control to the old checked value.  We need to keep track of whether  // we've already toggled the state from onclick since the user could  // explicitly dispatch DOMActivate on the element.  //  // This is a compatibility hack.  //  // Track whether we're in the "outer" HandleDOMEvent invocation  PRBool clickOrExtActivate =    (aEvent->message == NS_MOUSE_LEFT_CLICK ||     (aEvent->message == NS_UI_ACTIVATE &&      !GET_BOOLBIT(mBitField, BF_IN_INTERNAL_ACTIVATE)));  PRBool originalCheckedValue = PR_FALSE;  nsCOMPtr<nsIDOMHTMLInputElement> selectedRadioButton;  if (!(aFlags & NS_EVENT_FLAG_CAPTURE) &&      !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) &&      clickOrExtActivate) {    SET_BOOLBIT(mBitField, BF_CHECKED_IS_TOGGLED, PR_FALSE);    switch(mType) {      case NS_FORM_INPUT_CHECKBOX:        {          GetChecked(&originalCheckedValue);          DoSetChecked(!originalCheckedValue);          SET_BOOLBIT(mBitField, BF_CHECKED_IS_TOGGLED, PR_TRUE);        }        break;      case NS_FORM_INPUT_RADIO:        {          nsCOMPtr<nsIRadioGroupContainer> container = GetRadioGroupContainer();          if (container) {            nsAutoString name;            if (GetNameIfExists(name)) {              container->GetCurrentRadioButton(name,                                               getter_AddRefs(selectedRadioButton));            }          }          GetChecked(&originalCheckedValue);          if (!originalCheckedValue) {            DoSetChecked(PR_TRUE);            SET_BOOLBIT(mBitField, BF_CHECKED_IS_TOGGLED, PR_TRUE);          }        }        break;      case NS_FORM_INPUT_SUBMIT:      case NS_FORM_INPUT_IMAGE:        if(mForm) {          // tell the form that we are about to enter a click handler.          // that means that if there are scripted submissions, the          // latest one will be deferred until after the exit point of the handler.           mForm->OnSubmitClickBegin();        }        break;      default:        break;    } //switch  }  // If NS_EVENT_FLAG_NO_CONTENT_DISPATCH is set we will not allow content to handle  // this event.  But to allow middle mouse button paste to work we must allow   // middle clicks to go to text fields anyway.  PRBool noContentDispatch = aEvent->flags & NS_EVENT_FLAG_NO_CONTENT_DISPATCH;  if ((mType == NS_FORM_INPUT_TEXT || mType == NS_FORM_INPUT_PASSWORD) &&      aEvent->message == NS_MOUSE_MIDDLE_CLICK) {    aEvent->flags &= ~NS_EVENT_FLAG_NO_CONTENT_DISPATCH;  }  // We must cache type because mType may change during JS event (bug 2369)  //  PRInt32 oldType = mType;  // Try script event handlers first if its not a focus/blur event  //we dont want the doc to get these  rv = nsGenericHTMLFormElement::HandleDOMEvent(aPresContext, aEvent,                                                aDOMEvent, aFlags,                                                aEventStatus);  // Ideally we would make the default action for click and space just dispatch  // DOMActivate, and the default action for DOMActivate flip the checkbox/  // radio state and fire onchange.  However, for backwards compatibility, we  // need to flip the state before firing click, and we need to fire click  // when space is pressed.  So, we just nest the firing of DOMActivate inside  // the click event handling, and allow cancellation of DOMActivate to cancel  // the click.  if (*aEventStatus != nsEventStatus_eConsumeNoDefault &&      !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) &&      aEvent->message == NS_MOUSE_LEFT_CLICK && mType != NS_FORM_INPUT_TEXT) {    nsUIEvent actEvent(NS_UI_ACTIVATE, 1);    nsIPresShell *shell = aPresContext->GetPresShell();    if (shell) {      nsEventStatus status = nsEventStatus_eIgnore;      SET_BOOLBIT(mBitField, BF_IN_INTERNAL_ACTIVATE, PR_TRUE);      rv = shell->HandleDOMEventWithTarget(this, &actEvent, &status);      SET_BOOLBIT(mBitField, BF_IN_INTERNAL_ACTIVATE, PR_FALSE);      // If activate is cancelled, we must do the same as when click is      // cancelled (revert the checkbox to its original value).      if (status == nsEventStatus_eConsumeNoDefault)        *aEventStatus = status;    }  }  if (!(aFlags & NS_EVENT_FLAG_CAPTURE) &&      !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) &&      clickOrExtActivate) {    switch(oldType) {      case NS_FORM_INPUT_SUBMIT:      case NS_FORM_INPUT_IMAGE:        if(mForm) {          // tell the form that we are about to exit a click handler          // so the form knows not to defer subsequent submissions          // the pending ones that were created during the handler          // will be flushed or forgoten.          mForm->OnSubmitClickEnd();        }        break;    } //switch  }  // Reset the flag for other content besides this text field  aEvent->flags |= noContentDispatch ? NS_EVENT_FLAG_NO_CONTENT_DISPATCH : NS_EVENT_FLAG_NONE;  // now check to see if the event was "cancelled"  if (GET_BOOLBIT(mBitField, BF_CHECKED_IS_TOGGLED) && clickOrExtActivate) {    if (*aEventStatus == nsEventStatus_eConsumeNoDefault) {      // if it was cancelled and a radio button, then set the old      // selected btn to TRUE. if it is a checkbox then set it to its      // original value      if (selectedRadioButton) {        selectedRadioButton->SetChecked(PR_TRUE);        // If this one is no longer a radio button we must reset it back to        // false to cancel the action.  See how the web of hack grows?        if (mType != NS_FORM_INPUT_RADIO) {          DoSetChecked(PR_FALSE);        }      } else if (oldType == NS_FORM_INPUT_CHECKBOX) {        DoSetChecked(originalCheckedValue);      }    } else {      FireOnChange();#ifdef ACCESSIBILITY      // Fire an event to notify accessibility      if (mType == NS_FORM_INPUT_CHECKBOX) {        FireEventForAccessibility(aPresContext,                                  NS_LITERAL_STRING("CheckboxStateChange"));      } else {        FireEventForAccessibility(aPresContext,                                  NS_LITERAL_STRING("RadioStateChange"));      }#endif    }  }  if (NS_SUCCEEDED(rv) &&       !(aFlags & NS_EVENT_FLAG_CAPTURE) &&      !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT)) {    if (nsEventStatus_eIgnore == *aEventStatus) {      switch (aEvent->message) {        case NS_FOCUS_CONTENT:        {          // Check to see if focus has bubbled up from a form control's          // child textfield or button.  If that's the case, don't focus          // this parent file control -- leave focus on the child.          nsIFormControlFrame* formControlFrame = GetFormControlFrame(PR_FALSE);          if (formControlFrame && !(aFlags & NS_EVENT_FLAG_BUBBLE) &&              ShouldFocus(this))            formControlFrame->SetFocus(PR_TRUE, PR_TRUE);        }                                                                                 break; // NS_FOCUS_CONTENT        case NS_KEY_PRESS:        case NS_KEY_UP:        {          // For backwards compat, trigger checks/radios/buttons with          // space or enter (bug 25300)          nsKeyEvent * keyEvent = (nsKeyEvent *)aEvent;          if ((aEvent->message == NS_KEY_PRESS &&               keyEvent->keyCode == NS_VK_RETURN) ||              (aEvent->message == NS_KEY_UP &&               keyEvent->keyCode == NS_VK_SPACE)) {            switch(mType) {              case NS_FORM_INPUT_CHECKBOX:              case NS_FORM_INPUT_RADIO:              {                // Checkbox and Radio try to submit on Enter press                if (keyEvent->keyCode != NS_VK_SPACE) {                  MaybeSubmitForm(aPresContext);                  break;  // If we are submitting, do not send click event                }                // else fall through and treat Space like click...              }              case NS_FORM_INPUT_BUTTON:              case NS_FORM_INPUT_RESET:              case NS_FORM_INPUT_SUBMIT:              case NS_FORM_INPUT_IMAGE: // Bug 34418              {                nsEventStatus status = nsEventStatus_eIgnore;                nsMouseEvent event(NS_MOUSE_LEFT_CLICK);                rv = HandleDOMEvent(aPresContext, &event, nsnull,                                    NS_EVENT_FLAG_INIT, &status);              } // case            } // switch          }          if (aEvent->message == NS_KEY_PRESS && mType == NS_FORM_INPUT_RADIO &&              !keyEvent->isAlt && !keyEvent->isControl && !keyEvent->isMeta) {            PRBool isMovingBack = PR_FALSE;            switch (keyEvent->keyCode) {              case NS_VK_UP:               case NS_VK_LEFT:                isMovingBack = PR_TRUE;              case NS_VK_DOWN:              case NS_VK_RIGHT:              // Arrow key pressed, focus+select prev/next radio button              nsCOMPtr<nsIRadioGroupContainer> container = GetRadioGroupContainer();              if (container) {                nsAutoString name;                if (GetNameIfExists(name)) {                  container->GetNextRadioButton(name, isMovingBack, this,                                                getter_AddRefs(selectedRadioButton));                  nsCOMPtr<nsIContent> radioContent =                    do_QueryInterface(selectedRadioButton);                  if (radioContent) {                    rv = selectedRadioButton->Focus();                    if (NS_SUCCEEDED(rv)) {                      nsEventStatus status = nsEventStatus_eIgnore;                      nsMouseEvent event(NS_MOUSE_LEFT_CLICK);                      rv = radioContent->HandleDOMEvent(aPresContext, &event,                                                        nsnull, NS_EVENT_FLAG_INIT,                                                         &status);                      if (NS_SUCCEEDED(rv)) {                        *aEventStatus = nsEventStatus_eConsumeNoDefault;                      }                    }                  }                }              }            }          }          /*           * If this is input type=text, and the user hit enter, fire onChange           * and submit the form (if we are in one)           *           * Bug 99920, bug 109463 and bug 147850:           * (a) if there is a submit control in the form, click the first           *     submit control in the form.           * (b) if there is just one text control in the form, submit by           *     sending a submit event directly to the form           * (c) if there is more than one text input and no submit buttons, do           *     not submit, period.           */          if (aEvent->message == NS_KEY_PRESS &&              (keyEvent->keyCode == NS_VK_RETURN ||               keyEvent->keyCode == NS_VK_ENTER) &&              (mType == NS_FORM_INPUT_TEXT ||               mType == NS_FORM_INPUT_PASSWORD ||               mType == NS_FORM_INPUT_FILE)) {            PRBool isButton = PR_FALSE;            // If this is an enter on the button of a file input, don't submit            // -- that's supposed to put up the filepicker            if (mType == NS_FORM_INPUT_FILE && aDOMEvent) {              nsCOMPtr<nsIDOMNSEvent> nsEvent(do_QueryInterface(*aDOMEvent));              if (nsEvent) {                nsCOMPtr<nsIDOMEventTarget> originalTarget;                nsEvent->GetOriginalTarget(getter_AddRefs(originalTarget));                nsCOMPtr<nsIContent> maybeButton(do_QueryInterface(originalTarget));                if (maybeButton) {                  nsAutoString type;                  maybeButton->GetAttr(kNameSpaceID_None, nsHTMLAtoms::type,                                       type);                  isButton = type.EqualsLiteral("button");                }              }            }                        if (!isButton) {              nsIFrame* primaryFrame = GetPrimaryFrame(PR_FALSE);              if (primaryFrame) {                nsITextControlFrame* textFrame = nsnull;                CallQueryInterface(primaryFrame, &textFrame);                              // Fire onChange (if necessary)                if (textFrame) {                  textFrame->CheckFireOnChange();                }              }              rv = MaybeSubmitForm(aPresContext);              NS_ENSURE_SUCCESS(rv, rv);            }          }        } break; // NS_KEY_PRESS || NS_KEY_UP        // cancel all of these events for buttons        case NS_MOUSE_MIDDLE_BUTTON_DOWN:        case NS_MOUSE_MIDDLE_BUTTON_UP:        case NS_MOUSE_MIDDLE_DOUBLECLICK:        case NS_MOUSE_RIGHT_DOUBLECLICK:        case NS_MOUSE_RIGHT_BUTTON_DOWN:        case NS_MOUSE_RIGHT_BUTTON_UP:        {          if (mType == NS_FORM_INPUT_BUTTON ||               mType == NS_FORM_INPUT_RESET ||               mType == NS_FORM_INPUT_SUBMIT ) {            nsCOMPtr<nsIDOMNSEvent> nsevent;            if (aDOMEvent) {              nsevent = do_QueryInterface(*aDOMEvent);            }            if (nsevent) {              nsevent->PreventBubble();            } else {              rv = NS_ERROR_FAILURE;            }          }          break;        }      default:        break;      }      if (clickOrExtActivate) {        if (mForm && (oldType == NS_FORM_INPUT_SUBMIT ||                      oldType == NS_FORM_INPUT_IMAGE)) {          if (mType != NS_FORM_INPUT_SUBMIT && mType != NS_FORM_INPUT_IMAGE) {            // If the type has changed to a non-submit type, then we want to            // flush the stored submission if there is one (as if the submit()            // was allowed to succeed)            mForm->FlushPendingSubmission();          }        }        switch(mType) {        case NS_FORM_INPUT_RESET:        case NS_FORM_INPUT_SUBMIT:        case NS_FORM_INPUT_IMAGE:          if (mForm) {            nsFormEvent event((mType == NS_FORM_INPUT_RESET) ?                              NS_FORM_RESET : NS_FORM_SUBMIT);            event.originator      = this;            nsEventStatus status  = nsEventStatus_eIgnore;            nsIPresShell *presShell = aPresContext->GetPresShell();            // If |nsIPresShell::Destroy| has been called due to            // handling the event (base class HandleDOMEvent, above),            // the pres context will return a null pres shell.  See            // bug 125624.            if (presShell) {              nsCOMPtr<nsIContent> form(do_QueryInterface(mForm));              presShell->HandleDOMEventWithTarget(form, &event, &status);            }          }          break;        default:          break;        } //switch       } //click or outer activate event    } else {      if (clickOrExtActivate &&          (oldType == NS_FORM_INPUT_SUBMIT || oldType == NS_FORM_INPUT_IMAGE) &&          mForm) {        // tell the form to flush a possible pending submission.        // the reason is that the script returned false (the event was        // not ignored) so if there is a stored submission, it needs to        // be submitted immediately.        mForm->FlushPendingSubmission();      }    } //if  } // if  return rv;}