From 3a45c9d8be31da33f6f449c75c9c8fbfec4991c9 Mon Sep 17 00:00:00 2001 From: John Reilly Date: Wed, 15 Jan 2014 13:26:08 +0000 Subject: [PATCH] jQuery: Add jQuery/$ JSDoc And test suite --- jquery/jquery-tests.ts | 96 ++++++++++++++++++++++++++++++++++++++++++ jquery/jquery.d.ts | 64 ++++++++++++++++++++++++++-- 2 files changed, 156 insertions(+), 4 deletions(-) diff --git a/jquery/jquery-tests.ts b/jquery/jquery-tests.ts index a6cb92ee01..518e8a45d4 100644 --- a/jquery/jquery-tests.ts +++ b/jquery/jquery-tests.ts @@ -2103,6 +2103,102 @@ function test_jquery() { alert(' b is a jQuery object! '); } alert('You are running jQuery version: ' + $.fn.jquery); + + $("div.foo"); + + $("div.foo").click(function () { + $("span", this).addClass("bar"); + }); + + $("div.foo").click(function () { + $(this).slideUp(); + }); + + $.post("url.xml", function (data) { + var $child = $(data).find("child"); + }); + + // Define a plain object + var foo = { foo: "bar", hello: "world" }; + + // Pass it to the jQuery function + var $foo = $(foo); + + // Test accessing property values + var test1 = $foo.prop("foo"); // bar + + // Test setting property values + $foo.prop("foo", "foobar"); + var test2 = $foo.prop("foo"); // foobar + + // Test using .data() as summarized above + $foo.data("keyName", "someValue"); + console.log($foo); // will now contain a jQuery{randomNumber} property + + // Test binding an event name and triggering + $foo.on("eventName", function () { + console.log("eventName was called"); + }); + + $foo.trigger("eventName"); // Logs "eventName was called" + + $foo.triggerHandler("eventName"); // Also logs "eventName was called" + + $("div > p").css("border", "1px solid gray"); + + $("input:radio", document.forms[0]); + + $(document.body).css("background", "black"); + + var myForm: HTMLFormElement; + $(myForm.elements).hide(); + + $("

My new text

").appendTo("body"); + + $(""); + + $(""); + $(""); + + var el = $("1
2
3"); // returns [
, "2",
] + el = $("1
2
3 >"); // returns [
, "2",
, "3 >"] + + $("
", { + "class": "my-div", + on: { + touchstart: function (event) { + // Do something + } + } + }).appendTo("body"); + + $("
") + .addClass("my-div") + .on({ + touchstart: function (event) { + // Do something + } + }) + .appendTo("body"); + + $("

Hello

").appendTo("body") + + $("
", { + "class": "test", + text: "Click me!", + click: function () { + $(this).toggleClass("test"); + } + }) + .appendTo("body"); + + $(function () { + // Document is ready + }); + + jQuery(function ($) { + // Your code using failsafe $ alias here... + }); } function test_keydown() { diff --git a/jquery/jquery.d.ts b/jquery/jquery.d.ts index 7d1ad992b9..1dab9e7908 100644 --- a/jquery/jquery.d.ts +++ b/jquery/jquery.d.ts @@ -553,15 +553,71 @@ interface JQueryStatic { */ holdReady(hold: boolean): void; - (selector: string, context?: any): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param selector A string containing a selector expression + * @param context A DOM Element, Document, or jQuery to use as context + */ + (selector: string, context?: Element): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param selector A string containing a selector expression + * @param context A DOM Element, Document, or jQuery to use as context + */ + (selector: string, context?: JQuery): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param element A DOM element to wrap in a jQuery object. + */ (element: Element): JQuery; - (object: {}): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param elementArray An array containing a set of DOM elements to wrap in a jQuery object. + */ (elementArray: Element[]): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param object A plain object to wrap in a jQuery object. + */ + (object: {}): JQuery; + /** + * Accepts a string containing a CSS selector which is then used to match a set of elements. + * + * @param object An existing jQuery object to clone. + */ (object: JQuery): JQuery; - (func: Function): JQuery; - (array: any[]): JQuery; + /** + * Specify a function to execute when the DOM is fully loaded. + */ (): JQuery; + /** + * Creates DOM elements on the fly from the provided string of raw HTML. + * + * @param html A string of HTML to create on the fly. Note that this parses HTML, not XML. + * @param ownerDocument A document in which the new elements will be created. + */ + (html: string, ownerDocument?: Document): JQuery; + /** + * Creates DOM elements on the fly from the provided string of raw HTML. + * + * @param html A string defining a single, standalone, HTML element (e.g.
or
). + * @param attributes An object of attributes, events, and methods to call on the newly-created element. + */ + (html: string, attributes: Object): JQuery; + + /** + * Binds a function to be executed when the DOM has finished loading. + * + * @param callback A function to execute after the DOM is ready. + */ + (callback: Function): JQuery; + /** * Relinquish jQuery's control of the $ variable. *