Fixed a bug with $("body") in dynamic documents, refactored $(...), stopped the test...
[jquery.git] / src / jquery / coreTest.js
index 67dea20..4b29c8f 100644 (file)
@@ -67,6 +67,15 @@ test("attr(String)", function() {
        ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );\r
 });\r
 \r
+test("attr(String, Function|String)", function() {\r
+       ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );\r
+       ok( $('#text2').attr('value', "${this.id}")[0].value == "text2", "Set value from id" );\r
+       reset();\r
+       $('#text1, #text2').attr({value: "${this.id + 'foobar'}"});\r
+       ok( $('#text1')[0].value == "text1foobar", "Set value from id" );\r
+       ok( $('#text2')[0].value == "text2foobar", "Set value from id" );\r
+});\r
+\r
 test("attr(Hash)", function() {\r
        expect(1);\r
        var pass = true;\r
@@ -87,7 +96,7 @@ test("attr(String, Object)", function() {
        ok( pass, "Set Attribute" );\r
        \r
        $("#name").attr('name', 'something');\r
-       ok( $("#name").name() == 'something', 'Set name attribute' );\r
+       ok( $("#name").attr('name') == 'something', 'Set name attribute' );\r
        $("#check2").attr('checked', true);\r
        ok( document.getElementById('check2').checked == true, 'Set checked attribute' );\r
        $("#check2").attr('checked', false);\r
@@ -98,19 +107,21 @@ test("attr(String, Object)", function() {
        ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );\r
 });\r
 \r
-test("attr(String, Object)x", function() {\r
-       expect(2);\r
-       stop();\r
-       $.get('data/dashboard.xml', function(xml) { \r
-         var titles = [];\r
-         $('tab', xml).each(function() {\r
-           titles.push($(this).attr('title'));\r
-         });\r
-         ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );\r
-         ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );\r
-         start();\r
+if ( location.protocol != "file:" ) {\r
+       test("attr(String, Object)x", function() {\r
+               expect(2);\r
+               stop();\r
+               $.get('data/dashboard.xml', function(xml) { \r
+               var titles = [];\r
+               $('tab', xml).each(function() {\r
+               titles.push($(this).attr('title'));\r
+               });\r
+               ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );\r
+               ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );\r
+               start();\r
+               });\r
        });\r
-});\r
+}\r
 \r
 test("css(String|Hash)", function() {\r
        expect(8);\r
@@ -170,8 +181,8 @@ test("wrap(String|Element)", function() {
        ok( result.text() == defaultText, 'Check for element wrapping' );\r
 });\r
 \r
-test("append(String|Element|Array<Element>)", function() {\r
-       expect(4);\r
+test("append(String|Element|Array<Element>|jQuery)", function() {\r
+       expect(5);\r
        var defaultText = 'Try them out:'\r
        var result = $('#first').append('<b>buga</b>');\r
        ok( result.text() == defaultText + 'buga', 'Check if text appending works' );\r
@@ -186,10 +197,15 @@ test("append(String|Element|Array&lt;Element&gt;)", function() {
        expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
        $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);\r
        ok( expected == $('#sap').text(), "Check for appending of array of elements" );\r
+       \r
+       reset();\r
+       expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
+       $('#sap').append($("#first, #yahoo"));\r
+       ok( expected == $('#sap').text(), "Check for appending of jQuery object" );\r
 });\r
 \r
-test("prepend(String|Element|Array&lt;Element&gt;)", function() {\r
-       expect(4);\r
+test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(5);\r
        var defaultText = 'Try them out:'\r
        var result = $('#first').prepend('<b>buga</b>');\r
        ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );\r
@@ -204,10 +220,15 @@ test("prepend(String|Element|Array&lt;Element&gt;)", function() {
        expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
        $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);\r
        ok( expected == $('#sap').text(), "Check for prepending of array of elements" );\r
+       \r
+       reset();\r
+       expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
+       $('#sap').prepend($("#first, #yahoo"));\r
+       ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );\r
 });\r
 \r
-test("before(String|Element|Array&lt;Element&gt;)", function() {\r
-       expect(3);\r
+test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(4);\r
        var expected = 'This is a normal link: bugaYahoo';\r
        $('#yahoo').before('<b>buga</b>');\r
        ok( expected == $('#en').text(), 'Insert String before' );\r
@@ -221,10 +242,15 @@ test("before(String|Element|Array&lt;Element&gt;)", function() {
        expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
        $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);\r
        ok( expected == $('#en').text(), "Insert array of elements before" );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
+       $('#yahoo').before($("#first, #mark"));\r
+       ok( expected == $('#en').text(), "Insert jQuery before" );\r
 });\r
 \r
-test("after(String|Element|Array&lt;Element&gt;)", function() {\r
-       expect(3);\r
+test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(4);\r
        var expected = 'This is a normal link: Yahoobuga';\r
        $('#yahoo').after('<b>buga</b>');\r
        ok( expected == $('#en').text(), 'Insert String after' );\r
@@ -238,6 +264,11 @@ test("after(String|Element|Array&lt;Element&gt;)", function() {
        expected = "This is a normal link: YahooTry them out:diveintomark";\r
        $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);\r
        ok( expected == $('#en').text(), "Insert array of elements after" );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: YahooTry them out:diveintomark";\r
+       $('#yahoo').after($("#first, #mark"));\r
+       ok( expected == $('#en').text(), "Insert jQuery after" );\r
 });\r
 \r
 test("end()", function() {\r
@@ -261,6 +292,7 @@ test("clone()", function() {
 test("filter()", function() {\r
        isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );\r
        isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array&lt;String&gt;)" );\r
+       isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );\r
 });\r
 \r
 test("not(String)", function() {\r
@@ -322,145 +354,6 @@ test("$.extend(Object, Object, Object, Object)", function() {
        isSet ( options2, options2Copy, "Check if not modified: options2 must not be modified" );\r
 });\r
 \r
-test("expressions - element", function() {\r
-       expect(5);\r
-       ok( $("*").size() >= 30, "Select all" );\r
-       t( "Element Selector", "div", ["main","foo"] );\r
-       t( "Element Selector", "body", ["body"] );\r
-       t( "Element Selector", "html", ["html"] );\r
-       t( "Parent Element", "div div", ["foo"] );\r
-});\r
-\r
-test("expressions - id", function() {\r
-       expect(8);\r
-       t( "ID Selector", "#body", ["body"] );\r
-       t( "ID Selector w/ Element", "body#body", ["body"] );\r
-       t( "ID Selector w/ Element", "ul#first", [] );\r
-       \r
-       t( "ID Selector, child ID present", "#form > #radio1", ["radio1"] );  // bug #267\r
-       t( "ID Selector, not an ancestor ID", "#form  #first", [] );\r
-       t( "ID Selector, not a child ID", "#form > #option1a", [] );\r
-       \r
-       t( "All Children of ID", "#foo/*", ["sndp", "en", "sap"]  );\r
-       t( "All Children of ID with no children", "#firstUL/*", []  );\r
-});\r
-\r
-test("expressions - class", function() {\r
-       expect(4);\r
-       t( "Class Selector", ".blog", ["mark","simon"] );\r
-       t( "Class Selector", ".blog.link", ["simon"] );\r
-       t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );\r
-       t( "Parent Class Selector", "p .blog", ["mark","simon"] );\r
-});\r
-\r
-test("expressions - multiple", function() {\r
-       expect(4);\r
-       t( "Comma Support", "a.blog, div", ["mark","simon","main","foo"] );\r
-       t( "Comma Support", "a.blog , div", ["mark","simon","main","foo"] );\r
-       t( "Comma Support", "a.blog ,div", ["mark","simon","main","foo"] );\r
-       t( "Comma Support", "a.blog,div", ["mark","simon","main","foo"] );\r
-});\r
-\r
-test("expressions - child and adjacent", function() {\r
-       expect(14);\r
-       t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );\r
-       t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );\r
-       t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );\r
-       t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );\r
-       t( "Child w/ Class", "p > a.blog", ["mark","simon"] );\r
-       t( "All Children", "code > *", ["anchor1","anchor2"] );\r
-       t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );\r
-       t( "Adjacent", "a + a", ["groups"] );\r
-       t( "Adjacent", "a +a", ["groups"] );\r
-       t( "Adjacent", "a+ a", ["groups"] );\r
-       t( "Adjacent", "a+a", ["groups"] );\r
-       t( "Adjacent", "p + p", ["ap","en","sap"] );\r
-       t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );\r
-       t( "First Child", "p:first-child", ["firstp","sndp"] );\r
-});\r
-\r
-test("expressions - attributes", function() {\r
-       expect(19);\r
-       t( "Attribute Exists", "a[@title]", ["google"] );\r
-       t( "Attribute Exists", "*[@title]", ["google"] );\r
-       t( "Attribute Exists", "[@title]", ["google"] );\r
-       \r
-       t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] );\r
-       t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] );\r
-       t( "Attribute Equals", "a[@rel=bookmark]", ["simon1"] );\r
-       t( "Multiple Attribute Equals", "input[@type='hidden'],input[@type='radio']", ["hidden1","radio1","radio2"] );\r
-       t( "Multiple Attribute Equals", "input[@type=\"hidden\"],input[@type='radio']", ["hidden1","radio1","radio2"] );\r
-       t( "Multiple Attribute Equals", "input[@type=hidden],input[@type=radio]", ["hidden1","radio1","radio2"] );\r
-       \r
-       t( "Attribute Begins With", "a[@href ^= 'http://www']", ["google","yahoo"] );\r
-       t( "Attribute Ends With", "a[@href $= 'org/']", ["mark"] );\r
-       t( "Attribute Contains", "a[@href *= 'google']", ["google","groups"] );\r
-       \r
-       t("Select options via [@selected]", "#select1 option[@selected]", ["option1a"] );\r
-       t("Select options via [@selected]", "#select2 option[@selected]", ["option2d"] );\r
-       t("Select options via [@selected]", "#select3 option[@selected]", ["option3b", "option3c"] );\r
-       \r
-       t( "Grouped Form Elements", "input[@name='foo[bar]']", ["hidden2"] );\r
-       \r
-       t( ":not() Existing attribute", "select:not([@multiple])", ["select1", "select2"]);\r
-       t( ":not() Equals attribute", "select:not([@name=select1])", ["select2", "select3"]);\r
-       t( ":not() Equals quoted attribute", "select:not([@name='select1'])", ["select2", "select3"]);\r
-});\r
-\r
-test("expressions - pseudo (:) selctors", function() {\r
-       expect(30);\r
-       t( "First Child", "p:first-child", ["firstp","sndp"] );\r
-       t( "Last Child", "p:last-child", ["sap"] );\r
-       t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );\r
-       t( "Empty", "ul:empty", ["firstUL"] );\r
-       t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name"] );\r
-       t( "Disabled UI Element", "input:disabled", ["text2"] );\r
-       t( "Checked UI Element", "input:checked", ["radio2","check1"] );\r
-       t( "Selected Option Element", "option:selected", ["option1a","option2d","option3b","option3c"] );\r
-       t( "Text Contains", "a:contains('Google')", ["google","groups"] );\r
-       t( "Text Contains", "a:contains('Google Groups')", ["groups"] );\r
-       t( "Element Preceded By", "p ~ div", ["foo"] );\r
-       t( "Not", "a.blog:not(.link)", ["mark"] );\r
-       \r
-       t( "nth Element", "p:nth(1)", ["ap"] );\r
-       t( "First Element", "p:first", ["firstp"] );\r
-       t( "Last Element", "p:last", ["first"] );\r
-       t( "Even Elements", "p:even", ["firstp","sndp","sap"] );\r
-       t( "Odd Elements", "p:odd", ["ap","en","first"] );\r
-       t( "Position Equals", "p:eq(1)", ["ap"] );\r
-       t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] );\r
-       t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] );\r
-       t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] );\r
-       t( "Is Visible", "input:visible", ["text1","text2","radio1","radio2","check1","check2","name"] );\r
-       t( "Is Hidden", "input:hidden", ["hidden1","hidden2"] );\r
-       \r
-       t( "Form element :input", ":input", ["text1", "text2", "radio1", "radio2", "check1", "check2", "hidden1", "hidden2", "name", "button", "area1", "select1", "select2", "select3"] );\r
-       t( "Form element :radio", ":radio", ["radio1", "radio2"] );\r
-       t( "Form element :checkbox", ":checkbox", ["check1", "check2"] );\r
-       t( "Form element :text", ":text", ["text1", "text2", "hidden2", "name"] );\r
-       t( "Form element :radio:checked", ":radio:checked", ["radio2"] );\r
-       t( "Form element :checkbox:checked", ":checkbox:checked", ["check1"] );\r
-       t( "Form element :checkbox:checked, :radio:checked", ":checkbox:checked, :radio:checked", ["check1", "radio2"] );\r
-});\r
-\r
-test("expressions - basic xpath", function() {\r
-       expect(14);\r
-       ok( jQuery.find("//*").length >= 30, "All Elements (//*)" );\r
-       t( "All Div Elements", "//div", ["main","foo"] );\r
-       t( "Absolute Path", "/html/body", ["body"] );\r
-       t( "Absolute Path w/ *", "/* /body", ["body"] );\r
-       t( "Long Absolute Path", "/html/body/dl/div/div/p", ["sndp","en","sap"] );\r
-       t( "Absolute and Relative Paths", "/html//div", ["main","foo"] );\r
-       t( "All Children, Explicit", "//code/*", ["anchor1","anchor2"] );\r
-       t( "All Children, Implicit", "//code/", ["anchor1","anchor2"] );\r
-       t( "Attribute Exists", "//a[@title]", ["google"] );\r
-       t( "Attribute Equals", "//a[@rel='bookmark']", ["simon1"] );\r
-       t( "Parent Axis", "//p/..", ["main","foo"] );\r
-       t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","sndp","en","sap"] );\r
-       t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","sndp","en","sap"] );\r
-       t( "Has Children", "//p[a]", ["firstp","ap","en","sap"] );\r
-});\r
-\r
 test("val()", function() {\r
        expect(2);\r
        ok( $("#text1").val() == "Test", "Check for value of input element" );\r
@@ -486,27 +379,6 @@ test("html(String)", function() {
        ok( pass, "Set HTML" );\r
 });\r
 \r
-test("id()", function() {\r
-       expect(3);\r
-       ok( $(document.getElementById('main')).id() == "main", "Check for id" );\r
-       ok( $("#foo").id() == "foo", "Check for id" );\r
-       ok( !$("head").id(), "Check for id" );\r
-});\r
-\r
-test("title()", function() {\r
-       expect(2);\r
-       ok( $(document.getElementById('google')).title() == "Google!", "Check for title" );\r
-       ok( !$("#yahoo").title(), "Check for title" );\r
-});\r
-\r
-test("name()", function() {\r
-       expect(3);\r
-       ok( $(document.getElementById('text1')).name() == "action", "Check for name" );\r
-       ok( $("#hidden1").name() == "hidden", "Check for name" );\r
-       ok( !$("#area1").name(), "Check for name" );\r
-});\r
-\r
-\r
 test("siblings([String])", function() {\r
        expect(3);\r
        isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );\r
@@ -565,15 +437,7 @@ test("removeAttr(String", function() {
        ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );\r
 });\r
 \r
-test("unbind(event)", function() {\r
-       expect(3);\r
-       var el = $("#firstp");\r
-       el.click(function() {\r
-               ok( true, "Fake normal bind" );\r
-       });\r
-       el.click(function(event) {\r
-               el.unbind(event);\r
-               ok( true, "Fake onebind" );\r
-       });\r
-       el.click().click();\r
-});\r
+test("text(String, Boolean)", function() {\r
+       ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );\r
+       ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>", true)[0].innerHTML == "Hello cruel world!", "Check stripped text" );\r
+});
\ No newline at end of file