Bug 930374 part.1 Event.defaultPrevented should be false even if preventDefault() has been called by default action handler r=smaug

This commit is contained in:
Masayuki Nakano 2013-12-09 00:51:16 +09:00
parent eca8d0a86e
commit 91b716ae1a
10 changed files with 281 additions and 46 deletions

View File

@ -86,6 +86,7 @@ LOCAL_INCLUDES += [
'/dom/base',
'/dom/settings',
'/dom/src/storage',
'/js/xpconnect/wrappers',
'/layout/generic',
'/layout/xul',
'/layout/xul/tree/',

View File

@ -5,6 +5,7 @@
#include "base/basictypes.h"
#include "AccessCheck.h"
#include "ipc/IPCMessageUtils.h"
#include "nsCOMPtr.h"
#include "nsError.h"
@ -33,6 +34,14 @@
using namespace mozilla;
using namespace mozilla::dom;
namespace mozilla {
namespace dom {
namespace workers {
extern bool IsCurrentThreadRunningChromeWorker();
} // namespace workers
} // namespace dom
} // namespace mozilla
static char *sPopupAllowedEvents;
@ -217,6 +226,14 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMEvent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
bool
nsDOMEvent::IsChrome(JSContext* aCx) const
{
return mIsMainThreadEvent ?
xpc::AccessCheck::isChrome(js::GetContextCompartment(aCx)) :
mozilla::dom::workers::IsCurrentThreadRunningChromeWorker();
}
// nsIDOMEventInterface
NS_METHOD nsDOMEvent::GetType(nsAString& aType)
{
@ -436,25 +453,40 @@ nsDOMEvent::GetIsTrusted(bool *aIsTrusted)
NS_IMETHODIMP
nsDOMEvent::PreventDefault()
{
if (mEvent->mFlags.mCancelable) {
mEvent->mFlags.mDefaultPrevented = true;
// This method is called only from C++ code which must handle default action
// of this event. So, pass true always.
PreventDefaultInternal(true);
return NS_OK;
}
// Need to set an extra flag for drag events.
if (mEvent->eventStructType == NS_DRAG_EVENT && IsTrusted()) {
nsCOMPtr<nsINode> node = do_QueryInterface(mEvent->currentTarget);
if (!node) {
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mEvent->currentTarget);
if (win) {
node = win->GetExtantDoc();
}
}
if (node && !nsContentUtils::IsChromeDoc(node->OwnerDoc())) {
mEvent->mFlags.mDefaultPreventedByContent = true;
}
}
void
nsDOMEvent::PreventDefault(JSContext* aCx)
{
MOZ_ASSERT(aCx, "JS context must be specified");
// Note that at handling default action, another event may be dispatched.
// Then, JS in content mey be call preventDefault()
// even in the event is in system event group. Therefore, don't refer
// mInSystemGroup here.
PreventDefaultInternal(IsChrome(aCx));
}
void
nsDOMEvent::PreventDefaultInternal(bool aCalledByDefaultHandler)
{
if (!mEvent->mFlags.mCancelable) {
return;
}
return NS_OK;
mEvent->mFlags.mDefaultPrevented = true;
// Note that even if preventDefault() has already been called by chrome,
// a call of preventDefault() by content needs to overwrite
// mDefaultPreventedByContent to true because in such case, defaultPrevented
// must be true when web apps check it after they call preventDefault().
if (!aCalledByDefaultHandler) {
mEvent->mFlags.mDefaultPreventedByContent = true;
}
}
void
@ -1143,6 +1175,24 @@ const char* nsDOMEvent::GetEventName(uint32_t aEventType)
return nullptr;
}
bool
nsDOMEvent::DefaultPrevented(JSContext* aCx) const
{
MOZ_ASSERT(aCx, "JS context must be specified");
NS_ENSURE_TRUE(mEvent, false);
// If preventDefault() has never been called, just return false.
if (!mEvent->mFlags.mDefaultPrevented) {
return false;
}
// If preventDefault() has been called by content, return true. Otherwise,
// i.e., preventDefault() has been called by chrome, return true only when
// this is called by chrome.
return mEvent->mFlags.mDefaultPreventedByContent || IsChrome(aCx);
}
bool
nsDOMEvent::GetPreventDefault() const
{
@ -1151,6 +1201,9 @@ nsDOMEvent::GetPreventDefault() const
doc->WarnOnceAbout(nsIDocument::eGetPreventDefault);
}
}
// GetPreventDefault() is legacy and Gecko specific method. Although,
// the result should be same as defaultPrevented, we don't need to break
// backward compatibility of legacy method. Let's behave traditionally.
return DefaultPrevented();
}
@ -1166,6 +1219,9 @@ NS_IMETHODIMP
nsDOMEvent::GetDefaultPrevented(bool* aReturn)
{
NS_ENSURE_ARG_POINTER(aReturn);
// This method must be called by only event handlers implemented by C++.
// Then, the handlers must handle default action. So, this method don't need
// to check if preventDefault() has been called by content or chrome.
*aReturn = DefaultPrevented();
return NS_OK;
}

