module("manipulation", { teardown: moduleTeardown }); // Ensure that an extended Array prototype doesn't break jQuery Array.prototype.arrayProtoFn = function(arg) { throw("arrayProtoFn should not be called"); }; var bareObj = function(value) { return value; }; var functionReturningObj = function(value) { return (function() { return value; }); }; /* ======== local reference ======= bareObj and functionReturningObj can be used to test passing functions to setters See testVal below for an example bareObj( value ); This function returns whatever value is passed in functionReturningObj( value ); Returns a function that returns the value */ test("text()", function() { expect(5); var expected = "This link has class=\"blog\": Simon Willison's Weblog"; equal( jQuery("#sap").text(), expected, "Check for merged text of more then one element." ); // Check serialization of text values equal( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." ); notEqual( jQuery(document).text(), "", "Retrieving text for the document retrieves all text (#10724)."); // Retrieve from document fragments #10864 var frag = document.createDocumentFragment(); frag.appendChild( document.createTextNode("foo") ); equal( jQuery( frag ).text(), "foo", "Document Fragment Text node was retreived from .text()."); var $newLineTest = jQuery("
test
testy
").appendTo("#moretests"); $newLineTest.find("br").replaceWith("\n"); equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" ); }); test("text(undefined)", function() { expect(1); equal( jQuery("#foo").text("Hello cruel world!"); equal( 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(valueObj("hi!")); equal( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" ); equal( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" ); // Blackberry 4.6 doesn't maintain comments in the DOM equal( jQuery("#nonnodes")[0].childNodes.length < 3 ? 8 : j[2].nodeType, 8, "Check node,textnode,comment with text()" ); }; test("text(String)", function() { testText(bareObj); }); test("text(Function)", function() { testText(functionReturningObj); }); test("text(Function) with incoming value", function() { expect(2); var old = "This link has class=\"blog\": Simon Willison's Weblog"; jQuery("#sap").text(function(i, val) { equal( val, old, "Make sure the incoming value is correct." ); return "foobar"; }); equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." ); QUnit.reset(); }); var testWrap = function(val) { expect(19); var defaultText = "Try them out:"; var result = jQuery("#first").wrap(val( "
" )).text(); equal( defaultText, result, "Check for wrapping of on-the-fly html" ); ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); QUnit.reset(); result = jQuery("#first").wrap(val( document.getElementById("empty") )).parent(); ok( result.is("ol"), "Check for element wrapping" ); equal( result.text(), defaultText, "Check for element wrapping" ); QUnit.reset(); jQuery("#check1").click(function() { var checkbox = this; ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); jQuery(checkbox).wrap(val( "" )); ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); }).click(); // using contents will get comments regular, text, and comment nodes var j = jQuery("#nonnodes").contents(); j.wrap(val( "" )); // Blackberry 4.6 doesn't maintain comments in the DOM equal( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[0].childNodes.length, "Check node,textnode,comment wraps ok" ); equal( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" ); // Try wrapping a disconnected node var cacheLength = 0; for (var i in jQuery.cache) { cacheLength++; } j = jQuery("