Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle the case of an empty string. Fixes #13401 #1162

Closed
wants to merge 2 commits into from

5 participants

@markraddatz

This will fix .replaceWith() when you pass an empty string.

src/manipulation.js
@@ -433,7 +433,7 @@ jQuery.extend({
for ( ; i < l; i++ ) {
elem = elems[ i ];
- if ( elem || elem === 0 ) {
+ if ( elem || typeof elem === "string" || elem === 0 ) {
@jaubourg Collaborator
jaubourg added a note

Why not elem === "" instead?

@staabm
staabm added a note

the test also handles the case of " ", which wouldn't work with elem === ""

@jaubourg Collaborator
jaubourg added a note

... but would have been caught by elem since !!" " === true.

elem === "" should work as well.

@gibson042 Collaborator

-3 bytes by changing this condition to elem != null && elem !== false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gibson042
Collaborator

I approve of this pull, but it changes the behavior of other manipulation methods (append/prepend/before/after and *To/insert* inversions) and thus may not be fit for a patch release. But there is a regression with respect to 1.8 replaceWith, so maybe we should special-case "" or something (I'm open to suggestion on that).

@markraddatz

From this perspective I totally agree that the change causes more harm than good. I prefer to have a lean childNodes list without clutters of empty strings. I opt in for a solution that uses the 1.8 replaceWith way without domManip.

@gibson042
Collaborator

@markraddatz Do you want to submit another pull request with that approach? I'd suggest keeping the .domManip dependency but just adding a value === "" check in .replaceWith that either redefines value or (probably better) skips .domManip in favor of .remove.

@markraddatz

I submitted a new pull request that skips .domManip for this special case.

@dmethvin
Owner

Closing in favor of gh-1163.

@dmethvin dmethvin closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 1 deletion.
  1. +1 −1  src/manipulation.js
  2. +17 −0 test/unit/manipulation.js
View
2  src/manipulation.js
@@ -433,7 +433,7 @@ jQuery.extend({
for ( ; i < l; i++ ) {
elem = elems[ i ];
- if ( elem || elem === 0 ) {
+ if ( elem || elem === "" || elem === 0 ) {
@gibson042 Collaborator

-3 bytes by changing this condition to elem != null && elem !== false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
// Add nodes directly
if ( jQuery.type( elem ) === "object" ) {
View
17 test/unit/manipulation.js
@@ -2225,3 +2225,20 @@ test( "Make sure specific elements with content created correctly (#13232)", 20,
ok( jQuery.nodeName( this, results[ i ] ) );
});
});
+
+test( "jQuery.buildFragment - Handle the case of an empty string (#13401)", 4, function() {
+ var div = jQuery( "<div/>" ),
+ p = jQuery( "<p/>" );
+
+ div.html( p );
+ equal( div.html(), "<p></p>", "Check element exists" );
+
+ p.replaceWith( " " );
+ equal( div.html(), " ", "Check element is replaced" );
+
+ div.html( p );
+ equal( div.html(), "<p></p>", "Check element exists" );
+
+ p.replaceWith( "" );
+ equal( div.html(), "", "Check element is replaced" );
+});
Something went wrong with that request. Please try again.