Bug 930893 part.1 Implement constructor of KeyboardEvent r=smaug

This commit is contained in:
Masayuki Nakano 2014-04-14 15:37:47 +09:00
Родитель aa11ec4ab9
Коммит d930f4e3f8
5 изменённых файлов: 163 добавлений и 11 удалений

Просмотреть файл

@ -15,6 +15,8 @@ KeyboardEvent::KeyboardEvent(EventTarget* aOwner,
WidgetKeyboardEvent* aEvent)
: UIEvent(aOwner, aPresContext,
aEvent ? aEvent : new WidgetKeyboardEvent(false, 0, nullptr))
, mInitializedByCtor(false)
, mInitialzedWhichValue(0)
{
NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
@ -139,6 +141,11 @@ KeyboardEvent::GetCharCode(uint32_t* aCharCode)
uint32_t
KeyboardEvent::CharCode()
{
// If this event is initialized with ctor, we shouldn't check event type.
if (mInitializedByCtor) {
return mEvent->AsKeyboardEvent()->charCode;
}
switch (mEvent->message) {
case NS_KEY_UP:
case NS_KEY_DOWN:
@ -160,6 +167,11 @@ KeyboardEvent::GetKeyCode(uint32_t* aKeyCode)
uint32_t
KeyboardEvent::KeyCode()
{
// If this event is initialized with ctor, we shouldn't check event type.
if (mInitializedByCtor) {
return mEvent->AsKeyboardEvent()->keyCode;
}
switch (mEvent->message) {
case NS_KEY_UP:
case NS_KEY_PRESS:
@ -172,6 +184,11 @@ KeyboardEvent::KeyCode()
uint32_t
KeyboardEvent::Which()
{
// If this event is initialized with ctor, which can have independent value.
if (mInitializedByCtor) {
return mInitialzedWhichValue;
}
switch (mEvent->message) {
case NS_KEY_UP:
case NS_KEY_DOWN:
@ -206,6 +223,36 @@ KeyboardEvent::Location()
return mEvent->AsKeyboardEvent()->location;
}
// static
already_AddRefed<KeyboardEvent>
KeyboardEvent::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const KeyboardEventInit& aParam,
ErrorResult& aRv)
{
nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
nsRefPtr<KeyboardEvent> newEvent =
new KeyboardEvent(target, nullptr, nullptr);
bool trusted = newEvent->Init(target);
aRv = newEvent->InitKeyEvent(aType, aParam.mBubbles, aParam.mCancelable,
aParam.mView, aParam.mCtrlKey, aParam.mAltKey,
aParam.mShiftKey, aParam.mMetaKey,
aParam.mKeyCode, aParam.mCharCode);
newEvent->SetTrusted(trusted);
newEvent->mDetail = aParam.mDetail;
newEvent->mInitializedByCtor = true;
newEvent->mInitialzedWhichValue = aParam.mWhich;
WidgetKeyboardEvent* internalEvent = newEvent->mEvent->AsKeyboardEvent();
internalEvent->location = aParam.mLocation;
internalEvent->mIsRepeat = aParam.mRepeat;
internalEvent->mIsComposing = aParam.mIsComposing;
internalEvent->mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
internalEvent->mKeyValue = aParam.mKey;
return newEvent.forget();
}
NS_IMETHODIMP
KeyboardEvent::InitKeyEvent(const nsAString& aType,
bool aCanBubble,

Просмотреть файл

@ -30,6 +30,12 @@ public:
// Forward to base class
NS_FORWARD_TO_UIEVENT
static already_AddRefed<KeyboardEvent> Constructor(
const GlobalObject& aGlobal,
const nsAString& aType,
const KeyboardEventInit& aParam,
ErrorResult& aRv);
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE
{
return KeyboardEventBinding::Wrap(aCx, this);
@ -62,6 +68,14 @@ public:
aCtrlKey, aAltKey, aShiftKey,aMetaKey,
aKeyCode, aCharCode);
}
private:
// True, if the instance is created with Constructor().
bool mInitializedByCtor;
// If the instance is created with Constructor(), which may have independent
// value. mInitializedWhichValue stores it. I.e., this is invalid when
// mInitializedByCtor is false.
uint32_t mInitialzedWhichValue;
};
} // namespace dom

Просмотреть файл

@ -188,20 +188,11 @@ const kEventConstructors = {
},
},
KeyEvent: { create: function (aName, aProps) {
var e = document.createEvent("keyboardevent");
e.initKeyEvent(aName, aProps.bubbles, aProps.cancelable,
aProps.view,
aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
aProps.keyCode, aProps.charCode);
return e;
return new KeyboardEvent(aName, aProps);
},
},
KeyboardEvent: { create: function (aName, aProps) {
var e = document.createEvent("keyboardevent");
e.initKeyEvent(aName, aProps.bubbles, aProps.cancelable,
aProps.view, aProps.ctrlKey, aProps.altKey, aProps.shiftKey, aProps.metaKey,
aProps.keyCode, aProps.charCode);
return e;
return new KeyboardEvent(aName, aProps);
},
},
MediaStreamEvent: { create: function (aName, aProps) {

Просмотреть файл

@ -340,6 +340,88 @@ ok(e.cancelable, "Event should be cancelable!");
is(e.detail, 0, "detail should be 0");
ok(e.isComposing, "isComposing should be true");
// KeyboardEvent
try {
e = new KeyboardEvent();
} catch(exp) {
ex = true;
}
ok(ex, "KeyboardEvent: First parameter is required!");
ex = false;
e = new KeyboardEvent("hello");
ok(e.type, "hello", "KeyboardEvent: Wrong event type!");
ok(!e.isTrusted, "KeyboardEvent: Event shouldn't be trusted!");
ok(!e.bubbles, "KeyboardEvent: Event shouldn't bubble!");
ok(!e.cancelable, "KeyboardEvent: Event shouldn't be cancelable!");
document.dispatchEvent(e);
is(receivedEvent, e, "KeyboardEvent: Wrong event!");
var keyboardEventProps =
[
{ bubbles: false },
{ cancelable: false },
{ view: null },
{ detail: 0 },
{ key: "" },
{ location: 0 },
{ ctrlKey: false },
{ shiftKey: false },
{ altKey: false },
{ metaKey: false },
{ repeat: false },
{ isComposing: false },
{ charCode: 0 },
{ keyCode: 0 },
{ which: 0 },
];
var testKeyboardProps =
[
{ bubbles: true },
{ cancelable: true },
{ view: window },
{ detail: 1 },
{ key: "CustomKey" },
{ location: 1 },
{ ctrlKey: true },
{ shiftKey: true },
{ altKey: true },
{ metaKey: true },
{ repeat: true },
{ isComposing: true },
{ charCode: 2 },
{ keyCode: 3 },
{ which: 4 },
{ charCode: 5, which: 6 },
{ keyCode: 7, which: 8 },
{ keyCode: 9, charCode: 10 },
{ keyCode: 11, charCode: 12, which: 13 },
];
var defaultKeyboardEventValues = {};
for (var i = 0; i < keyboardEventProps.length; ++i) {
for (prop in keyboardEventProps[i]) {
ok(prop in e, "keyboardEvent: KeyboardEvent doesn't have property " + prop + "!");
defaultKeyboardEventValues[prop] = keyboardEventProps[i][prop];
}
}
while (testKeyboardProps.length) {
var p = testKeyboardProps.shift();
e = new KeyboardEvent("foo", p);
for (var def in defaultKeyboardEventValues) {
if (!(def in p)) {
is(e[def], defaultKeyboardEventValues[def],
"KeyboardEvent: Wrong default value for " + def + "!");
} else {
is(e[def], p[def],
"KeyboardEvent: Wrong event init value for " + def + "!");
}
}
}
// PageTransitionEvent
try {

Просмотреть файл

@ -6,6 +6,7 @@
interface WindowProxy;
[Constructor(DOMString typeArg, optional KeyboardEventInit keyboardEventInitDict)]
interface KeyboardEvent : UIEvent
{
readonly attribute unsigned long charCode;
@ -32,5 +33,22 @@ interface KeyboardEvent : UIEvent
readonly attribute DOMString key;
};
dictionary KeyboardEventInit : UIEventInit
{
DOMString key = "";
unsigned long location = 0;
boolean ctrlKey = false;
boolean shiftKey = false;
boolean altKey = false;
boolean metaKey = false;
boolean repeat = false;
boolean isComposing = false;
// legacy attributes
unsigned long charCode = 0;
unsigned long keyCode = 0;
unsigned long which = 0;
};
// Mozilla extensions
KeyboardEvent implements KeyEvent;