X-Git-Url: http://git.asbjorn.biz/?p=jquery.git;a=blobdiff_plain;f=test%2Funit%2Fmanipulation.js;h=739868bd7bf5d9d4b53e8f8cce78bb713d95dbd1;hp=0327f93b2379adc26c73c53d4e9d7dfee214e2da;hb=f1bd0cfc4d3eabf566b6a3345d888e6db67bc00b;hpb=7c04a64016c1f295dfdf4e731f693f8400a987bc diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 0327f93..739868b 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1,152 +1,327 @@ +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; }); }; + 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()." ); }); -test("wrap(String|Element)", function() { - expect(10); +var testText = function(valueObj) { + expect(4); + 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(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()" ); + + // Blackberry 4.6 doesn't maintain comments in the DOM + equals( 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) { + equals( val, old, "Make sure the incoming value is correct." ); + return "foobar"; + }); + + equals( 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('
').text(); + var result = jQuery('#first').wrap(val( '
' )).text(); equals( defaultText, result, 'Check for wrapping of on-the-fly html' ); ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' ); - reset(); + QUnit.reset(); var defaultText = 'Try them out:' - var result = jQuery('#first').wrap(document.getElementById('empty')).parent(); + var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent(); ok( result.is('ol'), 'Check for element wrapping' ); equals( result.text(), defaultText, 'Check for element wrapping' ); - reset(); + 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( '' ); + 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(""); - 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" ); + j.wrap(val( "" )); + + // Blackberry 4.6 doesn't maintain comments in the DOM + equals( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[0].childNodes.length, "Check node,textnode,comment wraps ok" ); + equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" ); // Try wrapping a disconnected node - j = jQuery("