From 2ab69f8719e7925c5ca0f41cc8ff5ccdb5cd18be Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Tue, 20 Jun 2017 17:19:10 -0400 Subject: [PATCH] [jquery] Clean up event-related methods on JQuery. Add tests for event-related methods on JQuery. --- types/jquery/index.d.ts | 253 +++++---- types/jquery/jquery-tests.ts | 1029 +++++++++++++++++++++++++++++++++- 2 files changed, 1159 insertions(+), 123 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index cea056ef60..4a0b3c30f1 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -296,6 +296,7 @@ interface JQuery { * @since 1.10 */ before(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + // [bind() overloads] https://github.com/jquery/api.jquery.com/issues/1048 /** * Attach a handler to an event for the elements. * @@ -304,43 +305,23 @@ interface JQuery { * @param handler A function to execute each time the event is triggered. * @see {@link https://api.jquery.com/bind/} * @since 1.0 - * @deprecated 3.0 - */ - bind(eventType: string, eventData: TData, handler: JQuery.EventHandler | false): this; - /** - * Attach a handler to an event for the elements. - * - * @param eventType A string containing one or more DOM event types, such as "click" or "submit," or custom event names. - * @param eventData An object containing data that will be passed to the event handler. - * @param preventBubble Setting the third argument to false will attach a function that prevents the default action from - * occurring and stops the event from bubbling. The default is true. - * @see {@link https://api.jquery.com/bind/} * @since 1.4.3 * @deprecated 3.0 */ - bind(eventType: string, eventData: any, preventBubble: boolean): this; + bind(eventType: string, eventData: TData, handler: JQuery.EventHandler): this; /** * Attach a handler to an event for the elements. * * @param eventType A string containing one or more DOM event types, such as "click" or "submit," or custom event names. * @param handler A function to execute each time the event is triggered. + * Setting the second argument to false will attach a function that prevents the default action from + * occurring and stops the event from bubbling. * @see {@link https://api.jquery.com/bind/} * @since 1.0 + * @since 1.4.3 * @deprecated 3.0 */ - bind(eventType: string, handler: JQuery.EventHandler | false): this; - /** - * Attach a handler to an event for the elements. - * - * @param eventType A string containing one or more DOM event types, such as "click" or "submit," or custom event names. - * @param preventBubble_eventData Setting the third argument to false will attach a function that prevents the default action from - * occurring and stops the event from bubbling. The default is true. - * An object containing data that will be passed to the event handler. - * @see {@link https://api.jquery.com/bind/} - * @since 1.0 - * @deprecated 3.0 - */ - bind(eventType: string, preventBubble_eventData?: boolean | any): this; + bind(eventType: string, handler: JQuery.EventHandler | false | null | undefined): this; /** * Attach a handler to an event for the elements. * @@ -358,7 +339,7 @@ interface JQuery { * @see {@link https://api.jquery.com/blur/} * @since 1.4.3 */ - blur(eventData: TData, handler: JQuery.EventHandler | false): this; + blur(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "blur" JavaScript event, or trigger that event on an element. * @@ -375,7 +356,7 @@ interface JQuery { * @see {@link https://api.jquery.com/change/} * @since 1.4.3 */ - change(eventData: TData, handler: JQuery.EventHandler | false): this; + change(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "change" JavaScript event, or trigger that event on an element. * @@ -408,7 +389,7 @@ interface JQuery { * @see {@link https://api.jquery.com/click/} * @since 1.4.3 */ - click(eventData: TData, handler: JQuery.EventHandler | false): this; + click(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "click" JavaScript event, or trigger that event on an element. * @@ -467,7 +448,7 @@ interface JQuery { * @see {@link https://api.jquery.com/contextmenu/} * @since 1.4.3 */ - contextmenu(eventData: TData, handler: JQuery.EventHandler | false): this; + contextmenu(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "contextmenu" JavaScript event, or trigger that event on an element. * @@ -566,7 +547,7 @@ interface JQuery { * @see {@link https://api.jquery.com/dblclick/} * @since 1.4.3 */ - dblclick(eventData: TData, handler: JQuery.EventHandler | false): this; + dblclick(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "dblclick" JavaScript event, or trigger that event on an element. * @@ -597,7 +578,7 @@ interface JQuery { * @since 1.4.2 * @deprecated 3.0 */ - delegate(selector: string, eventType: string, eventData: TData, handler: JQuery.EventHandler | false): this; + delegate(selector: JQuery.Selector, eventType: string, eventData: TData, handler: JQuery.EventHandler): this; /** * Attach a handler to one or more events for all elements that match the selector, now or in the * future, based on a specific set of root elements. @@ -610,7 +591,7 @@ interface JQuery { * @since 1.4.2 * @deprecated 3.0 */ - delegate(selector: string, eventType: string, handler: JQuery.EventHandler | false): this; + delegate(selector: JQuery.Selector, eventType: string, handler: JQuery.EventHandler | false): this; /** * Attach a handler to one or more events for all elements that match the selector, now or in the * future, based on a specific set of root elements. @@ -621,7 +602,7 @@ interface JQuery { * @since 1.4.3 * @deprecated 3.0 */ - delegate(selector: string, events: JQuery.PlainObject | false>): this; + delegate(selector: JQuery.Selector, events: JQuery.PlainObject | false>): this; /** * Execute the next function on the queue for the matched elements. * @@ -846,7 +827,7 @@ interface JQuery { * @see {@link https://api.jquery.com/focus/} * @since 1.4.3 */ - focus(eventData: TData, handler: JQuery.EventHandler | false): this; + focus(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "focus" JavaScript event, or trigger that event on an element. * @@ -863,7 +844,7 @@ interface JQuery { * @see {@link https://api.jquery.com/focusin/} * @since 1.4.3 */ - focusin(eventData: TData, handler: JQuery.EventHandler | false): this; + focusin(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "focusin" event. * @@ -880,7 +861,7 @@ interface JQuery { * @see {@link https://api.jquery.com/focusout/} * @since 1.4.3 */ - focusout(eventData: TData, handler: JQuery.EventHandler | false): this; + focusout(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "focusout" JavaScript event. * @@ -981,8 +962,7 @@ interface JQuery { * @since 1.0 * @since 1.4 */ - hover(handlerInOut: (this: TElement, eventObject: JQuery.Event) => void | false, - handlerOut?: (this: TElement, eventObject: JQuery.Event) => void | false): this; + hover(handlerInOut: JQuery.EventHandler | false, handlerOut?: JQuery.EventHandler | false): this; /** * Set the HTML contents of each element in the set of matched elements. * @@ -1093,7 +1073,7 @@ interface JQuery { * @see {@link https://api.jquery.com/keydown/} * @since 1.4.3 */ - keydown(eventData: TData, handler: JQuery.EventHandler | false): this; + keydown(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "keydown" JavaScript event, or trigger that event on an element. * @@ -1110,7 +1090,7 @@ interface JQuery { * @see {@link https://api.jquery.com/keypress/} * @since 1.4.3 */ - keypress(eventData: TData, handler: JQuery.EventHandler | false): this; + keypress(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "keypress" JavaScript event, or trigger that event on an element. * @@ -1127,7 +1107,7 @@ interface JQuery { * @see {@link https://api.jquery.com/keyup/} * @since 1.4.3 */ - keyup(eventData: TData, handler: JQuery.EventHandler | false): this; + keyup(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "keyup" JavaScript event, or trigger that event on an element. * @@ -1183,7 +1163,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mousedown/} * @since 1.4.3 */ - mousedown(eventData: TData, handler: JQuery.EventHandler | false): this; + mousedown(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "mousedown" JavaScript event, or trigger that event on an element. * @@ -1200,7 +1180,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mouseenter/} * @since 1.4.3 */ - mouseenter(eventData: TData, handler: JQuery.EventHandler | false): this; + mouseenter(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to be fired when the mouse enters an element, or trigger that handler on an element. * @@ -1217,7 +1197,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mouseleave/} * @since 1.4.3 */ - mouseleave(eventData: TData, handler: JQuery.EventHandler | false): this; + mouseleave(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to be fired when the mouse leaves an element, or trigger that handler on an element. * @@ -1234,7 +1214,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mousemove/} * @since 1.4.3 */ - mousemove(eventData: TData, handler: JQuery.EventHandler | false): this; + mousemove(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "mousemove" JavaScript event, or trigger that event on an element. * @@ -1251,7 +1231,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mouseout/} * @since 1.4.3 */ - mouseout(eventData: TData, handler: JQuery.EventHandler | false): this; + mouseout(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "mouseout" JavaScript event, or trigger that event on an element. * @@ -1268,7 +1248,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mouseover/} * @since 1.4.3 */ - mouseover(eventData: TData, handler: JQuery.EventHandler | false): this; + mouseover(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "mouseover" JavaScript event, or trigger that event on an element. * @@ -1285,7 +1265,7 @@ interface JQuery { * @see {@link https://api.jquery.com/mouseup/} * @since 1.4.3 */ - mouseup(eventData: TData, handler: JQuery.EventHandler | false): this; + mouseup(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "mouseup" JavaScript event, or trigger that event on an element. * @@ -1346,17 +1326,7 @@ interface JQuery { * @see {@link https://api.jquery.com/off/} * @since 1.7 */ - off(events: string, selector: string, handler: JQuery.EventHandler | false): this; - /** - * Remove an event handler. - * - * @param events An object where the string keys represent one or more space-separated event types and optional - * namespaces, and the values represent handler functions previously attached for the event(s). - * @param selector A selector which should match the one originally passed to .on() when attaching event handlers. - * @see {@link https://api.jquery.com/off/} - * @since 1.7 - */ - off(events: JQuery.PlainObject | false>, selector: string): this; + off(events: string, selector: JQuery.Selector, handler: JQuery.EventHandler | false): this; /** * Remove an event handler. * @@ -1367,17 +1337,25 @@ interface JQuery { * @see {@link https://api.jquery.com/off/} * @since 1.7 */ - off(events: string, selector_handler?: string | JQuery.EventHandler | false): this; + off(events: string, selector_handler?: JQuery.Selector | JQuery.EventHandler | false): this; /** * Remove an event handler. * - * @param events A jQuery.Event object. - * An object where the string keys represent one or more space-separated event types and optional + * @param events An object where the string keys represent one or more space-separated event types and optional * namespaces, and the values represent handler functions previously attached for the event(s). + * @param selector A selector which should match the one originally passed to .on() when attaching event handlers. * @see {@link https://api.jquery.com/off/} * @since 1.7 */ - off(events?: JQuery.Event | JQuery.PlainObject | false>): this; + off(events: JQuery.PlainObject | false>, selector?: JQuery.Selector): this; + /** + * Remove an event handler. + * + * @param event A jQuery.Event object. + * @see {@link https://api.jquery.com/off/} + * @since 1.7 + */ + off(event?: JQuery.Event): this; /** * Set the current coordinates of every element in the set of matched elements, relative to the document. * @@ -1411,37 +1389,33 @@ interface JQuery { * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the * selector is null or omitted, the event is always triggered when it reaches the selected element. * @param data Data to be passed to the handler in event.data when an event is triggered. - * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand - * for a function that simply does return false. + * @param handler A function to execute when the event is triggered. * @see {@link https://api.jquery.com/on/} * @since 1.7 */ - on(events: string, selector: string | null, data: TData, handler: JQuery.EventHandler | false): this; - /** - * Attach an event handler function for one or more events to the selected elements. - * - * @param events An object in which the string keys represent one or more space-separated event types and optional - * namespaces, and the values represent a handler function to be called for the event(s). - * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If - * the selector is null or omitted, the handler is always called when it reaches the selected element. - * @param data Data to be passed to the handler in event.data when an event occurs. - * @see {@link https://api.jquery.com/on/} - * @since 1.7 - */ - on(events: JQuery.PlainObject | false>, selector: string | null, data: TData): this; + on(events: string, selector: JQuery.Selector | null, data: TData, handler: JQuery.EventHandler): this; /** * Attach an event handler function for one or more events to the selected elements. * * @param events One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin". - * @param selector_data A selector string to filter the descendants of the selected elements that trigger the event. If the - * selector is null or omitted, the event is always triggered when it reaches the selected element. - * Data to be passed to the handler in event.data when an event is triggered. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the + * selector is null or omitted, the event is always triggered when it reaches the selected element. * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand * for a function that simply does return false. * @see {@link https://api.jquery.com/on/} * @since 1.7 */ - on(events: string, selector_data: string | null | TData, handler: JQuery.EventHandler | false): this; + on(events: string, selector: JQuery.Selector | null, handler: JQuery.EventHandler | false): this; + /** + * Attach an event handler function for one or more events to the selected elements. + * + * @param events One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin". + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. + * @see {@link https://api.jquery.com/on/} + * @since 1.7 + */ + on(events: string, data: TData, handler: JQuery.EventHandler): this; /** * Attach an event handler function for one or more events to the selected elements. * @@ -1457,13 +1431,43 @@ interface JQuery { * * @param events An object in which the string keys represent one or more space-separated event types and optional * namespaces, and the values represent a handler function to be called for the event(s). - * @param selector_data A selector string to filter the descendants of the selected elements that will call the handler. If - * the selector is null or omitted, the handler is always called when it reaches the selected element. - * Data to be passed to the handler in event.data when an event occurs. + * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If + * the selector is null or omitted, the handler is always called when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event occurs. * @see {@link https://api.jquery.com/on/} * @since 1.7 */ - on(events: JQuery.PlainObject | false>, selector_data?: string | null | TData): this; + on(events: JQuery.PlainObject | false>, selector: JQuery.Selector | null, data: TData): this; + /** + * Attach an event handler function for one or more events to the selected elements. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If + * the selector is null or omitted, the handler is always called when it reaches the selected element. + * @see {@link https://api.jquery.com/on/} + * @since 1.7 + */ + on(events: JQuery.PlainObject | false>, selector: JQuery.Selector): this; // tslint:disable-line:unified-signatures + /** + * Attach an event handler function for one or more events to the selected elements. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @param data Data to be passed to the handler in event.data when an event occurs. + * @see {@link https://api.jquery.com/on/} + * @since 1.7 + */ + on(events: JQuery.PlainObject | false>, data: TData): this; + /** + * Attach an event handler function for one or more events to the selected elements. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @see {@link https://api.jquery.com/on/} + * @since 1.7 + */ + on(events: JQuery.PlainObject | false>): this; /** * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. * @@ -1471,37 +1475,33 @@ interface JQuery { * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the * selector is null or omitted, the event is always triggered when it reaches the selected element. * @param data Data to be passed to the handler in event.data when an event is triggered. - * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand - * for a function that simply does return false. + * @param handler A function to execute when the event is triggered. * @see {@link https://api.jquery.com/one/} * @since 1.7 */ - one(events: string, selector: string | null, data: TData, handler: JQuery.EventHandler | false): this; + one(events: string, selector: JQuery.Selector | null, data: TData, handler: JQuery.EventHandler): this; /** * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. * * @param events One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin". - * @param selector_data A selector string to filter the descendants of the selected elements that trigger the event. If the - * selector is null or omitted, the event is always triggered when it reaches the selected element. - * Data to be passed to the handler in event.data when an event is triggered. + * @param selector A selector string to filter the descendants of the selected elements that trigger the event. If the + * selector is null or omitted, the event is always triggered when it reaches the selected element. * @param handler A function to execute when the event is triggered. The value false is also allowed as a shorthand * for a function that simply does return false. * @see {@link https://api.jquery.com/one/} * @since 1.7 */ - one(events: string, selector_data: string | null | TData, handler: JQuery.EventHandler | false): this; + one(events: string, selector: JQuery.Selector | null, handler: JQuery.EventHandler | false): this; /** * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. * - * @param events An object in which the string keys represent one or more space-separated event types and optional - * namespaces, and the values represent a handler function to be called for the event(s). - * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If - * the selector is null or omitted, the handler is always called when it reaches the selected element. - * @param data Data to be passed to the handler in event.data when an event occurs. + * @param events One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin". + * @param data Data to be passed to the handler in event.data when an event is triggered. + * @param handler A function to execute when the event is triggered. * @see {@link https://api.jquery.com/one/} * @since 1.7 */ - one(events: JQuery.PlainObject | false>, selector: string | null, data: TData): this; + one(events: string, data: TData, handler: JQuery.EventHandler): this; /** * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. * @@ -1517,13 +1517,43 @@ interface JQuery { * * @param events An object in which the string keys represent one or more space-separated event types and optional * namespaces, and the values represent a handler function to be called for the event(s). - * @param selector_data A selector string to filter the descendants of the selected elements that will call the handler. If - * the selector is null or omitted, the handler is always called when it reaches the selected element. - * Data to be passed to the handler in event.data when an event occurs. + * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If + * the selector is null or omitted, the handler is always called when it reaches the selected element. + * @param data Data to be passed to the handler in event.data when an event occurs. * @see {@link https://api.jquery.com/one/} * @since 1.7 */ - one(events: JQuery.PlainObject | false>, selector_data?: string | null | TData): this; + one(events: JQuery.PlainObject | false>, selector: JQuery.Selector | null, data: TData): this; + /** + * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @param selector A selector string to filter the descendants of the selected elements that will call the handler. If + * the selector is null or omitted, the handler is always called when it reaches the selected element. + * @see {@link https://api.jquery.com/one/} + * @since 1.7 + */ + one(events: JQuery.PlainObject | false>, selector: JQuery.Selector): this; // tslint:disable-line:unified-signatures + /** + * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @param data Data to be passed to the handler in event.data when an event occurs. + * @see {@link https://api.jquery.com/one/} + * @since 1.7 + */ + one(events: JQuery.PlainObject | false>, data: TData): this; + /** + * Attach a handler to an event for the elements. The handler is executed at most once per element per event type. + * + * @param events An object in which the string keys represent one or more space-separated event types and optional + * namespaces, and the values represent a handler function to be called for the event(s). + * @see {@link https://api.jquery.com/one/} + * @since 1.7 + */ + one(events: JQuery.PlainObject | false>): this; /** * Set the CSS outer height of each element in the set of matched elements. * @@ -1770,7 +1800,7 @@ interface JQuery { * @see {@link https://api.jquery.com/ready/} * @since 1.0 */ - ready(handler: ($: JQueryStatic) => void): this; + ready(handler: ($: JQueryStatic) => void): this; /** * Remove the set of matched elements from the DOM. * @@ -1843,7 +1873,7 @@ interface JQuery { * @see {@link https://api.jquery.com/resize/} * @since 1.4.3 */ - resize(eventData: TData, handler: JQuery.EventHandler | false): this; + resize(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "resize" JavaScript event, or trigger that event on an element. * @@ -1860,7 +1890,7 @@ interface JQuery { * @see {@link https://api.jquery.com/scroll/} * @since 1.4.3 */ - scroll(eventData: TData, handler: JQuery.EventHandler | false): this; + scroll(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "scroll" JavaScript event, or trigger that event on an element. * @@ -1908,7 +1938,7 @@ interface JQuery { * @see {@link https://api.jquery.com/select/} * @since 1.4.3 */ - select(eventData: TData, handler: JQuery.EventHandler | false): this; + select(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "select" JavaScript event, or trigger that event on an element. * @@ -2107,7 +2137,7 @@ interface JQuery { * @see {@link https://api.jquery.com/submit/} * @since 1.4.3 */ - submit(eventData: TData, handler: JQuery.EventHandler | false): this; + submit(eventData: TData, handler: JQuery.EventHandler): this; /** * Bind an event handler to the "submit" JavaScript event, or trigger that event on an element. * @@ -2208,7 +2238,7 @@ interface JQuery { * @since 1.0 * @since 1.3 */ - trigger(eventType: string | JQuery.Event, extraParameters?: any[] | JQuery.PlainObject): this; + trigger(eventType: string | JQuery.Event, extraParameters?: any[] | JQuery.PlainObject | string | number): this; /** * Execute all handlers attached to an element for an event. * @@ -2219,7 +2249,7 @@ interface JQuery { * @since 1.2 * @since 1.3 */ - triggerHandler(eventType: string | JQuery.Event, extraParameters?: any[] | JQuery.PlainObject): undefined | any; + triggerHandler(eventType: string | JQuery.Event, extraParameters?: any[] | JQuery.PlainObject | string | number): undefined | any; /** * Remove a previously-attached event handler from the elements. * @@ -4152,7 +4182,8 @@ declare namespace JQuery { // region Events - class Event { + // This should be a class but doesn't work correctly under the JQuery namespace. Event should be an inner class of jQuery. + interface Event { /** * The current DOM element within the event bubbling phase. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 0e2e7ee350..4ebc7b9fe1 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -1376,14 +1376,1025 @@ function JQuery() { } } - function on() { - function false_handler_shorthand() { - $().on('events', false); + function events() { + // [bind() overloads] https://github.com/jquery/api.jquery.com/issues/1048 + function bind() { + // $ExpectType JQuery + $('p').bind('myEvent', 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').bind('myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').bind('myEvent', false); + + // $ExpectType JQuery + $('p').bind({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + }, + myEvent2: false + }); + + // $ExpectType JQuery + $('p').bind('myEvent', null); + + // $ExpectType JQuery + $('p').bind('myEvent', undefined); } - function typed_event_data() { - $('#myElement').on('custom', 45, (event, data) => { - event.data === 23; + function delegate() { + // $ExpectType JQuery + $('table').delegate('td', 'myEvent', 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').delegate('td', 'myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').delegate('td', 'myEvent', false); + + // $ExpectType JQuery + $('table').delegate('td', { + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }); + } + + function off() { + // $ExpectType JQuery + $('table').off('myEvent', 'td', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').off('myEvent', 'td', false); + + // $ExpectType JQuery + $('table').off('myEvent', 'td'); + + // $ExpectType JQuery + $('table').off('myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').off('myEvent', false); + + // $ExpectType JQuery + $('table').off('myEvent'); + + // $ExpectType JQuery + $('table').off({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 'td'); + + // $ExpectType JQuery + $('table').off({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }); + + // $ExpectType JQuery + $('table').off($.Event('myEvent')); + + // $ExpectType JQuery + $('table').off(); + } + + function on() { + // $ExpectType JQuery + $('table').on('myEvent', 'td', 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').on('myEvent', null, 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').on('myEvent', 'td', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').on('myEvent', 'td', false); + + // $ExpectType JQuery + $('table').on('myEvent', 3, function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').on('myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').on('myEvent', false); + + // $ExpectType JQuery + $('table').on({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 'td', 'myData'); + + // $ExpectType JQuery + $('table').on({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, null, 'myData'); + + // $ExpectType JQuery + $('table').on({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 'td'); + + // $ExpectType JQuery + $('table').on({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 3); + + // $ExpectType JQuery + $('table').on({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }); + } + + function one() { + // $ExpectType JQuery + $('table').one('myEvent', 'td', 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').one('myEvent', null, 'myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').one('myEvent', 'td', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').one('myEvent', 'td', false); + + // $ExpectType JQuery + $('table').one('myEvent', 3, function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').one('myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').one('myEvent', false); + + // $ExpectType JQuery + $('table').one({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 'td', 'myData'); + + // $ExpectType JQuery + $('table').one({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, null, 'myData'); + + // $ExpectType JQuery + $('table').one({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 'td'); + + // $ExpectType JQuery + $('table').one({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }, 3); + + // $ExpectType JQuery + $('table').one({ + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }); + } + + function trigger() { + // $ExpectType JQuery + $('p').trigger('myEvent', ['Custom', 'Event']); + + // $ExpectType JQuery + $('p').trigger('myEvent', { myData: 'myData' }); + + // $ExpectType JQuery + $('p').trigger('myEvent', 'Custom'); + + // $ExpectType JQuery + $('p').trigger('myEvent', 3); + + // $ExpectType JQuery + $('p').trigger($.Event('myEvent'), ['Custom', 'Event']); + + // $ExpectType JQuery + $('p').trigger($.Event('myEvent'), { myData: 'myData' }); + + // $ExpectType JQuery + $('p').trigger($.Event('myEvent'), 'Custom'); + + // $ExpectType JQuery + $('p').trigger($.Event('myEvent'), 3); + } + + function triggerHandler() { + // $ExpectType any + $('p').triggerHandler('myEvent', ['Custom', 'Event']); + + // $ExpectType any + $('p').triggerHandler('myEvent', { myData: 'myData' }); + + // $ExpectType any + $('p').triggerHandler('myEvent', 'Custom'); + + // $ExpectType any + $('p').triggerHandler('myEvent', 3); + + // $ExpectType any + $('p').triggerHandler($.Event('myEvent'), ['Custom', 'Event']); + + // $ExpectType any + $('p').triggerHandler($.Event('myEvent'), { myData: 'myData' }); + + // $ExpectType any + $('p').triggerHandler($.Event('myEvent'), 'Custom'); + + // $ExpectType any + $('p').triggerHandler($.Event('myEvent'), 3); + } + + function unbind() { + // $ExpectType JQuery + $('p').unbind('myEvent', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').unbind('myEvent', false); + + // $ExpectType JQuery + $('p').unbind('myEvent'); + + // $ExpectType JQuery + $('p').unbind($.Event('myEvent')); + + // $ExpectType JQuery + $('p').unbind(); + } + + function undelegate() { + // $ExpectType JQuery + $('table').undelegate('td', 'click', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('table').undelegate('td', 'click', false); + + // $ExpectType JQuery + $('table').undelegate('td', 'click'); + + // $ExpectType JQuery + $('table').undelegate('td', { + myEvent1(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, + myEvent2: false + }); + + // $ExpectType JQuery + $('table').undelegate('.tt'); + + // $ExpectType JQuery + $('table').undelegate(); + } + + function blur() { + // $ExpectType JQuery + $('p').blur('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').blur(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').blur(false); + + // $ExpectType JQuery + $('p').blur(); + } + + function change() { + // $ExpectType JQuery + $('p').change('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').change(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').change(false); + + // $ExpectType JQuery + $('p').change(); + } + + function click() { + // $ExpectType JQuery + $('p').click('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').click(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').click(false); + + // $ExpectType JQuery + $('p').click(); + } + + function contextmenu() { + // $ExpectType JQuery + $('p').contextmenu('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').contextmenu(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').contextmenu(false); + + // $ExpectType JQuery + $('p').contextmenu(); + } + + function dblclick() { + // $ExpectType JQuery + $('p').dblclick('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').dblclick(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').dblclick(false); + + // $ExpectType JQuery + $('p').dblclick(); + } + + function focus() { + // $ExpectType JQuery + $('p').focus('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focus(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focus(false); + + // $ExpectType JQuery + $('p').focus(); + } + + function focusin() { + // $ExpectType JQuery + $('p').focusin('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focusin(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focusin(false); + + // $ExpectType JQuery + $('p').focusin(); + } + + function focusout() { + // $ExpectType JQuery + $('p').focusout('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focusout(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').focusout(false); + + // $ExpectType JQuery + $('p').focusout(); + } + + function keydown() { + // $ExpectType JQuery + $('p').keydown('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keydown(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keydown(false); + + // $ExpectType JQuery + $('p').keydown(); + } + + function keypress() { + // $ExpectType JQuery + $('p').keypress('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keypress(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keypress(false); + + // $ExpectType JQuery + $('p').keypress(); + } + + function keyup() { + // $ExpectType JQuery + $('p').keyup('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keyup(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').keyup(false); + + // $ExpectType JQuery + $('p').keyup(); + } + + function mousedown() { + // $ExpectType JQuery + $('p').mousedown('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mousedown(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mousedown(false); + + // $ExpectType JQuery + $('p').mousedown(); + } + + function mouseenter() { + // $ExpectType JQuery + $('p').mouseenter('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseenter(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseenter(false); + + // $ExpectType JQuery + $('p').mouseenter(); + } + + function mouseleave() { + // $ExpectType JQuery + $('p').mouseleave('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseleave(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseleave(false); + + // $ExpectType JQuery + $('p').mouseleave(); + } + + function mousemove() { + // $ExpectType JQuery + $('p').mousemove('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mousemove(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mousemove(false); + + // $ExpectType JQuery + $('p').mousemove(); + } + + function mouseout() { + // $ExpectType JQuery + $('p').mouseout('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseout(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseout(false); + + // $ExpectType JQuery + $('p').mouseout(); + } + + function mouseover() { + // $ExpectType JQuery + $('p').mouseover('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseover(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseover(false); + + // $ExpectType JQuery + $('p').mouseover(); + } + + function mouseup() { + // $ExpectType JQuery + $('p').mouseup('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseup(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').mouseup(false); + + // $ExpectType JQuery + $('p').mouseup(); + } + + function resize() { + // $ExpectType JQuery + $('p').resize('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').resize(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').resize(false); + + // $ExpectType JQuery + $('p').resize(); + } + + function scroll() { + // $ExpectType JQuery + $('p').scroll('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').scroll(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').scroll(false); + + // $ExpectType JQuery + $('p').scroll(); + } + + function select() { + // $ExpectType JQuery + $('p').select('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').select(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').select(false); + + // $ExpectType JQuery + $('p').select(); + } + + function submit() { + // $ExpectType JQuery + $('p').submit('myData', function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').submit(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').submit(false); + + // $ExpectType JQuery + $('p').submit(); + } + + function hover() { + // $ExpectType JQuery + $('p').hover(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').hover(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }, false); + + // $ExpectType JQuery + $('p').hover(false, function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').hover(false, false); + + // $ExpectType JQuery + $('p').hover(function(event) { + // $ExpectType HTMLElement + this; + // $ExpectType Event + event; + }); + + // $ExpectType JQuery + $('p').hover(false); + } + + function ready() { + // $ExpectType JQuery + $('p').ready(($) => { + // $ExpectType JQueryStatic + $; }); } } @@ -1563,12 +2574,6 @@ function EffectsOptions() { } function JQuery_Event() { - function type_guard(e: object) { - if (e instanceof JQuery.Event) { - e.isDefaultPrevented() === true; - } - } - function mixin() { const e = $.Event('keydown', { mySpecialKeyCode: JQuery.Key.CapsLock,