Skip to main content

Bug Tracker

Side navigation

#7623 closed bug (fixed)

Opened November 24, 2010 05:04PM UTC

Closed April 17, 2011 04:46PM UTC

Last modified March 09, 2012 08:08PM UTC

Exception thrown in replaceWith

Reported by: mattlong Owned by: rwaldron
Priority: high Milestone: 1.6
Component: manipulation Version: 1.4.4
Keywords: replaceWith emptySet pushStack Cc:
Blocked by: Blocking:
Description

An exception is thrown (Syntax error, unrecognized expression), when replaceWith is invoked on an empty set with a replacement value of "\\n". e.g:

$('.nonexistent-class').replaceWith("\\n");

The bug is due to this LOC within the replaceWith function, which is only reached when the set its invoked on is empty:

return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );

Sure enough, simply executing $("\\n") throws the same exception.

Attachments (0)
Change History (9)

Changed December 01, 2010 12:16AM UTC by snover comment:1

#7658 is a duplicate of this ticket.

Changed December 01, 2010 12:28AM UTC by rwaldron comment:2

summary: Exception thrown in replaceWidthException thrown in replaceWith

Changed December 01, 2010 06:32AM UTC by SlexAxton comment:3

component: unfiledmanipulation
keywords: → replaceWith emptySet pushStack
priority: undecidedlow
status: newopen
version: 1.4.31.4.4

Thanks for the bug report. I can see where this would be an issue.

Until it is fixed, I'd suggest wrapping calls that use this technique in a check:

var set = $('.class');

if (set.length) {

set.replaceWith("\\n");

}

Thanks for your report.

Changed December 05, 2010 09:00PM UTC by matteosistisette comment:4

after() suffers from the same problem.

Priority should be raised, it can't be "low". Note that when an exception is thrown, all subsequent instructions within the same funcion are dropped, so this is a huge issue unless you put a check on each and every call to after(), replaceWidth() and any other affected methods.

Changed December 07, 2010 08:56AM UTC by anonymous comment:5

another problem with replaceWith invoked on an empty set

$(".nonexistent-class").replaceWith(function() { return $(this).attr("title") });

The function is called on the Window object.

Changed December 07, 2010 01:48PM UTC by rwaldron comment:6

owner: → rwaldron
priority: lowhigh
status: openassigned

Changed December 08, 2010 09:49PM UTC by jitter comment:7

Replying to [comment:5 anonymous]:

another problem with replaceWith invoked on an empty set
> $(".nonexistent-class").replaceWith(function() { return $(this).attr("title") });
> 
The function is called on the Window object.

With jQuery-git at least this doesn't throw an exception anymore.

Changed April 07, 2011 03:38PM UTC by pettys comment:8

Here's a reproduction of the strange behavior when passing a function to replaceWith called on an empty set:

http://jsfiddle.net/xjUWd/2/

Changed April 17, 2011 04:46PM UTC by john comment:9

milestone: → 1.6
resolution: → fixed
status: assignedclosed

This was actually resolved in jQuery 1.6.