r76102 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76101‎ | r76102 | r76103 >
Date:15:07, 5 November 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Cleanup javascript code a little
Modified paths:
  • /trunk/extensions/Translate/TranslatePage.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/utils/CompatibilityCode.php (added) (history)
  • /trunk/extensions/Translate/utils/TranslationEditPage.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslatePage.php
@@ -477,18 +477,19 @@
478478 $desc = $this->getGroupDescription( $block );
479479 $hasSubblocks = is_array( $blocks ) && count( $blocks );
480480
481 - $subid = "mw-subgroup-$id";
 481+ $subid = Sanitizer::escapeId( "mw-subgroup-$id" );
482482
483483 if ( $hasSubblocks ) {
484484 $msg = wfMsgExt( 'translate-showsub', 'parsemag', $wgLang->formatNum( count( $blocks ) ) );
485 - $desc .= Html::element( 'a', array( 'onclick' => "jQuery('#$subid').toggle()", 'class' => 'mw-sp-showmore' ), $msg );
 485+ $target = TranslationHelpers::jQueryPathId( $subid );
 486+ $desc .= Html::element( 'a', array( 'onclick' => "jQuery($target).toggle()", 'class' => 'mw-sp-showmore' ), $msg );
486487 }
487488
488489 $out = "\n<tr><td>$label</td>\n<td>$desc</td></tr>\n";
489490 if ( $hasSubblocks ) {
490491 $out .= "<tr><td></td><td>\n";
491492 $tableParams = array(
492 - 'id' => "mw-subgroup-$id",
 493+ 'id' => $subid,
493494 'style' => 'display:none;',
494495 'class' => "mw-sp-translate-subgroup depth-$level",
495496 );
Index: trunk/extensions/Translate/_autoload.php
@@ -110,6 +110,8 @@
111111 $wgAutoloadClasses['TranslationMemoryUpdater'] = $dir . 'utils/TranslationMemoryUpdater.php';
112112
113113 $wgAutoloadClasses['TranslateYaml'] = $dir . 'utils/TranslateYaml.php';
 114+
 115+$wgAutoloadClasses['TranslateBC'] = $dir . 'utils/CompatibilityCode.php';
114116 /**@}*/
115117
116118 /**
Index: trunk/extensions/Translate/utils/TranslationEditPage.php
@@ -169,21 +169,14 @@
170170 public static function jsEdit( Title $title, $group = "" ) {
171171 global $wgUser;
172172
173 - if ( !$wgUser->isAllowed( 'translate' ) ) {
 173+ if ( !$wgUser->isAllowed( 'translate' ) || !$wgUser->getOption( 'translate-jsedit' ) ) {
174174 return array();
175175 }
176176
177 - if ( !$wgUser->getOption( 'translate-jsedit' ) ) {
178 - return array();
179 - }
180 -
181 - $dbKey = $title->getPrefixedDbKey();
182 - $jsTitle = Xml::escapeJsString( $dbKey );
183 - $jsGroup = Xml::escapeJsString( $group );
184 -
185177 return array(
186 - 'onclick' => "return trlOpenJsEdit( \"$jsTitle\", \"$jsGroup\" );",
187 - 'title' => wfMsg( 'translate-edit-title', $dbKey )
 178+ 'onclick' => TranslateBC::encodeJsCall(
 179+ 'return trlOpenJsEdit', array( $title->getPrefixedDbKey(), $group ) ),
 180+ 'title' => wfMsg( 'translate-edit-title', $title->getPrefixedText() )
188181 );
189182 }
190183 }
Index: trunk/extensions/Translate/utils/CompatibilityCode.php
@@ -0,0 +1,44 @@
 2+<?php
 3+/**
 4+ * Code for facilitiating backwards compatibility for older %MediaWikis.
 5+ *
 6+ * @file
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2010, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+/**
 13+ * General BC code.
 14+ */
 15+class TranslateBC {
 16+
 17+ /**
 18+ * Create a call to a JavaScript function. The supplied arguments will be
 19+ * encoded using Xml::encodeJsVar().
 20+ *
 21+ * @param $name The name of the function to call, or a JavaScript expression
 22+ * which evaluates to a function object which is called.
 23+ * @param $args Array of arguments to pass to the function.
 24+ * @since 1.17
 25+ */
 26+ public static function encodeJsCall( $name, $args ) {
 27+ if ( method_exists( 'Xml', 'encodeJsCall' ) ) {
 28+ return Xml::encodeJsCall( $name, $args );
 29+ }
 30+
 31+ $s = "$name(";
 32+ $first = true;
 33+ foreach ( $args as $arg ) {
 34+ if ( $first ) {
 35+ $first = false;
 36+ } else {
 37+ $s .= ', ';
 38+ }
 39+ $s .= Xml::encodeJsVar( $arg );
 40+ }
 41+ $s .= ");\n";
 42+ return $s;
 43+ }
 44+
 45+}
Property changes on: trunk/extensions/Translate/utils/CompatibilityCode.php
___________________________________________________________________
Added: svn:eol-style
146 + native
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -964,25 +964,26 @@
965965 'page' => $this->title->getPrefixedDbKey(),
966966 'loadgroup' => $this->group->getId(),
967967 ) );
968 - $url = Xml::escapeJsString( $url );
 968+ $url = Xml::encodeJsVar( $url );
969969
970 - $dialogID = $this->dialogID();
971 - $id = Sanitizer::escapeId( "tm-lazysug-$dialogID" );
 970+ $id = Sanitizer::escapeId( 'tm-lazysug-' . $this->dialogID() );
 971+ $target = self::jQueryPathId( $id );
972972
973 - $script = Html::inlineScript( "jQuery('#$id').load( \"$url\" )" );
 973+ $script = Html::inlineScript( "jQuery($target).load($url)" );
974974 $spinner = Html::element( 'div', array( 'class' => 'mw-ajax-loader' ) );
975975 return Html::rawElement( 'div', array( 'id' => $id ), $script . $spinner );
976976 }
977977
978978 public function dialogID() {
979 - return sha1( $this->title->getPrefixedDbKey() );
 979+ $hash = sha1( $this->title->getPrefixedDbKey() );
 980+ return substr( $hash, 0, 4 );
980981 }
981982
982983 public function adder( $source ) {
983 - $target = Xml::escapeJsString( $this->getTextareaId() );
984 - $source = Xml::escapeJsString( $source );
 984+ $target = self::jQueryPathId( $this->getTextareaId() );
 985+ $source = self::jQueryPathId( $source );
985986 $params = array(
986 - 'onclick' => "jQuery('#$target').val(jQuery('#$source').text()).focus(); return false;",
 987+ 'onclick' => "jQuery($target).val($source).text()).focus(); return false;",
987988 'href' => '#',
988989 'title' => wfMsg( 'translate-use-suggestion' )
989990 );
@@ -1027,6 +1028,10 @@
10281029 return $wgUser->getSkin()->link( $target, $text, $jsEdit, $params );
10291030 }
10301031
 1032+ public static function jQueryPathId( $id ) {
 1033+ return Xml::encodeJsVar( "#$id" );
 1034+ }
 1035+
10311036 /**
10321037 * How many failures during failure period need to happen to consider
10331038 * the service being temporarily off-line. */

Follow-up revisions

RevisionCommit summaryAuthorDate
r76183Fix js error introduced in r76102nikerabbit08:57, 6 November 2010

Status & tagging log