#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
comment:2 Changed 7 years ago by
| Summary: | Exception thrown in replaceWidth → Exception thrown in replaceWith |
|---|
comment:3 Changed 7 years ago by
| Component: | unfiled → manipulation |
|---|---|
| Keywords: | replaceWith emptySet pushStack added |
| Priority: | undecided → low |
| Status: | new → open |
| Version: | 1.4.3 → 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 7 years ago by
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 7 years ago by
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
| Owner: | set to Rick Waldron |
|---|---|
| Priority: | low → high |
| Status: | open → assigned |
comment:7 Changed 7 years ago by
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 6 years ago by
Here's a reproduction of the strange behavior when passing a function to replaceWith called on an empty set:
comment:9 Changed 6 years ago by
| Milestone: | → 1.6 |
|---|---|
| Resolution: | → fixed |
| Status: | assigned → closed |
This was actually resolved in jQuery 1.6.

#7658 is a duplicate of this ticket.