From: Bartosz DziewoƄski Date: Thu, 20 Aug 2015 15:27:42 +0000 (+0200) Subject: jquery.byteLimit: Trigger 'change' event after limitting X-Git-Tag: 1.31.0-rc.0~9920^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmes_infos.php?a=commitdiff_plain;h=3bb3601ffc30ebabb1dadd38b901ef1ddf410b43;p=lhc%2Fweb%2Fwiklou.git jquery.byteLimit: Trigger 'change' event after limitting For better interoperability with scripts that are unaware of it. Since jquery.byteLimit itself listens to the 'change' event, tweak the conditions to avoid getting stuck in an infinite loop. Change-Id: Ie782d14ba7d676b248109a3179bbec19552dfc52 --- diff --git a/resources/src/jquery/jquery.byteLimit.js b/resources/src/jquery/jquery.byteLimit.js index e2315d2326..5ffcae8dcf 100644 --- a/resources/src/jquery/jquery.byteLimit.js +++ b/resources/src/jquery/jquery.byteLimit.js @@ -86,11 +86,11 @@ } } - newVal = inpParts.join( '' ); - return { - newVal: newVal, - trimmed: true + newVal: inpParts.join( '' ), + // For pathological fn() that always returns a value longer than the limit, we might have + // ended up not trimming - check for this case to avoid infinite loops + trimmed: newVal !== inpParts.join( '' ) }; } @@ -219,6 +219,9 @@ // This is a side-effect of limiting after the fact. if ( res.trimmed === true ) { this.value = res.newVal; + // Trigger a 'change' event to let other scripts attached to this node know that the value + // was changed. This will also call ourselves again, but that's okay, it'll be a no-op. + $el.trigger( 'change' ); } // Always adjust prevSafeVal to reflect the input value. Not doing this could cause // trimValueForByteLength to compare the new value to an empty string instead of the