From 0b3165fd23ba4d240dd72f93d315bb5f7bf970e8 Mon Sep 17 00:00:00 2001 From: jeresig Date: Mon, 25 Jan 2010 19:22:28 -0500 Subject: [PATCH] Make sure that the correct context is being passed in for replaceWith(fn). Fixes #5798. --- src/manipulation.js | 6 ++++++ test/unit/manipulation.js | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/manipulation.js b/src/manipulation.js index 37a8cfd..8c188c2 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -233,6 +233,12 @@ jQuery.fn.extend({ // this can help fix replacing a parent with child elements if ( !jQuery.isFunction( value ) ) { value = jQuery( value ).detach(); + + } else { + return this.each(function(i) { + var self = jQuery(this), old = self.html(); + self.replaceWith( value.call( this, i, old ) ); + }); } return this.each(function() { diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index bbf9a3a..168c17c 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -720,6 +720,16 @@ test("replaceWith(String|Element|Array<Element>|jQuery)", function() { test("replaceWith(Function)", function() { testReplaceWith(functionReturningObj); + + expect(16); + + var y = jQuery("#yahoo")[0]; + + jQuery(y).replaceWith(function(){ + equals( this, y, "Make sure the context is coming in correctly." ); + }); + + reset(); }) test("replaceAll(String|Element|Array<Element>|jQuery)", function() { -- 1.7.10.4