jquery.lengthLimit: Fix 'cut'/'paste' event handling
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 16 Apr 2018 18:41:56 +0000 (20:41 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 30 May 2018 19:31:47 +0000 (21:31 +0200)
For the 'cut' and 'paste' events, the input value is only updated after
the event handlers resolve.

Bug: T64319
Change-Id: Ia0b85ce986eb05aceb7096c56a235477970a4d6f

resources/src/jquery/jquery.lengthLimit.js

index 2738d1a..186ad17 100644 (file)
                        // we can trim the previous one).
                        // 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 () {
+
+                       function enforceLimit() {
                                var res = trimFn(
                                        prevSafeVal,
                                        this.value,
                                // trimFn to compare the new value to an empty string instead of the
                                // old value, resulting in trimming always from the end (T42850).
                                prevSafeVal = res.newVal;
+                       }
+
+                       $el.on( eventKeys, function ( e ) {
+                               if ( e.type === 'cut' || e.type === 'paste' ) {
+                                       // For 'cut'/'paste', the input value is only updated after the event handlers resolve.
+                                       setTimeout( enforceLimit.bind( this ) );
+                               } else {
+                                       enforceLimit.call( this );
+                               }
                        } );
                } );
        }