From 073bdc108d75990005f5cafe9c1cc6a74a3b9a39 Mon Sep 17 00:00:00 2001 From: Boris Yankov Date: Mon, 5 Nov 2012 13:42:58 +0200 Subject: [PATCH] Several fixes for jQuery and some tests added --- Definitions/jquery-1.8.d.ts | 5 +- Tests/jquery-tests.ts | 177 +++++++++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 2 deletions(-) diff --git a/Definitions/jquery-1.8.d.ts b/Definitions/jquery-1.8.d.ts index 467f498e7..8a6aa748f 100644 --- a/Definitions/jquery-1.8.d.ts +++ b/Definitions/jquery-1.8.d.ts @@ -174,6 +174,8 @@ interface JQueryStatic { ajaxPrefilter(dataTypes: string, handler: (opts: any, originalOpts: any, jqXHR: JQueryXHR) => any): any; ajaxPrefilter(handler: (opts: any, originalOpts: any, jqXHR: JQueryXHR) => any): any; + ajaxSettings: JQueryAjaxSettings; + ajaxSetup(options: any); get(url: string, data?: any, success?: any, dataType?: any): JQueryXHR; @@ -214,6 +216,7 @@ interface JQueryStatic { css(e: any, propertyName: string, value?: any); css(e: any, propertyName: any, value?: any); cssHooks: { [key: string]: any; }; + cssNumber: any; /**** DATA @@ -328,7 +331,7 @@ interface JQuery { ATTRIBUTES ***********/ addClass(classNames: string): JQuery; - addClass(func: (index: any, currentClass?: any) => string): JQuery; + addClass(func: (index: any, currentClass: any) => string): JQuery; attr(attributeName: string): string; attr(attributeName: string, value: any): JQuery; diff --git a/Tests/jquery-tests.ts b/Tests/jquery-tests.ts index 6fc2c7398..a5d3924db 100644 --- a/Tests/jquery-tests.ts +++ b/Tests/jquery-tests.ts @@ -30,7 +30,7 @@ function test_addClass() { $("p:last").addClass("selected"); $("p:last").addClass("selected highlight"); $("div").addClass(function (index, currentClass) { - var addedClass; + var addedClass: string; if (currentClass === "red") { addedClass = "green"; $("p").text("There is one green div"); @@ -697,6 +697,181 @@ function test_children() { $("div").children(".selected").css("color", "blue"); } +function test_clearQueue() { + $("#start").click(function () { + var myDiv = $("div"); + myDiv.show("slow"); + myDiv.animate({ left: '+=200' }, 5000); + myDiv.queue(function () { + var _this = $(this); + _this.addClass("newcolor"); + _this.dequeue(); + }); + myDiv.animate({ left: '-=200' }, 1500); + myDiv.queue(function () { + var _this = $(this); + _this.removeClass("newcolor"); + _this.dequeue(); + }); + myDiv.slideUp(); + + }); + $("#stop").click(function () { + var myDiv = $("div"); + myDiv.clearQueue(); + myDiv.stop(); + }); +} + +function test_click() { + $("#target").click(function () { + alert("Handler for .click() called."); + }); + $("#other").click(function () { + $("#target").click(); + }); + $("p").click(function () { + $(this).slideUp(); + }); + $("p").click(); +} + +function test_clone() { + $('.hello').clone().appendTo('.goodbye'); + var $elem = $('#elem').data({ "arr": [1] }), + $clone = $elem.clone(true) + .data("arr", $.extend([], $elem.data("arr"))); + $("b").clone().prependTo("p"); + $('#copy').append($('#orig .elem') + .clone() + .children('a') + .prepend('foo - ') + .parent() + .clone()); +} + +function test_closest() { + $('li.item-a').closest('ul') + .css('background-color', 'red'); + $('li.item-a').closest('li') + .css('background-color', 'red'); + var listItemII = document.getElementById('ii'); + $('li.item-a').closest('ul', listItemII) + .css('background-color', 'red'); + $('li.item-a').closest('#one', listItemII) + .css('background-color', 'green'); + $(document).bind("click", function (e) { + $(e.target).closest("li").toggleClass("hilight"); + }); + var $listElements = $("li").css("color", "blue"); + $(document).bind("click", function (e) { + $(e.target).closest($listElements).toggleClass("hilight"); + }); +} + +function test_contains() { + jQuery.contains(document.documentElement, document.body); + jQuery.contains(document.body, document.documentElement); +} + +function test_contents() { + $('.container').contents().filter(function () { + return this.nodeType == 3; + }) + .wrap('

') + .end() + .filter('br') + .remove(); + $("#frameDemo").contents().find("a").css("background-color", "#BADA55"); +} + +function test_context() { + $("ul") + .append("
  • " + $("ul").context + "
  • ") + .append("
  • " + $("ul", document.body).context.nodeName + "
  • "); +} + +function test_css() { + $("div").click(function () { + var color = $(this).css("background-color"); + $("#result").html("That div is " + color + "."); + }); + $('div.example').css('width', function (index) { + return index * 50; + }); + $("p").mouseover(function () { + $(this).css("color", "red"); + }); + $("#box").one("click", function () { + $(this).css("width", "+=200"); + }); + var words = $("p:first").text().split(" "); + var text = words.join(" "); + $("p:first").html("" + text + ""); + $("span").click(function () { + $(this).css("background-color", "yellow"); + }); + $("p").hover(function () { + $(this).css({ 'background-color': 'yellow', 'font-weight': 'bolder' }); + }, function () { + var cssObj = { + 'background-color': '#ddd', + 'font-weight': '', + 'color': 'rgb(0,40,244)' + } + $(this).css(cssObj); + }); + $("div").click(function () { + $(this).css({ + width: function (index, value) { + return parseFloat(value) * 1.2; + }, + height: function (index, value) { + return parseFloat(value) * 1.2; + } + + }); + }); +} + +function test_cssHooks() { + if (!$.cssHooks) { + throw ("jQuery 1.4.3 or above is required for this plugin to work"); + return; + } + $.cssHooks["someCSSProp"] = { + get: function (elem, computed, extra) { }, + set: function (elem, value) { } + }; + function styleSupport(prop) { + var vendorProp, supportedProp, + capProp = prop.charAt(0).toUpperCase() + prop.slice(1), + prefixes = ["Moz", "Webkit", "O", "ms"], + div = document.createElement("div"); + + if (prop in div.style) { + supportedProp = prop; + } else { + for (var i = 0; i < prefixes.length; i++) { + vendorProp = prefixes[i] + capProp; + if (vendorProp in div.style) { + supportedProp = vendorProp; + break; + } + } + } + div = null; + $.support[prop] = supportedProp; + return supportedProp; + } + styleSupport("borderRadius"); + + $.cssNumber["someCSSProp"] = true; + $.fx.step["someCSSProp"] = function (fx) { + $.cssHooks["someCSSProp"].set(fx.elem, fx.now + fx.unit); + }; +} + function test_each() { $('li').each(function (index) { alert(index + ': ' + $(this).text());