diff options
author | Jeremy Dunck <jdunck@gmail.com> | 2013-08-19 17:55:05 +0200 |
---|---|---|
committer | Michał Gołębiowski <m.goleb@gmail.com> | 2013-08-19 17:58:00 +0200 |
commit | 7dfe0ad191a592234fd95aa6548bbed40d49ea76 (patch) | |
tree | 90a2e5dcf9152612f2d9674bc4c811514b6b6e63 /src | |
parent | 49670c5f483d944eafdcc494e697f04fd6d9fbbd (diff) | |
download | jquery-7dfe0ad191a592234fd95aa6548bbed40d49ea76.tar.gz jquery-7dfe0ad191a592234fd95aa6548bbed40d49ea76.zip |
Fix #14164: assign className in addClass/removeClass only if changed. Close gh-1331. (cherry-picked from c418b94eb48188cd9329519ae5e030a52dd81cc9)
Diffstat (limited to 'src')
-rw-r--r-- | src/attributes/classes.js | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/attributes/classes.js b/src/attributes/classes.js index f4cdb88a3..6ac81392d 100644 --- a/src/attributes/classes.js +++ b/src/attributes/classes.js @@ -8,7 +8,7 @@ var rclass = /[\t\r\n\f]/g; jQuery.fn.extend({ addClass: function( value ) { - var classes, elem, cur, clazz, j, + var classes, elem, cur, clazz, j, finalValue, i = 0, len = this.length, proceed = typeof value === "string" && value; @@ -37,8 +37,12 @@ jQuery.fn.extend({ cur += clazz + " "; } } - elem.className = jQuery.trim( cur ); + // only assign if different to avoid unneeded rendering. + finalValue = jQuery.trim( cur ); + if ( elem.className !== finalValue ) { + elem.className = finalValue; + } } } } @@ -47,7 +51,7 @@ jQuery.fn.extend({ }, removeClass: function( value ) { - var classes, elem, cur, clazz, j, + var classes, elem, cur, clazz, j, finalValue, i = 0, len = this.length, proceed = arguments.length === 0 || typeof value === "string" && value; @@ -76,7 +80,12 @@ jQuery.fn.extend({ cur = cur.replace( " " + clazz + " ", " " ); } } - elem.className = value ? jQuery.trim( cur ) : ""; + + // only assign if different to avoid unneeded rendering. + finalValue = value ? jQuery.trim( cur ) : ""; + if ( elem.className !== finalValue ) { + elem.className = finalValue; + } } } } |