From 58ecbfc318ef14ba41881c5ebb46c54ca2fbbe29 Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Wed, 26 May 2010 09:10:56 +0000 Subject: [PATCH] (bug 22967) Use javascript to make the maxlength of the edit summary field work properly for multibyte characters. Previously the edit summary was set to 200 characters, however with multibyte characters, you could end up with more than 255 bytes which is the db field limit. This adds javascript to enforce the limit more reliably. --- RELEASE-NOTES | 1 + includes/EditPage.php | 1 + skins/common/edit.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index cf4a934140..b234b56f74 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -172,6 +172,7 @@ in a negative namespace (which is invalid). * (bug 1347) Render \phi in math using images, in order to create consistent and correct render results. * (bug 16573) Render \epsilon in math using images, in order to create consistent and correct render results. * (bug 22541) Support image redirects when using ForeignAPIRepo +* (bug 22967) Make edit summary length cut-off behave correctly for multibyte characters. === API changes in 1.17 === * (bug 22738) Allow filtering by action type on query=logevent diff --git a/includes/EditPage.php b/includes/EditPage.php index f6b6cb5bdb..ff57eb795d 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1486,6 +1486,7 @@ HTML */ function getSummaryInput($summary = "", $labelText = null, $inputAttrs = null, $spanLabelAttrs = null) { global $wgUser; + //Note: the maxlength is overriden in JS to 250 and to make it use UTF-8 bytes, not characters. $inputAttrs = ( is_array($inputAttrs) ? $inputAttrs : array() ) + array( 'id' => 'wpSummary', 'maxlength' => '200', diff --git a/skins/common/edit.js b/skins/common/edit.js index 423205f8f7..17d29e8c1f 100644 --- a/skins/common/edit.js +++ b/skins/common/edit.js @@ -229,3 +229,37 @@ hookEvent( 'load', function() { editForm } ); +//make sure edit summary does not exceed byte limit +addOnloadHook(function () { + var summary = document.getElementById('wpSummary'); + summary.maxLength = 250; //L must be capitalized in length. + + checkSummary = function (e) { + if (!e) e = window.event; //IE + + //first check to see if this is actually a character key + // being pressed. + //Based on key-event info from http://unixpapa.com/js/key.html + //note === sign, if undefined, still could be a real key + + if (e.which === 0 || e.charCode === 0 || e.ctrlKey || e.altKey || e.metaKey) { + return true; //a special key (backspace, etc) so don't interefere. + } + + //This basically figures out how many bytes a utf-16 string (which is what js sees) will take in utf-8 + //by replacing a 2 byte character with 2 *'s, etc, and counting that. + //Note, surogate (\uD800-\uDFFF) characters are counted as 2 bytes, since theres two of them + //and the actual character takes 4 bytes in utf-8 (2*2=4). might not work perfectly in edge cases such as + //such as illegal sequences, but that should never happen. + + len = summary.value.replace(/[\u0080-\u07FF\uD800-\uDFFF]/g, '**').replace(/[\u0800-\uD7FF\uE000-\uFFFF]/g, '***').length; + if (len > 250) { + if (e.preventDefault) e.preventDefault(); + e.returnValue = false; //IE + return false; + } + } + + addHandler(summary, 'keypress', checkSummary); +}); + -- 2.20.1