From 0c0dad3cd36c5a627b168183330f5578b039ba11 Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Wed, 19 Jan 2011 03:59:11 +0000 Subject: [PATCH] (follow-up r66913) Per CR, make the editsummary length checker use jQuery/RL fanciness. The js was tested in firefox 3.0.6, IE6, some oldish version of Opera, Konqourer, and Chrome. (Of course in IE6, the rest of mediawiki fell on its face, but the js added here worked). --- includes/EditPage.php | 2 +- resources/Resources.php | 3 ++ .../mediawiki.action/mediawiki.action.edit.js | 30 +++++++++++++++ skins/common/edit.js | 38 ------------------- 4 files changed, 34 insertions(+), 39 deletions(-) create mode 100644 resources/mediawiki.action/mediawiki.action.edit.js diff --git a/includes/EditPage.php b/includes/EditPage.php index 601ffb32ff..3f7ef96571 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -335,7 +335,7 @@ class EditPage { $this->preview = true; } - $wgOut->addModules( 'mediawiki.legacy.edit' ); + $wgOut->addModules( array( 'mediawiki.legacy.edit', 'mediawiki.action.edit' ) ); if ( $wgUser->getOption( 'uselivepreview', false ) ) { $wgOut->addModules( 'mediawiki.legacy.preview' ); diff --git a/resources/Resources.php b/resources/Resources.php index 79e55e602e..652b5bf120 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -353,6 +353,9 @@ return array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.history.js', 'dependencies' => 'mediawiki.legacy.history', ), + 'mediawiki.action.edit' => array( + 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.js', + ), 'mediawiki.action.view.rightClickEdit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js', ), diff --git a/resources/mediawiki.action/mediawiki.action.edit.js b/resources/mediawiki.action/mediawiki.action.edit.js new file mode 100644 index 0000000000..ebfc942ec5 --- /dev/null +++ b/resources/mediawiki.action/mediawiki.action.edit.js @@ -0,0 +1,30 @@ +/* Note, there is still stuff in skins/common/edit.js that + * has not been jQuery-ized. + */ + +//make sure edit summary does not exceed byte limit +$( '#wpSummary' ).attr( 'maxLength', 250 ).keypress( function (e) { + + // 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 + // JQuery should also normalize e.which to be consistent cross-browser, + // however the same check is still needed regardless of jQuery. + + if (e.which === 0 || e.charCode === 0 || e.ctrlKey || e.altKey || e.metaKey) { + return true; //a special key (backspace, etc) so don't interfere. + } + + // 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, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's 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 illegal sequences, but that should never happen. + + len = this.value.replace(/[\u0080-\u07FF\uD800-\uDFFF]/g, '**').replace(/[\u0800-\uD7FF\uE000-\uFFFF]/g, '***').length; + //247 as this doesn't count character about to be inserted. + if (len > 247) { + e.preventDefault(); + } +}); + diff --git a/skins/common/edit.js b/skins/common/edit.js index 287fbe3dbd..f986c854b0 100644 --- a/skins/common/edit.js +++ b/skins/common/edit.js @@ -229,41 +229,3 @@ hookEvent( 'load', function() { editForm } ); -//make sure edit summary does not exceed byte limit -addOnloadHook(function () { - var summary = document.getElementById( 'wpSummary' ); - if ( !summary ) { - return; - } - 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; - //247 as this doesn't count character about to be inserted. - if (len > 247) { - if (e.preventDefault) e.preventDefault(); - e.returnValue = false; //IE - return false; - } - }; - - addHandler(summary, 'keypress', checkSummary); -}); - -- 2.20.1