diff --git a/mozilla/content/events/src/nsEventStateManager.cpp b/mozilla/content/events/src/nsEventStateManager.cpp index 56c7f4515d8..56740e18884 100644 --- a/mozilla/content/events/src/nsEventStateManager.cpp +++ b/mozilla/content/events/src/nsEventStateManager.cpp @@ -1270,22 +1270,28 @@ nsEventStateManager::FireContextClick() } if (allowedToDispatch) { + // make sure the widget sticks around + nsCOMPtr targetWidget(mCurrentTarget->GetWindow()); + // init the event while mCurrentTarget is still good + nsMouseEvent event(PR_TRUE, NS_CONTEXTMENU, + targetWidget, + nsMouseEvent::eReal); + event.clickCount = 1; + FillInEventFromGestureDown(&event); + // stop selection tracking, we're in control now nsCOMPtr frameSel; GetSelection(mCurrentTarget, mPresContext, getter_AddRefs(frameSel)); if (frameSel) { PRBool mouseDownState = PR_TRUE; frameSel->GetMouseDownState(&mouseDownState); - if (mouseDownState) + if (mouseDownState) { + // note that this can cause selection changed events to fire if we're in + // a text field, which will null out mCurrentTarget frameSel->SetMouseDownState(PR_FALSE); + } } - nsMouseEvent event(PR_TRUE, NS_CONTEXTMENU, - mCurrentTarget->GetWindow(), - nsMouseEvent::eReal); - event.clickCount = 1; - FillInEventFromGestureDown(&event); - // dispatch to DOM mGestureDownContent->HandleDOMEvent(mPresContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status);