diff --git a/types/jquery/JQuery.d.ts b/types/jquery/JQuery.d.ts index cbcc12d956..ed0147a89f 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/ }\` @@ -9990,7 +9993,14 @@ $( "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, 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 9e4dea80ee..0acf6e0d24 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -5565,11 +5565,21 @@ function JQuery() { $('p').replaceWith([new HTMLElement()]); // $ExpectType JQuery - $('p').replaceWith(function() { + $('p').replaceWith(document.createTextNode('bar')); + + // $ExpectType JQuery + $('p').replaceWith(document.createComment('bar')); + + // $ExpectType JQuery + $('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; }); } @@ -5693,6 +5703,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 @@ -5702,7 +5718,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 @@ -5805,6 +5826,12 @@ function JQuery() { // $ExpectType JQuery $('p').add($('span')); + + // $ExpectType JQuery + $('p').add(document.createTextNode('bar')); + + // $ExpectType JQuery + $('p').add(document.createComment('bar')); } function closest() {