View File

@ -153,9 +153,20 @@ public:
// xpidl implementation
// void PreventDefault();
// You MUST NOT call PreventDefaultJ(JSContext*) from C++ code. A call of
// this method always sets Event.defaultPrevented true for web contents.
// If default action handler calls this, web applications meet wrong
// defaultPrevented value.
void PreventDefault(JSContext* aCx);
// You MUST NOT call DefaultPrevented(JSContext*) from C++ code. This may
// return false even if PreventDefault() has been called.
// See comments in its implementation for the detail.
bool DefaultPrevented(JSContext* aCx) const;
bool DefaultPrevented() const
{
return mEvent && mEvent->mFlags.mDefaultPrevented;
return mEvent->mFlags.mDefaultPrevented;
}
bool MultipleActionsPrevented() const
@ -190,6 +201,20 @@ protected:
void SetEventType(const nsAString& aEventTypeArg);
already_AddRefed<nsIContent> GetTargetFromFrame();
/**
* IsChrome() returns true if aCx is chrome context or the event is created
* in chrome's thread. Otherwise, false.
*/
bool IsChrome(JSContext* aCx) const;
/**
* @param aCalledByDefaultHandler Should be true when this is called by
* C++ or Chrome. Otherwise, e.g., called
* by a call of Event.preventDefault() in
* content script, false.
*/
void PreventDefaultInternal(bool aCalledByDefaultHandler);
mozilla::WidgetEvent* mEvent;
nsRefPtr<nsPresContext> mPresContext;
nsCOMPtr<mozilla::dom::EventTarget> mExplicitOriginalTarget;

View File

