From 4c54d08d3c4dea48c98efd2e31d7f580dacfd018 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Fri, 23 Jun 2017 14:57:03 -0400 Subject: [PATCH] [jquery] Allow passing output of $.parseHTML() directly to manipulation methods. --- types/jquery/index.d.ts | 21 +++++++++++---------- types/jquery/jquery-tests.ts | 3 +++ types/jquery/test/example-tests.ts | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 6107c98747..8856684262 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -103,7 +103,7 @@ interface JQuery { * @see {@link https://api.jquery.com/after/} * @since 1.0 */ - after(...contents: Array | JQuery>): this; + after(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, after each element in the set of matched elements. * @@ -115,7 +115,7 @@ interface JQuery { * @since 1.4 * @since 1.10 */ - after(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + after(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Register a handler to be called when Ajax requests complete. This is an AjaxEvent. * @@ -219,7 +219,7 @@ interface JQuery { * @see {@link https://api.jquery.com/append/} * @since 1.0 */ - append(...contents: Array | JQuery>): this; + append(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, to the end of each element in the set of matched elements. * @@ -230,7 +230,7 @@ interface JQuery { * @see {@link https://api.jquery.com/append/} * @since 1.4 */ - append(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + append(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Insert every element in the set of matched elements to the end of the target. * @@ -277,7 +277,7 @@ interface JQuery { * @see {@link https://api.jquery.com/before/} * @since 1.0 */ - before(...contents: Array | JQuery>): this; + before(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, before each element in the set of matched elements. * @@ -289,7 +289,7 @@ interface JQuery { * @since 1.4 * @since 1.10 */ - before(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + 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. @@ -1629,7 +1629,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prepend/} * @since 1.0 */ - prepend(...contents: Array | JQuery>): this; + prepend(...contents: Array | JQuery>): this; /** * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. * @@ -1640,7 +1640,7 @@ interface JQuery { * @see {@link https://api.jquery.com/prepend/} * @since 1.4 */ - prepend(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; + prepend(fn: (this: TElement, index: number, html: string) => JQuery.htmlString | JQuery.TypeOrArray | JQuery): this; /** * Insert every element in the set of matched elements to the beginning of the target. * @@ -3067,7 +3067,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.parseHTML/} * @since 1.8 */ - parseHTML(data: string, context: Document | null | undefined, keepScripts: boolean): Node[]; + parseHTML(data: string, context: Document | null | undefined, keepScripts: boolean): JQuery.Node[]; /** * Parses a string into an array of DOM nodes. * @@ -3077,7 +3077,7 @@ interface JQueryStatic { * @see {@link https://api.jquery.com/jQuery.parseHTML/} * @since 1.8 */ - parseHTML(data: string, context_keepScripts?: Document | null | undefined | boolean): Node[]; + parseHTML(data: string, context_keepScripts?: Document | null | undefined | boolean): JQuery.Node[]; /** * Takes a well-formed JSON string and returns the resulting JavaScript value. * @@ -3281,6 +3281,7 @@ interface JQueryStatic { declare namespace JQuery { type TypeOrArray = T | T[]; + type Node = Element | Text | Comment; /** * A string is designated htmlString in jQuery documentation when it is used to represent one or more diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 3464902284..7a05a5630a 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -2546,6 +2546,9 @@ function JQuery() { return $('p'); }); + + // $ExpectType JQuery + $('p').append($.parseHTML('myTextNode ')); } function before() { diff --git a/types/jquery/test/example-tests.ts b/types/jquery/test/example-tests.ts index f0e2e197f6..404aba2013 100644 --- a/types/jquery/test/example-tests.ts +++ b/types/jquery/test/example-tests.ts @@ -2907,7 +2907,7 @@ function examples() { function jQuery_parse_html_0() { var $log = $('#log'), str = 'hello, my name is jQuery.', - html = $.parseHTML(str) as HTMLElement[], + html = $.parseHTML(str), nodeNames: string[] = []; // Append the parsed HTML