Bug Tracker

Ticket #7623 (closed bug: fixed)

Opened 4 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 4 years ago by snover

#7658 is a duplicate of this ticket.

comment:2 Changed 4 years ago by rwaldron

  • Summary changed from Exception thrown in replaceWidth to Exception thrown in replaceWith

comment:3 Changed 4 years ago by SlexAxton

  • Keywords replaceWith emptySet pushStack added
  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to manipulation
  • Version changed from 1.4.3 to 1.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 4 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 follow-up: ↓ 7 Changed 4 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 4 years ago by rwaldron

  • Owner set to rwaldron
  • Priority changed from low to high
  • Status changed from open to assigned

comment:7 in reply to: ↑ 5 Changed 4 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 4 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 4 years ago by john

  • Status changed from assigned to closed
  • Resolution set to fixed
  • Milestone set to 1.6

This was actually resolved in jQuery 1.6.

Note: See TracTickets for help on using tickets.