Bug 709127 - Implement MouseEvent and UIEvent ctors, r=jst, a=sheriff

This commit is contained in:
Olli Pettay 2011-12-14 22:36:46 +02:00
parent ddddb0b2e3
commit 50e93e9d3d
8 changed files with 205 additions and 0 deletions

View File

@ -144,6 +144,61 @@ nsDOMMouseEvent::InitMouseEvent(const nsAString & aType, bool aCanBubble, bool a
return NS_OK;
}
nsresult
nsDOMMouseEvent::InitFromCtor(const nsAString& aType, nsISupports* aDict,
JSContext* aCx, JSObject* aObj)
{
nsCOMPtr<nsIMouseEventInit> eventInit = do_QueryInterface(aDict);
bool bubbles = false;
bool cancelable = false;
nsCOMPtr<nsIDOMWindow> view;
PRInt32 detail = 0;
PRInt32 screenX = 0;
PRInt32 screenY = 0;
PRInt32 clientX = 0;
PRInt32 clientY = 0;
bool ctrl = false;
bool alt = false;
bool shift = false;
bool meta = false;
PRUint16 button = 0;
nsCOMPtr<nsIDOMEventTarget> relatedTarget;
if (eventInit) {
nsresult rv = eventInit->GetBubbles(&bubbles);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetCancelable(&cancelable);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetView(getter_AddRefs(view));
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetDetail(&detail);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetScreenX(&screenX);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetScreenY(&screenY);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetClientX(&clientX);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetClientY(&clientY);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetCtrlKey(&ctrl);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetShiftKey(&shift);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetAltKey(&alt);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetMetaKey(&meta);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetButton(&button);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetRelatedTarget(getter_AddRefs(relatedTarget));
NS_ENSURE_SUCCESS(rv, rv);
}
return InitMouseEvent(aType, bubbles, cancelable,
view, detail, screenX, screenY, clientX, clientY,
ctrl, alt, shift, meta,
button, relatedTarget);
}
NS_IMETHODIMP
nsDOMMouseEvent::InitNSMouseEvent(const nsAString & aType, bool aCanBubble, bool aCancelable,
nsIDOMWindow *aView, PRInt32 aDetail, PRInt32 aScreenX,

View File

@ -60,6 +60,9 @@ public:
// Forward to base class
NS_FORWARD_TO_NSDOMUIEVENT
virtual const nsIID& EventInitIID() { return NS_GET_IID(nsIMouseEventInit); }
virtual nsresult InitFromCtor(const nsAString& aType, nsISupports* aDict,
JSContext* aCx, JSObject* aObj);
protected:
// Specific implementation for a mouse event.
virtual nsresult Which(PRUint32* aWhich);

View File

@ -205,6 +205,28 @@ nsDOMUIEvent::InitUIEvent(const nsAString& typeArg,
return NS_OK;
}
nsresult
nsDOMUIEvent::InitFromCtor(const nsAString& aType, nsISupports* aDict,
JSContext* aCx, JSObject* aObj)
{
nsCOMPtr<nsIUIEventInit> eventInit = do_QueryInterface(aDict);
bool bubbles = false;
bool cancelable = false;
nsCOMPtr<nsIDOMWindow> view;
PRInt32 detail = 0;
if (eventInit) {
nsresult rv = eventInit->GetBubbles(&bubbles);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetCancelable(&cancelable);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetView(getter_AddRefs(view));
NS_ENSURE_SUCCESS(rv, rv);
rv = eventInit->GetDetail(&detail);
NS_ENSURE_SUCCESS(rv, rv);
}
return InitUIEvent(aType, bubbles, cancelable, view, detail);
}
// ---- nsDOMNSUIEvent implementation -------------------
nsIntPoint
nsDOMUIEvent::GetPagePoint()

View File

@ -64,6 +64,9 @@ public:
NS_FORWARD_NSIDOMNSEVENT(nsDOMEvent::)
virtual const nsIID& EventInitIID() { return NS_GET_IID(nsIUIEventInit); }
virtual nsresult InitFromCtor(const nsAString& aType, nsISupports* aDict,
JSContext* aCx, JSObject* aObj);
protected:
// Internal helper functions
nsIntPoint GetClientPoint();

View File

@ -274,6 +274,101 @@ is(e.state, window, "persisted should be window");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// UIEvent
try {
e = new UIEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new UIEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
is(e.detail, 0, "detail should be 0");
is(e.view, null, "view should be null");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
e = new UIEvent("hello",
{ bubbles: true, cancelable: true, view: window, detail: 1});
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(e.bubbles, "Event should bubble!");
ok(e.cancelable, "Event should be cancelable!");
is(e.detail, 1, "detail should be 1");
is(e.view, window, "view should be window");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
// UIEvent
try {
e = new UIEvent();
} catch(exp) {
ex = true;
}
ok(ex, "First parameter is required!");
ex = false;
e = new MouseEvent("hello");
ok(e.type, "hello", "Wrong event type!");
ok(!e.isTrusted, "Event shouldn't be trusted!");
ok(!e.bubbles, "Event shouldn't bubble!");
ok(!e.cancelable, "Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "Wrong event!");
var mouseEventProps =
[ { screenX: 0 },
{ screenY: 0 },
{ clientX: 0 },
{ clientY: 0 },
{ ctrlKey: false },
{ shiftKey: false },
{ altKey: false },
{ metaKey: false },
{ button: 0 },
{ relatedTarget: null }
];
var testProps =
[
{ screenX: 1 },
{ screenY: 2 },
{ clientX: 3 },
{ clientY: 4 },
{ ctrlKey: true },
{ shiftKey: true },
{ altKey: true },
{ metaKey: true },
{ button: 5 },
{ relatedTarget: window }
];
var defaultMouseEventValues = {};
for (var i = 0; i < mouseEventProps.length; ++i) {
for (prop in mouseEventProps[i]) {
ok(prop in e, "MouseEvent doesn't have property " + prop + "!");
defaultMouseEventValues[prop] = mouseEventProps[i][prop];
}
}
while (testProps.length) {
var p = testProps.shift();
e = new MouseEvent("foo", p);
for (var def in defaultMouseEventValues) {
if (!(def in p)) {
is(e[def], defaultMouseEventValues[def], "Wrong default value for " + def + "!");
} else {
is(e[def], p[def], "Wrong event init value for " + def + "!");
}
}
}
SimpleTest.finish();

View File

@ -1591,6 +1591,8 @@ NS_DEFINE_EVENT_CTOR(PopStateEvent)
NS_DEFINE_EVENT_CTOR(HashChangeEvent)
NS_DEFINE_EVENT_CTOR(PageTransitionEvent)
NS_DEFINE_EVENT_CTOR(CloseEvent)
NS_DEFINE_EVENT_CTOR(UIEvent)
NS_DEFINE_EVENT_CTOR(MouseEvent)
struct nsConstructorFuncMapData
{
@ -1614,6 +1616,8 @@ static const nsConstructorFuncMapData kConstructorFuncMap[] =
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(HashChangeEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(PageTransitionEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(CloseEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(UIEvent)
NS_DEFINE_EVENT_CONSTRUCTOR_FUNC_DATA(MouseEvent)
};
nsIXPConnect *nsDOMClassInfo::sXPConnect = nsnull;

View File

@ -113,3 +113,19 @@ interface nsIDOMMouseEvent : nsIDOMUIEvent
in float pressure,
in unsigned short inputSourceArg);
};
[scriptable, uuid(9495a977-5c9e-4b34-8d51-22bfd9b4fcf6)]
interface nsIMouseEventInit : nsIUIEventInit
{
attribute long screenX;
attribute long screenY;
attribute long clientX;
attribute long clientY;
attribute boolean ctrlKey;
attribute boolean shiftKey;
attribute boolean altKey;
attribute boolean metaKey;
attribute unsigned short button;
// attribute unsigned short buttons; is not supported yet.
attribute nsIDOMEventTarget relatedTarget;
};

View File

@ -73,3 +73,10 @@ interface nsIDOMUIEvent : nsIDOMEvent
readonly attribute boolean isChar;
};
[scriptable, uuid(610eb27e-9718-4acd-8ed1-7d1840bc6c7f)]
interface nsIUIEventInit : nsIEventInit
{
attribute nsIDOMWindow view;
attribute long detail;
};