X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fmanipulation.js;h=9242dddb5f7ebcb5e886d75e1088397cf92ce633;hb=66975de2d249643779e2b3daad0457f7f5f92508;hp=00984168ca385de088031b221f029fc9128687fa;hpb=4bda398e6aa63082c41bb0d06fcc184acd7f76c5;p=jquery.git
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 0098416..9242ddd 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -4,13 +4,16 @@ var bareObj = function(value) { return value; };
var functionReturningObj = function(value) { return (function() { return value; }); };
test("text()", function() {
- expect(1);
+ expect(2);
var expected = "This link has class=\"blog\": Simon Willison's Weblog";
equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
+
+ // Check serialization of text values
+ equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
});
var testWrap = function(val) {
- expect(15);
+ expect(18);
var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '
' )).text();
equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
@@ -34,7 +37,7 @@ var testWrap = function(val) {
var j = jQuery("#nonnodes").contents();
j.wrap(val( "" ));
equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
- equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
+ equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
// Try wrapping a disconnected node
j = jQuery("").wrap(val( "" ));
@@ -51,6 +54,20 @@ var testWrap = function(val) {
equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
equals( j.length, 1, "There should only be one element (no cloning)." );
equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
+
+ // Wrap an element with a jQuery set
+ j = jQuery("").wrap(jQuery(""));
+ equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
+
+ // Wrap an element with a jQuery set and event
+ result = jQuery("").click(function(){
+ ok(true, "Event triggered.");
+ });
+
+ j = jQuery("").wrap(result);
+ equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
+
+ j.parent().trigger("click");
}
test("wrap(String|Element)", function() {
@@ -88,7 +105,7 @@ test("wrapAll(String|Element)", function() {
// TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
// test("wrapAll(Function)", function() {
-// testWrapAll(functionReturningObj);
+// testWrapAll(functionReturningObj);
// })
var testWrapInner = function(val) {
@@ -113,7 +130,7 @@ test("wrapInner(String|Element)", function() {
// TODO: wrapInner uses wrapAll -- get wrapAll working with Function
// test("wrapInner(Function)", function() {
-// testWrapInner(functionReturningObj)
+// testWrapInner(functionReturningObj)
// })
var testUnwrap = function() {
@@ -124,7 +141,7 @@ var testUnwrap = function() {
var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
abcdef = jQuery('#unwrap span').get();
- equals( jQuery('#unwrap1 span, #unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
+ equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
@@ -148,7 +165,7 @@ test("unwrap()", function() {
});
var testAppend = function(valueObj) {
- expect(21);
+ expect(22);
var defaultText = 'Try them out:'
var result = jQuery('#first').append(valueObj('buga'));
equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
@@ -221,6 +238,8 @@ var testAppend = function(valueObj) {
t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
+ equals( "Two nodes", jQuery('').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
+
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
var d = jQuery("").appendTo("#nonnodes").append(j);
@@ -232,7 +251,7 @@ var testAppend = function(valueObj) {
}
test("append(String|Element|Array<Element>|jQuery)", function() {
- testAppend(bareObj);
+ testAppend(bareObj);
});
test("append(Function)", function() {
@@ -463,7 +482,7 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
});
var testReplaceWith = function(val) {
- expect(12);
+ expect(14);
jQuery('#yahoo').replaceWith(val( 'buga' ));
ok( jQuery("#replace")[0], 'Replace element with string' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
@@ -488,6 +507,13 @@ var testReplaceWith = function(val) {
var set = jQuery("").replaceWith(val("test"));
equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
equals( set.length, 1, "Replace the disconnected node." );
+
+ var $div = jQuery("").appendTo("body");
+ $div.replaceWith("");
+ equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
+ jQuery('.replacewith').remove();
}
test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
@@ -523,7 +549,7 @@ test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
});
test("clone()", function() {
- expect(28);
+ expect(30);
equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
var clone = jQuery('#yahoo').clone();
equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
@@ -568,6 +594,11 @@ test("clone()", function() {
div = div.clone(true);
equals( div.length, 1, "One element cloned" );
equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
+
+ div = jQuery("").data({ a: true, b: true });
+ div = div.clone(true);
+ equals( div.data("a"), true, "Data cloned." );
+ equals( div.data("b"), true, "Data cloned." );
});
if (!isLocal) {
@@ -588,7 +619,7 @@ test("clone() on XML nodes", function() {
}
test("val()", function() {
- expect(9);
+ expect(17);
document.getElementById('text1').value = "bla";
equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
@@ -610,10 +641,37 @@ test("val()", function() {
equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
+ equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
+
+ jQuery('#select3').val("");
+ same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
+
+ var checks = jQuery("").appendTo("#form")
+ .add( jQuery("").appendTo("#form") )
+ .add( jQuery("").appendTo("#form") )
+ .add( jQuery("").appendTo("#form") );
+
+ same( checks.serialize(), "", "Get unchecked values." );
+
+ equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
+
+ checks.val([ "2" ]);
+ same( checks.serialize(), "test=2", "Get a single checked value." );
+
+ checks.val([ "1", "" ]);
+ same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
+
+ checks.val([ "", "2" ]);
+ same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
+
+ checks.val([ "1", "on" ]);
+ same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
+
+ checks.remove();
});
var testVal = function(valueObj) {
- expect(5);
+ expect(6);
jQuery("#text1").val(valueObj( 'test' ));
equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
@@ -627,6 +685,10 @@ var testVal = function(valueObj) {
jQuery("#select1").val(valueObj( 2 ));
equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
+ jQuery("#select1").append("");
+ jQuery("#select1").val(valueObj( 4 ));
+ equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
+
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
j.val(valueObj( "asdf" ));
@@ -643,9 +705,7 @@ test("val(Function)", function() {
})
var testHtml = function(valueObj) {
- expect(17);
-
- window.debug = true;
+ expect(22);
jQuery.scriptorder = 0;
@@ -657,8 +717,6 @@ var testHtml = function(valueObj) {
}
ok( pass, "Set HTML" );
- delete window.debug;
-
reset();
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
@@ -676,6 +734,16 @@ var testHtml = function(valueObj) {
equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
+ var $div2 = jQuery(''), insert = "<div>hello1</div>";
+ equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
+ equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
+ equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
+
+ var map = jQuery("").html(valueObj(""));
+
+ equals( map[0].childNodes.length, 1, "The area was inserted." );
+ equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
+
reset();
jQuery("#main").html(valueObj(''));
@@ -698,15 +766,16 @@ test("html(String)", function() {
test("html(Function)", function() {
testHtml(functionReturningObj);
-})
+});
var testText = function(valueObj) {
expect(4);
- equals( jQuery("#foo").text("
Hello cruel world!
")[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
+ var val = valueObj("
Hello cruel world!
");
+ equals( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
- j.text("hi!");
+ j.text(valueObj("hi!"));
equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );