標籤:
轉載請註明:http://blog.csdn.net/lichwei1983
第一種方法: 直接修改WebCore的Window, HTML中關於Event名稱,監聽器。
1 修改HTMLAttributeName.in,增加event name: onirkeyevent;
2 修改DOMWindow.idl,增加事件監聽器:attribute EventListener onirkeypress;
3 修改Domcument.idl,增加事件監聽器 attribute [DontEnum] EventListener onirkeypress;
4 修改Document.h,定義事件監聽器 DEFINE_ATTRIBUTE_EVENT_LISTENER(irkeypress);
第二中方法: 使用event專用IDL的構造模板
我們可以使用兩個關鍵詞來建立自己的Event:[ConstructorTemplate]是用來修飾介面的, [InitializedByEventConstructor]是用來修飾屬性的。
用法: [ConstructorTemplate=Event]. 這種方法只能用於制定事件介面. [InitializedByEventConstructor]可以用來修飾介面的屬性,舉例如下:
[ ConstructorTemplate=Event ] interface MichaelIREvent { attribute DOMString str1; [InitializedByEventConstructor] attribute DOMString str2; };
因為事件介面的構建器(constructor)要求特殊的綁定,我們需要使用這種方法來實現,通常的Constructor是不行的。
當把如上IDL檔案加入Webkit編譯以後,測試的JavaScript代碼如下,首先建一個MichaelIREvent的DOM對象:
var e = new MichaelIREvent("type", { bubbles: true, cancelable: true });
然後,WebCore裡的MichaelIREvent::create()就會被調用. Specifically, WebCore會自動產生如下方法並把它作為構建器的回呼函數:
PassRefPtr<FooEvent> MichaelIREvent::create(const AtomicString& type, const FooEventInit& initializer) { ...; }
[InitializedByEventConstructor] 應該修飾那些需要構造器初始化的屬性. Event介面的Spec規定了哪些屬性需要初始化.具體參看W3C的連結:the spec of Event.
[Constructor(DOMStringtype, optional EventInit eventInitDict), Exposed=(Window,Worker)]interfaceEvent{ readonly attribute DOMString type; readonly attribute EventTarget? target; readonly attribute EventTarget? currentTarget; const unsigned short NONE = 0; const unsigned short CAPTURING_PHASE = 1; const unsigned short AT_TARGET = 2; const unsigned short BUBBLING_PHASE = 3; readonly attribute unsigned short eventPhase; void stopPropagation(); void stopImmediatePropagation(); readonly attribute boolean bubbles; readonly attribute boolean cancelable; void preventDefault(); readonly attribute boolean defaultPrevented; [Unforgeable] readonly attribute boolean isTrusted; readonly attribute DOMTimeStamp timeStamp; void initEvent(DOMStringtype, booleanbubbles, booleancancelable);};dictionary EventInit{ boolean bubbles = false; boolean cancelable = false;};
EventInit有屬性:bubbles 和 cancelable, 因此bubbles 和 cancelable唯一需要被Event構建器初始化的兩個參數. 換句話說,對於自訂的事件,你只需要為bubbles和cancelable屬性指定InitializedByEventConstructorIn修飾即可。
Webkit裡建立Event的兩種方法