jquery.byteLimit: Partial rewrite to fix logic errors
authorTimo Tijhof <ttijhof@wikimedia.org>
Sat, 23 Jun 2012 02:16:06 +0000 (04:16 +0200)
committerCatrope <roan.kattouw@gmail.com>
Wed, 4 Jul 2012 06:11:11 +0000 (23:11 -0700)
commit1ceab34587b98abae29bba9027838ed354330025
tree40a39e455ddebc9618f0d1ed1ab52497e6fc174c
parent126437076ca25a5aba3c53112b76e92e48368ca3
jquery.byteLimit: Partial rewrite to fix logic errors

* Previously it was hooking into the keypress event, this meant
  that it had to do all sorts of calculation to figure out what
  the string length would be *after* the to-be-pressed key
  is allowed to be pressed. This sounds trivial, but is quite the
  contrary. First of all are control, alt, shift etc. those were
  covered.

  What wasn't covered and imho can't be realistically covered
  without bugs is: copy/paste, drag/drop, text selection and
  replacing or removing more at once. Or even a combination of the
  above. And then there is custom insert methods such as Narayam,
  where the character will not be literally inserted at all.

  Therefore I've changed it to instead listen to *after* the insert.
  and if necessary undo the last (one or more) characters until the
  byteLimit is satisfied. This works much more stable. Still very
  fast and not noticeable to the user.

  This also fixes the bug where when the limit is reached, you
  select all and type something else, you couldn't because it would
  prevent the keypress since the limit is reached.

* Updated documentation comments.

* Updated unit tests to simulate onkeyup instead of onkeypress.
 - hasLimit option in byteLimitTest() was redundant, using limit
   directly, instead of duplicating it twice.
 - Added more comments and better test names. Tests themselves
   have not changed.
 - Removed redundant tests at the bottom that no longer make sense
   in the new version (and were broken) since the property is
   now always unset instead of just if there is a callback.
   The native limit must not interfere.

Change-Id: I9ace3ab79f488d24ad13baace32118c151c06347
resources/jquery/jquery.byteLimit.js
tests/qunit/suites/resources/jquery/jquery.byteLength.test.js
tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js