@ -2610,10 +2610,13 @@ nsEventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame,
nsWeakFrame targetFrame(aTargetFrame);
nsEventStatus statusX = *aStatus;
nsEventStatus statusY = *aStatus;
MOZ_ASSERT(*aStatus != nsEventStatus_eConsumeNoDefault &&
!aEvent->mFlags.mDefaultPrevented,
"If you make legacy events dispatched for default prevented wheel "
"event, you need to initialize stateX and stateY");
EventState stateX, stateY;
if (scrollDeltaY) {
SendLineScrollEvent(aTargetFrame, aEvent, &statusY,
SendLineScrollEvent(aTargetFrame, aEvent, stateY,
scrollDeltaY, DELTA_DIRECTION_Y);
if (!targetFrame.IsAlive()) {
*aStatus = nsEventStatus_eConsumeNoDefault;
@ -2622,7 +2625,7 @@ nsEventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame,
}
if (pixelDeltaY) {
SendPixelScrollEvent(aTargetFrame, aEvent, &statusY,
SendPixelScrollEvent(aTargetFrame, aEvent, stateY,
pixelDeltaY, DELTA_DIRECTION_Y);
if (!targetFrame.IsAlive()) {
*aStatus = nsEventStatus_eConsumeNoDefault;
@ -2631,7 +2634,7 @@ nsEventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame,
}
if (scrollDeltaX) {
SendLineScrollEvent(aTargetFrame, aEvent, &statusX,
SendLineScrollEvent(aTargetFrame, aEvent, stateX,
scrollDeltaX, DELTA_DIRECTION_X);
if (!targetFrame.IsAlive()) {
*aStatus = nsEventStatus_eConsumeNoDefault;
@ -2640,7 +2643,7 @@ nsEventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame,
}
if (pixelDeltaX) {
SendPixelScrollEvent(aTargetFrame, aEvent, &statusX,
SendPixelScrollEvent(aTargetFrame, aEvent, stateX,
pixelDeltaX, DELTA_DIRECTION_X);
if (!targetFrame.IsAlive()) {
*aStatus = nsEventStatus_eConsumeNoDefault;
@ -2648,21 +2651,18 @@ nsEventStateManager::DispatchLegacyMouseScrollEvents(nsIFrame* aTargetFrame,
}
}
if (statusY == nsEventStatus_eConsumeNoDefault ||
statusX == nsEventStatus_eConsumeNoDefault) {
if (stateY.mDefaultPrevented || stateX.mDefaultPrevented) {
*aStatus = nsEventStatus_eConsumeNoDefault;
return;
}
if (statusY == nsEventStatus_eConsumeDoDefault ||
statusX == nsEventStatus_eConsumeDoDefault) {
*aStatus = nsEventStatus_eConsumeDoDefault;
aEvent->mFlags.mDefaultPrevented = true;
aEvent->mFlags.mDefaultPreventedByContent |=
stateY.mDefaultPreventedByContent || stateX.mDefaultPreventedByContent;
}
}
void
nsEventStateManager::SendLineScrollEvent(nsIFrame* aTargetFrame,
WidgetWheelEvent* aEvent,
nsEventStatus* aStatus,
EventState& aState,
int32_t aDelta,
DeltaDirection aDeltaDirection)
{
@ -2678,9 +2678,8 @@ nsEventStateManager::SendLineScrollEvent(nsIFrame* aTargetFrame,
WidgetMouseScrollEvent event(aEvent->mFlags.mIsTrusted, NS_MOUSE_SCROLL,
aEvent->widget);
if (*aStatus == nsEventStatus_eConsumeNoDefault) {
event.mFlags.mDefaultPrevented = true;
}
event.mFlags.mDefaultPrevented = aState.mDefaultPrevented;
event.mFlags.mDefaultPreventedByContent = aState.mDefaultPreventedByContent;
event.refPoint = aEvent->refPoint;
event.widget = aEvent->widget;
event.time = aEvent->time;
@ -2690,14 +2689,18 @@ nsEventStateManager::SendLineScrollEvent(nsIFrame* aTargetFrame,
event.delta = aDelta;
event.inputSource = aEvent->inputSource;
nsEventStatus status = nsEventStatus_eIgnore;
nsEventDispatcher::Dispatch(targetContent, aTargetFrame->PresContext(),
&event, nullptr, aStatus);
&event, nullptr, &status);
aState.mDefaultPrevented =
event.mFlags.mDefaultPrevented || status == nsEventStatus_eConsumeNoDefault;
aState.mDefaultPreventedByContent = event.mFlags.mDefaultPreventedByContent;
}
void
nsEventStateManager::SendPixelScrollEvent(nsIFrame* aTargetFrame,
WidgetWheelEvent* aEvent,
nsEventStatus* aStatus,
EventState& aState,
int32_t aPixelDelta,
DeltaDirection aDeltaDirection)
{
@ -2714,9 +2717,8 @@ nsEventStateManager::SendPixelScrollEvent(nsIFrame* aTargetFrame,
WidgetMouseScrollEvent event(aEvent->mFlags.mIsTrusted, NS_MOUSE_PIXEL_SCROLL,
aEvent->widget);
if (*aStatus == nsEventStatus_eConsumeNoDefault) {
event.mFlags.mDefaultPrevented = true;
}
event.mFlags.mDefaultPrevented = aState.mDefaultPrevented;
event.mFlags.mDefaultPreventedByContent = aState.mDefaultPreventedByContent;
event.refPoint = aEvent->refPoint;
event.widget = aEvent->widget;
event.time = aEvent->time;
@ -2726,8 +2728,12 @@ nsEventStateManager::SendPixelScrollEvent(nsIFrame* aTargetFrame,
event.delta = aPixelDelta;
event.inputSource = aEvent->inputSource;
nsEventStatus status = nsEventStatus_eIgnore;
nsEventDispatcher::Dispatch(targetContent, aTargetFrame->PresContext(),
&event, nullptr, aStatus);
&event, nullptr, &status);
aState.mDefaultPrevented =
event.mFlags.mDefaultPrevented || status == nsEventStatus_eConsumeNoDefault;
aState.mDefaultPreventedByContent = event.mFlags.mDefaultPreventedByContent;
}
nsIScrollableFrame*

View File

@ -481,6 +481,17 @@ protected:
DELTA_DIRECTION_Y
};
struct MOZ_STACK_CLASS EventState
{
bool mDefaultPrevented;
bool mDefaultPreventedByContent;
EventState() :
mDefaultPrevented(false), mDefaultPreventedByContent(false)
{
}
};
/**
* SendLineScrollEvent() dispatches a DOMMouseScroll event for the
* WidgetWheelEvent. This method shouldn't be called for non-trusted
@ -488,14 +499,15 @@ protected:
*
* @param aTargetFrame The event target of wheel event.
* @param aEvent The original Wheel event.
* @param aStatus The event status, must not be
* nsEventStatus_eConsumeNoDefault.
* @param aState The event which should be set to the dispatching
* event. This also returns the dispatched event
* state.
* @param aDelta The delta value of the event.
* @param aDeltaDirection The X/Y direction of dispatching event.
*/
void SendLineScrollEvent(nsIFrame* aTargetFrame,
mozilla::WidgetWheelEvent* aEvent,
nsEventStatus* aStatus,
EventState& aState,
int32_t aDelta,
DeltaDirection aDeltaDirection);
@ -506,14 +518,15 @@ protected:
*
* @param aTargetFrame The event target of wheel event.
* @param aEvent The original Wheel event.
* @param aStatus The event status, must not be
* nsEventStatus_eConsumeNoDefault.
* @param aState The event which should be set to the dispatching
* event. This also returns the dispatched event
* state.
* @param aPixelDelta The delta value of the event.
* @param aDeltaDirection The X/Y direction of dispatching event.
*/
void SendPixelScrollEvent(nsIFrame* aTargetFrame,
mozilla::WidgetWheelEvent* aEvent,
nsEventStatus* aStatus,
EventState& aState,
int32_t aPixelDelta,
DeltaDirection aDeltaDirection);

View File

@ -15,4 +15,5 @@ support-files =
[test_bug602962.xul]
[test_bug617528.xul]
[test_bug679494.xul]
[test_bug930374-chrome.html]
[test_eventctors.xul]

View File

@ -83,6 +83,7 @@ skip-if = true # Disabled due to timeouts.
[test_bug847597.html]
[test_bug855741.html]
[test_bug864040.html]
[test_bug930374-content.html]
skip-if = toolkit == "gonk"
[test_clickevent_on_input.html]
[test_continuous_wheel_events.html]

View File

@ -0,0 +1,59 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=930374
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 930374</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=930374">Mozilla Bug 930374</a>
<div id="display">
<input id="input-text">
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var gKeyPress = null;
function onKeyPress(aEvent)
{
gKeyPress = aEvent;
is(aEvent.target, document.getElementById("input-text"), "input element should have focus");
ok(!aEvent.defaultPrevented, "keypress event should be consumed before keypress event handler");
}
function runTests()
{
document.addEventListener("keypress", onKeyPress, false);
var input = document.getElementById("input-text");
input.focus();
input.addEventListener("input", function (aEvent) {
input.removeEventListener("input", arguments.callee, false);
ok(gKeyPress,
"Test1: keypress event must be fired before an input event");
ok(gKeyPress.defaultPrevented,
"Test1: keypress event's defaultPrevented should be true in chrome even if it's consumed by default action handler of editor");
setTimeout(function () {
ok(gKeyPress.defaultPrevented,
"Test2: keypress event's defaultPrevented should be true after event dispatching finished");
SimpleTest.finish();
}, 0);
}, false);
sendChar("a");
}
SimpleTest.waitForFocus(runTests);
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,72 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=930374
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 930374</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=930374">Mozilla Bug 930374</a>
<div id="display">
<input id="input-text">
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
var gKeyPress = null;
function onKeyPress(aEvent)
{
gKeyPress = aEvent;
is(aEvent.target, document.getElementById("input-text"), "input element should have focus");
ok(!aEvent.defaultPrevented, "keypress event should be consumed before keypress event handler");
}
function runTests()
{
document.addEventListener("keypress", onKeyPress, false);
var input = document.getElementById("input-text");
input.focus();
input.addEventListener("input", function (aEvent) {
input.removeEventListener("input", arguments.callee, false);
ok(gKeyPress,
"Test1: keypress event must be fired before an input event");
ok(!gKeyPress.defaultPrevented,
"Test1: keypress event's defaultPrevented should be false even though it's consumed by the default action handler of editor");
gKeyPress.preventDefault();
ok(gKeyPress.defaultPrevented,
"Test1: keypress event's defaultPrevented should become true because of a call of preventDefault()");
}, false);
sendChar("a");
gKeyPress = null;
input.addEventListener("input", function (aEvent) {
input.removeEventListener("input", arguments.callee, false);
ok(gKeyPress,
"Test2: keypress event must be fired before an input event");
ok(!gKeyPress.defaultPrevented,
"Test2: keypress event's defaultPrevented should be false even though it's consumed by the default action handler of editor");
setTimeout(function () {
ok(!gKeyPress.defaultPrevented,
"Test2: keypress event's defaultPrevented should not become true after event dispatching finished");
SimpleTest.finish();
}, 0);
}, false);
sendChar("b");
}
SimpleTest.waitForFocus(runTests);
</script>
</pre>
</body>
</html>

View File

@ -385,6 +385,7 @@ DOMInterfaces = {
'Event': {
'nativeType': 'nsDOMEvent',
'implicitJSContext': [ 'defaultPrevented', 'preventDefault' ],
},
'EventTarget': {