Merge "Even more out-factoring to methods in the header"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.byteLimit.js
index afff463..b1692bb 100644 (file)
                // until the limit is statisfied.
                if ( fn ) {
                        // stop, when there is nothing to slice - bug 41450
-                       while ( $.byteLength( fn( inpParts.join( '' ) ) ) > byteLimit && inpParts[1].length > 0 ) {
-                               inpParts[1] = inpParts[1].slice( 0, -1 );
+                       while ( $.byteLength( fn( inpParts.join( '' ) ) ) > byteLimit && inpParts[ 1 ].length > 0 ) {
+                               inpParts[ 1 ] = inpParts[ 1 ].slice( 0, -1 );
                        }
                } else {
                        while ( $.byteLength( inpParts.join( '' ) ) > byteLimit ) {
-                               inpParts[1] = inpParts[1].slice( 0, -1 );
+                               inpParts[ 1 ] = inpParts[ 1 ].slice( 0, -1 );
                        }
                }
 
-               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( '' )
                };
        };
 
                                // maxLength is a strange property. Removing or setting the property to
                                // undefined directly doesn't work. Instead, it can only be unset internally
                                // by the browser when removing the associated attribute (Firefox/Chrome).
-                               // http://code.google.com/p/chromium/issues/detail?id=136004
+                               // https://bugs.chromium.org/p/chromium/issues/detail?id=136004
                                $el.removeAttr( 'maxlength' );
 
                        } else {
                        // changed while text is being entered and keyup/change will not be fired yet
                        // (such as holding down a single key, fires keydown, and after each keydown,
                        // we can trim the previous one).
-                       // See http://www.w3.org/TR/DOM-Level-3-Events/#events-keyboard-event-order for
+                       // See https://www.w3.org/TR/DOM-Level-3-Events/#events-keyboard-event-order for
                        // the order and characteristics of the key events.
                        $el.on( eventKeys, function () {
                                var res = $.trimByteLength(
                                // 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
                                // trimByteLength to compare the new value to an empty string instead of the