switch (aEvent->message) {  case NS_FOCUS_CONTENT: { if (formControlFrame) {    if (type != NS_FORM_INPUT_FILE || !(aFlags & NS_EVENT_FLAG_BUBBLE)) { SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_TRUE); formControlFrame->SetFocus(PR_TRUE, PR_TRUE); SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_FALSE); } return NS_OK; } } break;  case NS_KEY_PRESS: case NS_KEY_UP: {   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(type) { case NS_FORM_INPUT_CHECKBOX: case NS_FORM_INPUT_RADIO: {  if (keyEvent->keyCode != NS_VK_SPACE) {  nsCOMPtr<nsIContent> form(do_QueryInterface(mForm));  if (form) { nsCOMPtr<nsIPresShell> shell; aPresContext->GetShell(getter_AddRefs(shell)); if (shell) { nsCOMPtr<nsIContent> formControl = this;  nsFormEvent event; event.eventStructType = NS_FORM_EVENT; event.message         = NS_FORM_SUBMIT; event.originator      = formControl; nsEventStatus status  = nsEventStatus_eIgnore; shell->HandleDOMEventWithTarget(form, &event, &status);
if (nsEventStatus_eIgnore == *aEventStatus) { switch (aEvent->message) {  case NS_FOCUS_CONTENT: { if (formControlFrame) {    if (type != NS_FORM_INPUT_FILE || !(aFlags & NS_EVENT_FLAG_BUBBLE)) { SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_TRUE); formControlFrame->SetFocus(PR_TRUE, PR_TRUE); SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_FALSE); } return NS_OK; } } break;  case NS_KEY_PRESS: case NS_KEY_UP: {   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(type) { case NS_FORM_INPUT_CHECKBOX: case NS_FORM_INPUT_RADIO: {  if (keyEvent->keyCode != NS_VK_SPACE) {  nsCOMPtr<nsIContent> form(do_QueryInterface(mForm));  if (form) { nsCOMPtr<nsIPresShell> shell; aPresContext->GetShell(getter_AddRefs(shell)); if (shell) { nsCOMPtr<nsIContent> formControl = this;  nsFormEvent event; event.eventStructType = NS_FORM_EVENT; event.message         = NS_FORM_SUBMIT; event.originator      = formControl; nsEventStatus status  = nsEventStatus_eIgnore; shell->HandleDOMEventWithTarget(form, &event, &status); } } break; }  } case NS_FORM_INPUT_BUTTON: case NS_FORM_INPUT_RESET: case NS_FORM_INPUT_SUBMIT: case NS_FORM_INPUT_IMAGE: { nsEventStatus status = nsEventStatus_eIgnore; nsMouseEvent event; event.eventStructType = NS_MOUSE_EVENT; event.message = NS_MOUSE_LEFT_CLICK; event.isShift = PR_FALSE; event.isControl = PR_FALSE; event.isAlt = PR_FALSE; event.isMeta = PR_FALSE; event.clickCount = 0; event.widget = nsnull; rv = HandleDOMEvent(aPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); } } }    if (aEvent->message == NS_KEY_PRESS && (keyEvent->keyCode == NS_VK_RETURN || keyEvent->keyCode == NS_VK_ENTER) && (type == NS_FORM_INPUT_TEXT || type == NS_FORM_INPUT_PASSWORD)) {  if (mForm) { nsIFrame* primaryFrame = GetPrimaryFrame(PR_FALSE); if (primaryFrame) { nsITextControlFrame* textFrame = nsnull; CallQueryInterface(primaryFrame, &textFrame);   if (textFrame) { textFrame->CheckFireOnChange(); } }    nsCOMPtr<nsIContent> submitControl; PRInt32 numTextControlsFound = 0;  nsCOMPtr<nsISimpleEnumerator> formControls; mForm->GetControlEnumerator(getter_AddRefs(formControls));  nsCOMPtr<nsISupports> currentControlSupports; nsCOMPtr<nsIFormControl> currentControl; PRBool hasMoreElements; while (NS_SUCCEEDED(formControls->HasMoreElements(&hasMoreElements)) && hasMoreElements) { formControls->GetNext(getter_AddRefs(currentControlSupports)); currentControl = do_QueryInterface(currentControlSupports); if (currentControl) { PRInt32 type; currentControl->GetType(&type); if (!submitControl && (type == NS_FORM_INPUT_SUBMIT || type == NS_FORM_BUTTON_SUBMIT || type == NS_FORM_INPUT_IMAGE)) { submitControl = do_QueryInterface(currentControl);    break; } else if (type == NS_FORM_INPUT_TEXT || type == NS_FORM_INPUT_PASSWORD) { numTextControlsFound++;
nsHTMLInputElement::HandleDOMEvent(nsIPresContext* aPresContext,                                   nsEvent* aEvent,                                   nsIDOMEvent** aDOMEvent,                                   PRUint32 aFlags,                                   nsEventStatus* aEventStatus){  NS_ENSURE_ARG_POINTER(aEventStatus);  if ((aEvent->message == NS_FOCUS_CONTENT       && GET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT))      || (aEvent->message == NS_BLUR_CONTENT          && GET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT))) {    return NS_OK;  }  nsCOMPtr<nsIDOMEventTarget> oldTarget;        // Do not process any DOM events if the element is disabled  PRBool disabled;  nsresult rv = GetDisabled(&disabled);  if (NS_FAILED(rv) || disabled) {    return rv;  }    // Bugscape 2369: Type might change during JS event handler  //                This pointer is only valid until  //                nsGenericHTMLLeafFormElement::HandleDOMEvent  nsIFormControlFrame* formControlFrame = GetFormControlFrame(PR_FALSE);  if (formControlFrame) {    nsIFrame* formFrame = nsnull;    CallQueryInterface(formControlFrame, &formFrame);    if (formFrame) {      const nsStyleUserInterface* uiStyle;      formFrame->GetStyleData(eStyleStruct_UserInterface,                              (const nsStyleStruct *&)uiStyle);      if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE ||          uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED) {        return NS_OK;      }    }  }  // If we're a file input we have anonymous content underneath  // that we need to hide.  We need to set the event target now  // to ourselves  //  // Bugscape 2369: Type might change during JS event handler  //                This is only valid until  //                nsGenericHTMLLeafFormElement::HandleDOMEvent  //  PRInt32 type;  GetType(&type);  if (type == NS_FORM_INPUT_FILE ||      type == NS_FORM_INPUT_TEXT ) {    // If the event is starting here that's fine.  If it's not    // init'ing here it started beneath us and needs modification.    if (!(NS_EVENT_FLAG_INIT & aFlags)) {      if (!*aDOMEvent) {        // We haven't made a DOMEvent yet.  Force making one now.        nsCOMPtr<nsIEventListenerManager> listenerManager;        rv = GetListenerManager(getter_AddRefs(listenerManager));        if (NS_FAILED(rv)) {          return rv;        }        nsAutoString empty;        rv = listenerManager->CreateEvent(aPresContext, aEvent, empty,                                          aDOMEvent);        if (NS_FAILED(rv)) {          return rv;        }      }      if (!*aDOMEvent) {        return NS_ERROR_FAILURE;      }      nsCOMPtr<nsIPrivateDOMEvent> privateEvent(do_QueryInterface(*aDOMEvent));      if (!privateEvent) {        return NS_ERROR_FAILURE;      }      (*aDOMEvent)->GetTarget(getter_AddRefs(oldTarget));      nsCOMPtr<nsIDOMEventTarget> originalTarget;      nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(*aDOMEvent));      if (nsevent) {        nsevent->GetOriginalTarget(getter_AddRefs(originalTarget));      }      if (!originalTarget) {        privateEvent->SetOriginalTarget(oldTarget);      }      nsCOMPtr<nsIDOMEventTarget>        target(do_QueryInterface((nsIDOMHTMLInputElement*)this));      privateEvent->SetTarget(target);    }  }  // Preset the the value of the checkbox or the radiobutton before  // calling into script If the event gets "cancelled" then we have to  // "back out" this change, but the odds of that are slimmer than it  // being set each time  //  // Start by remember the original value and for radio buttons we  // must get the currently selected radiobtn. So we go get the  // content instead of the frame  //  PRBool originalCheckedValue = PR_FALSE;  PRBool checkWasSet         = PR_FALSE;  nsCOMPtr<nsIDOMHTMLInputElement> selectedRadioButton;  if (!(aFlags & NS_EVENT_FLAG_CAPTURE) && !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) &&      aEvent->message == NS_MOUSE_LEFT_CLICK) {    GetChecked(&originalCheckedValue);    checkWasSet = PR_TRUE;    switch(type) {      case NS_FORM_INPUT_CHECKBOX:        {          PRBool checked;          GetChecked(&checked);          SetChecked(!checked);          FireOnChange();          // Fire an event to notify accessibility#ifdef ACCESSIBILITY          FireEventForAccessibility(aPresContext,                                    NS_LITERAL_STRING("CheckboxStateChange"));#endif        }        break;      case NS_FORM_INPUT_RADIO:        {          nsCOMPtr<nsIRadioGroupContainer> container = GetRadioGroupContainer();          if (container) {            nsAutoString name;            if (GetNameIfExists(name)) {              container->GetCurrentRadioButton(name,                                               getter_AddRefs(selectedRadioButton));            }          }          PRBool checked;          GetChecked(&checked);          if (!checked) {            SetChecked(PR_TRUE);            FireOnChange();          }          // Fire an event to notify accessibility#ifdef ACCESSIBILITY          if (selectedRadioButton != this) {            FireEventForAccessibility(aPresContext,                                      NS_LITERAL_STRING("RadioStateChange"));          }#endif        }        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 ((type == NS_FORM_INPUT_TEXT || type == NS_FORM_INPUT_PASSWORD) &&      aEvent->message == NS_MOUSE_MIDDLE_CLICK) {    aEvent->flags &= ~NS_EVENT_FLAG_NO_CONTENT_DISPATCH;  }  // Try script event handlers first if its not a focus/blur event  //we dont want the doc to get these  rv = nsGenericHTMLLeafFormElement::HandleDOMEvent(aPresContext,                                                    aEvent,                                                    aDOMEvent,                                                    aFlags,                                                    aEventStatus);  // 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 (nsEventStatus_eConsumeNoDefault == *aEventStatus && checkWasSet && !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT)      && (type == NS_FORM_INPUT_CHECKBOX || type == NS_FORM_INPUT_RADIO)) {    // 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);    } else if (type == NS_FORM_INPUT_CHECKBOX) {      SetChecked(originalCheckedValue);    }  }  // Bugscape 2369: Frame might have changed during event handler  formControlFrame = GetFormControlFrame(PR_FALSE);  // Finish the special file control processing...  if (oldTarget) {    // If the event is starting here that's fine.  If it's not    // init'ing here it started beneath us and needs modification.    if (!(NS_EVENT_FLAG_INIT & aFlags)) {      if (!*aDOMEvent) {        return NS_ERROR_FAILURE;      }      nsCOMPtr<nsIPrivateDOMEvent> privateEvent =        do_QueryInterface(*aDOMEvent);      if (!privateEvent) {        return NS_ERROR_FAILURE;      }      // This will reset the target to its original value      privateEvent->SetTarget(oldTarget);    }  }  // Bugscape 2369: type might have changed during event handler  GetType(&type);  if ((NS_OK == rv) && (nsEventStatus_eIgnore == *aEventStatus) &&      !(aFlags & NS_EVENT_FLAG_CAPTURE) &&      !(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT)) {    switch (aEvent->message) {      case NS_FOCUS_CONTENT:      {        if (formControlFrame) {          // If this is a file control, check to see if focus has bubbled          // up from it's child textfield or child button.  If that's the case,           // don't focus this parent file control -- leave focus on the child.          if (type != NS_FORM_INPUT_FILE || !(aFlags & NS_EVENT_FLAG_BUBBLE)) {            SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_TRUE);            formControlFrame->SetFocus(PR_TRUE, PR_TRUE);            SET_BOOLBIT(mBitField, BF_SKIP_FOCUS_EVENT, PR_FALSE);          }          return NS_OK;        }      }                                                                               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(type) {            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) {                // Generate a submit event targeted at the form content                nsCOMPtr<nsIContent> form(do_QueryInterface(mForm));                if (form) {                  nsCOMPtr<nsIPresShell> shell;                  aPresContext->GetShell(getter_AddRefs(shell));                  if (shell) {                    nsCOMPtr<nsIContent> formControl = this; // kungFuDeathGrip                    nsFormEvent event;                    event.eventStructType = NS_FORM_EVENT;                    event.message         = NS_FORM_SUBMIT;                    event.originator      = formControl;                    nsEventStatus status  = nsEventStatus_eIgnore;                    shell->HandleDOMEventWithTarget(form, &event, &status);                  }                }                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;              event.eventStructType = NS_MOUSE_EVENT;              event.message = NS_MOUSE_LEFT_CLICK;              event.isShift = PR_FALSE;              event.isControl = PR_FALSE;              event.isAlt = PR_FALSE;              event.isMeta = PR_FALSE;              event.clickCount = 0;              event.widget = nsnull;              rv = HandleDOMEvent(aPresContext, &event, nsnull,                                  NS_EVENT_FLAG_INIT, &status);            } // case          } // switch        }        /*         * 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) &&            (type == NS_FORM_INPUT_TEXT || type == NS_FORM_INPUT_PASSWORD)) {          if (mForm) {            nsIFrame* primaryFrame = GetPrimaryFrame(PR_FALSE);            if (primaryFrame) {              nsITextControlFrame* textFrame = nsnull;              CallQueryInterface(primaryFrame, &textFrame);              // Fire onChange (if necessary)              if (textFrame) {                textFrame->CheckFireOnChange();              }            }            // Find the first submit control in elements[]            // and also check how many text controls we have in the form            nsCOMPtr<nsIContent> submitControl;            PRInt32 numTextControlsFound = 0;            nsCOMPtr<nsISimpleEnumerator> formControls;            mForm->GetControlEnumerator(getter_AddRefs(formControls));            nsCOMPtr<nsISupports> currentControlSupports;            nsCOMPtr<nsIFormControl> currentControl;            PRBool hasMoreElements;            while (NS_SUCCEEDED(formControls->HasMoreElements(&hasMoreElements)) &&                   hasMoreElements) {              formControls->GetNext(getter_AddRefs(currentControlSupports));              currentControl = do_QueryInterface(currentControlSupports);              if (currentControl) {                PRInt32 type;                currentControl->GetType(&type);                if (!submitControl &&                    (type == NS_FORM_INPUT_SUBMIT ||                     type == NS_FORM_BUTTON_SUBMIT ||                     type == NS_FORM_INPUT_IMAGE)) {                  submitControl = do_QueryInterface(currentControl);                  // We know as soon as we find a submit control that it no                  // longer matters how many text controls there are--we are                  // going to fire the onClick handler.                  break;                } else if (type == NS_FORM_INPUT_TEXT ||                           type == NS_FORM_INPUT_PASSWORD) {                  numTextControlsFound++;                }              }            }                        nsCOMPtr<nsIPresShell> shell;            aPresContext->GetShell(getter_AddRefs(shell));            if (shell) {              if (submitControl) {               // Fire the button's onclick handler and let the button handle               // submitting the form.                nsGUIEvent event;                event.eventStructType = NS_MOUSE_EVENT;                event.message = NS_MOUSE_LEFT_CLICK;                event.widget = nsnull;                nsEventStatus status = nsEventStatus_eIgnore;                shell->HandleDOMEventWithTarget(submitControl, &event, &status);              } else if (numTextControlsFound == 1) {                // If there's only one text control, just submit the form                nsCOMPtr<nsIContent> form = do_QueryInterface(mForm);                nsFormEvent event;                event.eventStructType = NS_FORM_EVENT;                event.message         = NS_FORM_SUBMIT;                event.originator      = nsnull;                nsEventStatus status  = nsEventStatus_eIgnore;                shell->HandleDOMEventWithTarget(form, &event, &status);              }            }          }        }      } 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 (type == NS_FORM_INPUT_BUTTON ||             type == NS_FORM_INPUT_RESET ||             type == NS_FORM_INPUT_SUBMIT ) {          nsCOMPtr<nsIDOMNSEvent> nsevent;          if (aDOMEvent) {            nsevent = do_QueryInterface(*aDOMEvent);          }          if (nsevent) {            nsevent->PreventBubble();          } else {            rv = NS_ERROR_FAILURE;          }        }        break;      }      case NS_MOUSE_LEFT_CLICK:      {        switch(type) {          case NS_FORM_INPUT_RESET:          case NS_FORM_INPUT_SUBMIT:          case NS_FORM_INPUT_IMAGE:            {              if (mForm) {                nsFormEvent event;                event.eventStructType = NS_FORM_EVENT;                event.message         = (type == NS_FORM_INPUT_RESET) ? NS_FORM_RESET : NS_FORM_SUBMIT;                event.originator      = this;                nsEventStatus status  = nsEventStatus_eIgnore;                nsCOMPtr<nsIPresShell> presShell;                aPresContext->GetShell(getter_AddRefs(presShell));                // 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       } break;// NS_MOUSE_LEFT_BUTTON_DOWN    } //switch   } // if  return rv;}