Ticket #7623 (closed bug: fixed)
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:2 Changed 2 years ago by rwaldron
- Summary changed from Exception thrown in replaceWidth to Exception thrown in replaceWith
comment:3 Changed 2 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 2 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 2 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 2 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 2 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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

#7658 is a duplicate of this ticket.