Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#7623 closed bug (fixed)

Exception thrown in replaceWith

Reported by: mattlong Owned by: Rick Waldron
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.

Change History (9)

comment:1 Changed 7 years ago by snover

#7658 is a duplicate of this ticket.

comment:2 Changed 7 years ago by Rick Waldron

Summary: Exception thrown in replaceWidthException thrown in replaceWith

comment:3 Changed 7 years ago by SlexAxton

Component: unfiledmanipulation
Keywords: replaceWith emptySet pushStack added
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.

comment:4 Changed 7 years ago by matteosistisette

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.

comment:5 Changed 7 years ago by 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.

comment:6 Changed 7 years ago by Rick Waldron

Owner: set to Rick Waldron
Priority: lowhigh
Status: openassigned

comment:7 in reply to:  5 Changed 7 years ago by jitter

Replying to 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.

comment:8 Changed 7 years ago by pettys

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

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

comment:9 Changed 7 years ago by john

Milestone: 1.6
Resolution: fixed
Status: assignedclosed

This was actually resolved in jQuery 1.6.

Note: See TracTickets for help on using tickets.