From 5c9f9cc6aea035fe8159787f2e422bc48132698a Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Fri, 2 Nov 2018 06:59:22 -0400 Subject: [PATCH 1/2] [jquery] Allow Text/Comment nodes. See https://github.com/jquery/api.jquery.com/issues/879#issuecomment-175774521. --- types/jquery/JQuery.d.ts | 14 +++++++++++--- types/jquery/jquery-tests.ts | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/types/jquery/JQuery.d.ts b/types/jquery/JQuery.d.ts index 123fad3b34..6ece3c1c55 100644 --- a/types/jquery/JQuery.d.ts +++ b/types/jquery/JQuery.d.ts @@ -83,6 +83,7 @@ $( document.body ) * @since 1.4 */ add(selector: JQuery.Selector, context: Element): this; + // TODO: The return type should reflect newly selected types. /** * Create a new jQuery object with elements added to the set of matched elements. * @param selector_elements_html_selection _@param_ `selector_elements_html_selection` @@ -226,7 +227,7 @@ collection.css( "background", "yellow" ); ``` */ - add(selector_elements_html_selection: JQuery.Selector | JQuery.TypeOrArray | JQuery.htmlString | JQuery): this; + add(selector_elements_html_selection: JQuery.Selector | JQuery.TypeOrArray | JQuery.htmlString | JQuery | JQuery.Node): this; /** * Add the previous set of elements on the stack to the current set, optionally filtered by a selector. * @param selector A string containing a selector expression to match the current set of elements against. @@ -5141,7 +5142,9 @@ $( "div b" ) ``` */ - html(htmlString_function: JQuery.htmlString | ((this: TElement, index: number, oldhtml: JQuery.htmlString) => JQuery.htmlString)): this; + html(htmlString_function: JQuery.htmlString | + JQuery.Node | + ((this: TElement, index: number, oldhtml: JQuery.htmlString) => JQuery.htmlString | JQuery.Node)): this; /** * Get the HTML contents of the first element in the set of matched elements. * @see \`{@link https://api.jquery.com/html/ }\` @@ -9836,6 +9839,7 @@ $( "Paragraph. " ).replaceAll( "p" ); ``` */ replaceAll(target: JQuery.Selector | JQuery | JQuery.TypeOrArray): this; + // TODO: Validate signature of `function`. /** * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. * @param newContent_function _@param_ `newContent_function` @@ -9986,7 +9990,11 @@ $( "button" ).on( "click", function() { ``` */ - replaceWith(newContent_function: JQuery.htmlString | JQuery | JQuery.TypeOrArray | ((this: TElement) => any)): this; + replaceWith(newContent_function: JQuery.htmlString | + JQuery | + JQuery.TypeOrArray | + JQuery.Node | + ((this: TElement) => any)): this; /** * Bind an event handler to the "resize" JavaScript event, or trigger that event on an element. * @param eventData An object containing data that will be passed to the event handler. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index b1f63f3ecf..990d7d2ff6 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -5543,6 +5543,12 @@ function JQuery() { // $ExpectType JQuery $('p').replaceWith([new HTMLElement()]); + // $ExpectType JQuery + $('p').replaceWith(document.createTextNode('bar')); + + // $ExpectType JQuery + $('p').replaceWith(document.createComment('bar')); + // $ExpectType JQuery $('p').replaceWith(function() { // $ExpectType HTMLElement @@ -5672,6 +5678,12 @@ function JQuery() { // $ExpectType JQuery $('p').html(''); + // $ExpectType JQuery + $('p').html(document.createTextNode('bar')); + + // $ExpectType JQuery + $('p').html(document.createComment('bar')); + // $ExpectType JQuery $('p').html(function(index, oldhtml) { // $ExpectType HTMLElement @@ -5681,7 +5693,12 @@ function JQuery() { // $ExpectType string oldhtml; - return oldhtml; + switch (index) { + case 0: return document.createTextNode('bar'); + case 1: return document.createComment('bar'); + + default: return oldhtml; + } }); // $ExpectType string @@ -5784,6 +5801,12 @@ function JQuery() { // $ExpectType JQuery $('p').add($('span')); + + // $ExpectType JQuery + $('p').add(document.createTextNode('bar')); + + // $ExpectType JQuery + $('p').add(document.createComment('bar')); } function closest() { From 5001e2c42f6fc98305a683b018e6ff5af4a9ac16 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Fri, 2 Nov 2018 08:02:48 -0400 Subject: [PATCH 2/2] [jquery] Fix declaration of `function` parameter for `.replaceWith()`. --- types/jquery/JQuery.d.ts | 8 +++++--- types/jquery/jquery-tests.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/types/jquery/JQuery.d.ts b/types/jquery/JQuery.d.ts index 6ece3c1c55..e7eccf8030 100644 --- a/types/jquery/JQuery.d.ts +++ b/types/jquery/JQuery.d.ts @@ -9839,7 +9839,6 @@ $( "Paragraph. " ).replaceAll( "p" ); ``` */ replaceAll(target: JQuery.Selector | JQuery | JQuery.TypeOrArray): this; - // TODO: Validate signature of `function`. /** * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. * @param newContent_function _@param_ `newContent_function` @@ -9991,10 +9990,13 @@ $( "button" ).on( "click", function() { ``` */ replaceWith(newContent_function: JQuery.htmlString | - JQuery | + JQuery | JQuery.TypeOrArray | JQuery.Node | - ((this: TElement) => any)): this; + ((this: TElement, index: number, oldhtml: JQuery.htmlString) => JQuery.htmlString | + JQuery | + JQuery.TypeOrArray | + JQuery.Node)): this; /** * Bind an event handler to the "resize" JavaScript event, or trigger that event on an element. * @param eventData An object containing data that will be passed to the event handler. diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 990d7d2ff6..6db5ecb3c8 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -5550,11 +5550,15 @@ function JQuery() { $('p').replaceWith(document.createComment('bar')); // $ExpectType JQuery - $('p').replaceWith(function() { + $('p').replaceWith(function(index, oldhtml) { // $ExpectType HTMLElement this; + // $ExpectType number + index; + // $ExpectType string + oldhtml; - return this; + return undefined! as JQuery.htmlString | JQuery | JQuery.TypeOrArray | JQuery.Node; }); }