From 246e0785b887f8070d9d2640bca066edc7d99b17 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 14 Mar 2014 10:16:19 +0000 Subject: [PATCH 1/3] jQuery: JSDoc completeness within reach well nearly --- jquery/jquery-tests.ts | 81 ++++++++++++---- jquery/jquery.d.ts | 207 +++++++++++++++++++++++++++-------------- svgjs/svgjs.d.ts | 8 ++ 3 files changed, 210 insertions(+), 86 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 022e35f9c..53fbe55c3 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -1857,6 +1857,26 @@ function test_getScript() { }); } +function test_jQueryget() { + console.log($("li").get(0)); + console.log($("li")[0]); + console.log($("li").get(-1)); + $("*", document.body).click(function (event) { + event.stopPropagation(); + var domElement = $(this).get(0); + $("span:first").text("Clicked on - " + domElement.nodeName); + }); + + function display(divs) { + var a = []; + for (var i = 0; i < divs.length; i++) { + a.push(divs[i].innerHTML); + } + $("span").text(a.join(" ")); + } + display($("div").get().reverse()); +} + function test_globalEval() { jQuery.globalEval("var newVar = true;"); } @@ -2021,6 +2041,38 @@ function test_height() { }); } +function test_wrap() { + $(".inner").wrap("
"); + $(".inner").wrap(function () { + return "
"; + }); + $("span").wrap("

"); + $("p").wrap(document.createElement("div")); + $("p").wrap($(".doublediv")); +} + +function test_wrapAll() { + $(".inner").wrapAll("
"); + $("p").wrapAll("
"); + $("span").wrapAll("

"); + $("p").wrapAll(document.createElement("div")); + $("p").wrapAll($(".doublediv")); +} + +function test_wrapInner() { + $(".inner").wrapInner("
"); + $(".inner").wrapInner(function () { + return "
"; + }); + var elem: Element; + $(elem).wrapInner("
"); + $(elem).wrapInner("
"); + $("p").wrapInner(""); + $("body").wrapInner("

"); + $("p").wrapInner(document.createElement("b")); + $("p").wrapInner($("")); +} + function test_width() { // Returns width of browser viewport $(window).width(); @@ -2172,17 +2224,11 @@ function test_index() { function test_innerHeight() { var p = $("p:first"); $("p:last").text("innerHeight:" + p.innerHeight()); - - p.innerHeight(123); - p.innerHeight('123px'); } function test_innerWidth() { var p = $("p:first"); $("p:last").text("innerWidth:" + p.innerWidth()); - - p.innerWidth(123); - p.innerWidth('123px'); } function test_outerHeight() { @@ -2190,9 +2236,6 @@ function test_outerHeight() { $("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight( true ):" + p.outerHeight(true)); - - p.outerHeight(123); - p.outerHeight('123px'); } function test_outerWidth() { @@ -2200,9 +2243,6 @@ function test_outerWidth() { $("p:last").text( "outerWidth:" + p.outerWidth() + " , outerWidth( true ):" + p.outerWidth(true)); - - p.outerWidth(123); - p.outerWidth('123px'); } function test_scrollLeft() { @@ -2920,16 +2960,25 @@ function test_map() { return $(this).val(); }).get().join(", ")); var mappedItems = $("li").map(function (index) { - var replacement = $("
  • ").text($(this).text()).get(0); - if (index == 0) { + var replacement:any = $("
  • ").text($(this).text()).get(0); + if (index === 0) { + + // Make the first item all caps $(replacement).text($(replacement).text().toUpperCase()); - } else if (index == 1 || index == 3) { + } else if (index === 1 || index === 3) { + + // Delete the second and fourth items replacement = null; - } else if (index == 2) { + } else if (index === 2) { + + // Make two of the third item and add some text replacement = [replacement, $("
  • ").get(0)]; $(replacement[0]).append(" - A"); $(replacement[1]).append("Extra - B"); } + + // Replacement will be a dom element, null, + // or an array of dom elements return replacement; }); $("#results").append(mappedItems); diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 5012181e8..540301746 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -1418,39 +1418,11 @@ interface JQuery { */ innerHeight(): number; - /** - * Sets the inner height on elements in the set of matched elements, including padding but not border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - innerHeight(height: number): JQuery; - - /** - * Sets the inner height on elements in the set of matched elements, including padding but not border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - innerHeight(height: string): JQuery; - /** * Get the current computed width for the first element in the set of matched elements, including padding but not border. */ innerWidth(): number; - /** - * Sets the inner width on elements in the set of matched elements, including padding but not border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - innerWidth(width: number): JQuery; - - /** - * Sets the inner width on elements in the set of matched elements, including padding but not border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - innerWidth(width: string): JQuery; - /** * Get the current coordinates of the first element in the set of matched elements, relative to the document. */ @@ -1475,20 +1447,6 @@ interface JQuery { */ outerHeight(includeMargin?: boolean): number; - /** - * Sets the outer height on elements in the set of matched elements, including padding and border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - outerHeight(height: number): JQuery; - - /** - * Sets the outer height on elements in the set of matched elements, including padding and border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - outerHeight(height: string): JQuery; - /** * Get the current computed width for the first element in the set of matched elements, including padding and border. * @@ -1496,20 +1454,6 @@ interface JQuery { */ outerWidth(includeMargin?: boolean): number; - /** - * Sets the outer width on elements in the set of matched elements, including padding and border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - outerWidth(width: number): JQuery; - - /** - * Sets the outer width on elements in the set of matched elements, including padding and border. - * - * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). - */ - outerWidth(width: string): JQuery; - /** * Get the current coordinates of the first element in the set of matched elements, relative to the offset parent. */ @@ -3169,15 +3113,79 @@ interface JQuery { */ toArray(): any[]; + /** + * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. + */ unwrap(): JQuery; - wrap(wrappingElement: any): JQuery; - wrap(func: (index: any) => any): JQuery; + /** + * Wrap an HTML structure around each element in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrap(wrappingElement: JQuery): JQuery; + /** + * Wrap an HTML structure around each element in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrap(wrappingElement: Element): JQuery; + /** + * Wrap an HTML structure around each element in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrap(wrappingElement: string): JQuery; + /** + * Wrap an HTML structure around each element in the set of matched elements. + * + * @param func A callback function returning the HTML content or jQuery object to wrap around the matched elements. Receives the index position of the element in the set as an argument. Within the function, this refers to the current element in the set. + */ + wrap(func: (index: number) => any): JQuery; - wrapAll(wrappingElement: any): JQuery; + /** + * Wrap an HTML structure around all elements in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrapAll(wrappingElement: JQuery): JQuery; + /** + * Wrap an HTML structure around all elements in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrapAll(wrappingElement: Element): JQuery; + /** + * Wrap an HTML structure around all elements in the set of matched elements. + * + * @param wrappingElement A selector, element, HTML string, or jQuery object specifying the structure to wrap around the matched elements. + */ + wrapAll(wrappingElement: string): JQuery; - wrapInner(wrappingElement: any): JQuery; - wrapInner(func: (index: any) => any): JQuery; + /** + * Wrap an HTML structure around the content of each element in the set of matched elements. + * + * @param wrappingElement An HTML snippet, selector expression, jQuery object, or DOM element specifying the structure to wrap around the content of the matched elements. + */ + wrapInner(wrappingElement: JQuery): JQuery; + /** + * Wrap an HTML structure around the content of each element in the set of matched elements. + * + * @param wrappingElement An HTML snippet, selector expression, jQuery object, or DOM element specifying the structure to wrap around the content of the matched elements. + */ + wrapInner(wrappingElement: Element): JQuery; + /** + * Wrap an HTML structure around the content of each element in the set of matched elements. + * + * @param wrappingElement An HTML snippet, selector expression, jQuery object, or DOM element specifying the structure to wrap around the content of the matched elements. + */ + wrapInner(wrappingElement: string): JQuery; + /** + * Wrap an HTML structure around the content of each element in the set of matched elements. + * + * @param func A callback function which generates a structure to wrap around the content of the matched elements. Receives the index position of the element in the set as an argument. Within the function, this refers to the current element in the set. + */ + wrapInner(func: (index: number) => any): JQuery; /** * Iterate over a jQuery object, executing a function for each matched element. @@ -3186,25 +3194,84 @@ interface JQuery { */ each(func: (index: number, elem: Element) => any): JQuery; - get(index?: number): any; + /** + * Retrieve one of the elements matched by the jQuery object. + * + * @param index A zero-based integer indicating which element to retrieve. + */ + get(index: number): HTMLElement; + /** + * Retrieve the elements matched by the jQuery object. + */ + get(): any[]; + /** + * Search for a given element from among the matched elements. + */ index(): number; + /** + * Search for a given element from among the matched elements. + * + * @param selector A selector representing a jQuery collection in which to look for an element. + */ index(selector: string): number; - index(element: any): number; + /** + * Search for a given element from among the matched elements. + * + * @param element The DOM element or first element within the jQuery object to look for. + */ + index(element: JQuery): number; + /** + * Search for a given element from among the matched elements. + * + * @param element The DOM element or first element within the jQuery object to look for. + */ + index(element: Element): number; - // Properties + /** + * The number of elements in the jQuery object. + */ length: number; + /** + * A selector representing selector passed to jQuery(), if any, when creating the original set. + * version deprecated: 1.7, removed: 1.9 + */ selector: string; - [x: string]: any; - [x: number]: HTMLElement; + [index: string]: any; + [index: number]: HTMLElement; - // Traversing - add(selector: string, context?: any): JQuery; - add(...elements: any[]): JQuery; + /** + * Add elements to the set of matched elements. + * + * @param selector A string representing a selector expression to find additional elements to add to the set of matched elements. + * @param context The point in the document at which the selector should begin matching; similar to the context argument of the $(selector, context) method. + */ + add(selector: string, context?: Element): JQuery; + /** + * Add elements to the set of matched elements. + * + * @param elements One or more elements to add to the set of matched elements. + */ + add(...elements: Element[]): JQuery; + /** + * Add elements to the set of matched elements. + * + * @param html An HTML fragment to add to the set of matched elements. + */ add(html: string): JQuery; + /** + * Add elements to the set of matched elements. + * + * @param obj An existing jQuery object to add to the set of matched elements. + */ add(obj: JQuery): JQuery; - children(selector?: any): JQuery; + /** + * Get the children of each element in the set of matched elements, optionally filtered by a selector. + * + * @param selector A string containing a selector expression to match elements against. + */ + children(selector?: string): JQuery; closest(selector: string): JQuery; closest(selector: string, context?: Element): JQuery; diff --git a/svgjs/svgjs.d.ts b/svgjs/svgjs.d.ts index 44f32d9b1..b68b89f80 100644 --- a/svgjs/svgjs.d.ts +++ b/svgjs/svgjs.d.ts @@ -270,3 +270,11 @@ declare module svgjs { f?: number; } } +interface JQuery { + /** + * Retrieve one of the elements matched by the jQuery object. + * + * @param index A zero-based integer indicating which element to retrieve. + */ + get(index: number): svgjs.LinkedHTMLElement; +} From cfde90d4e7ff96b1f18b16fded5e47c6308c175d Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 14 Mar 2014 10:51:24 +0000 Subject: [PATCH 2/3] jQuery: tests now cast to svgjs.LinkedHTMLElement This could be made implicit by extending the JQuery interface with: get(index: number): svgjs.LinkedHTMLElement; Looking at the library it didn't seem sensible to create a dependency on jQuery even though the tests have one. --- svgjs/svgjs-tests.ts | 8 ++++---- svgjs/svgjs.d.ts | 8 -------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/svgjs/svgjs-tests.ts b/svgjs/svgjs-tests.ts index 845702ac4..fce9c3a74 100644 --- a/svgjs/svgjs-tests.ts +++ b/svgjs/svgjs-tests.ts @@ -1,5 +1,5 @@ -/// /// +/// // create svg drawing paper @@ -46,7 +46,7 @@ function renderSVG(data:string) { var container = SVG(div) // this creates an SVG tag inside container.svg(data) // this creates an SVG inside the SVG var $inner = $(div).find("svg svg") - var inner:svgjs.Element = $inner.get(0).instance + var inner:svgjs.Element = ($inner.get(0)).instance // Copy in the important attributes root.attr('x', inner.attr('x')) @@ -61,8 +61,8 @@ function renderSVG(data:string) { // Activate and Label all child paths var index = 0 el.find("rect, path, circle, ellipse").each(function() { - var $path = $(this) - var path = $path.get(0).instance + var $path: JQuery = $(this) + var path = ($path.get(0)).instance var uniqueId = "path"+index++ path.attr({"path-id": uniqueId}) }) diff --git a/svgjs/svgjs.d.ts b/svgjs/svgjs.d.ts index b68b89f80..44f32d9b1 100644 --- a/svgjs/svgjs.d.ts +++ b/svgjs/svgjs.d.ts @@ -270,11 +270,3 @@ declare module svgjs { f?: number; } } -interface JQuery { - /** - * Retrieve one of the elements matched by the jQuery object. - * - * @param index A zero-based integer indicating which element to retrieve. - */ - get(index: number): svgjs.LinkedHTMLElement; -} From 002e1bf169e267b71c643ad8321d6a124c0e23c1 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Fri, 14 Mar 2014 11:02:11 +0000 Subject: [PATCH 3/3] jQuery: Put back in what I accidentally removed --- jquery/jquery-tests.ts | 13 ++++++++++ jquery/jquery.d.ts | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index 53fbe55c3..ad7a9444c 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -2224,11 +2224,18 @@ function test_index() { function test_innerHeight() { var p = $("p:first"); $("p:last").text("innerHeight:" + p.innerHeight()); + + p.innerHeight(123); + p.innerHeight('123px'); } function test_innerWidth() { var p = $("p:first"); $("p:last").text("innerWidth:" + p.innerWidth()); + + + p.innerWidth(123); + p.innerWidth('123px'); } function test_outerHeight() { @@ -2236,6 +2243,9 @@ function test_outerHeight() { $("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight( true ):" + p.outerHeight(true)); + + p.outerHeight(123); + p.outerHeight('123px'); } function test_outerWidth() { @@ -2243,6 +2253,9 @@ function test_outerWidth() { $("p:last").text( "outerWidth:" + p.outerWidth() + " , outerWidth( true ):" + p.outerWidth(true)); + + p.outerWidth(123); + p.outerWidth('123px'); } function test_scrollLeft() { diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 540301746..58a672d42 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -1418,11 +1418,39 @@ interface JQuery { */ innerHeight(): number; + /** + * Sets the inner height on elements in the set of matched elements, including padding but not border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + innerHeight(height: number): JQuery; + + /** + * Sets the inner height on elements in the set of matched elements, including padding but not border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + innerHeight(height: string): JQuery; + /** * Get the current computed width for the first element in the set of matched elements, including padding but not border. */ innerWidth(): number; + /** + * Sets the inner width on elements in the set of matched elements, including padding but not border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + innerWidth(width: number): JQuery; + + /** + * Sets the inner width on elements in the set of matched elements, including padding but not border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + innerWidth(width: string): JQuery; + /** * Get the current coordinates of the first element in the set of matched elements, relative to the document. */ @@ -1447,6 +1475,20 @@ interface JQuery { */ outerHeight(includeMargin?: boolean): number; + /** + * Sets the outer height on elements in the set of matched elements, including padding and border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + outerHeight(height: number): JQuery; + + /** + * Sets the outer height on elements in the set of matched elements, including padding and border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + outerHeight(height: string): JQuery; + /** * Get the current computed width for the first element in the set of matched elements, including padding and border. * @@ -1454,6 +1496,20 @@ interface JQuery { */ outerWidth(includeMargin?: boolean): number; + /** + * Sets the outer width on elements in the set of matched elements, including padding and border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + outerWidth(width: number): JQuery; + + /** + * Sets the outer width on elements in the set of matched elements, including padding and border. + * + * @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string). + */ + outerWidth(width: string): JQuery; + /** * Get the current coordinates of the first element in the set of matched elements, relative to the offset parent. */