r8757 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r8756‎ | r8757 | r8758 >
Date:08:04, 30 April 2005
Author:vibber
Status:old
Tags:
Comment:
* (bug 1877) JavaScript error in page editing in some localizations
Add wfEscapeJsString() function to do proper escaping of JavaScript string literals.
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -674,6 +674,30 @@
675675 }
676676
677677 /**
 678+ * Returns an escaped string suitable for inclusion in a string literal
 679+ * for JavaScript source code.
 680+ * Illegal control characters are assumed not to be present.
 681+ *
 682+ * @param string $string
 683+ * @return string
 684+ */
 685+function wfEscapeJsString( $string ) {
 686+ // See ECMA 262 section 7.8.4 for string literal format
 687+ $pairs = array(
 688+ "\\" => "\\\\",
 689+ "\"" => "\\\"",
 690+ "\'" => "\\\'",
 691+ "\n" => "\\n",
 692+ "\r" => "\\r",
 693+
 694+ # To avoid closing the element or CDATA section
 695+ "<" => "\\x3c",
 696+ ">" => "\\x3e",
 697+ );
 698+ return strtr( $string, $pairs );
 699+}
 700+
 701+/**
678702 * @todo document
679703 * @return float
680704 */
Index: trunk/phase3/includes/Linker.php
@@ -719,13 +719,13 @@
720720 /** @todo document */
721721 function tocList($toc) {
722722 return "<table id='toc' class='toc'><tr><td>"
723 - . "<div id='toctitle'><h2>" . wfMsg('toc') . "</h2></div>\n"
 723+ . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
724724 . $toc
725725 . "</ul>\n</td></tr></table>\n"
726726 . '<script type="text/javascript">'
727727 . ' if (window.showTocToggle) {'
728 - . ' var tocShowText = "' . addslashes( wfMsg('showtoc') ) . '";'
729 - . ' var tocHideText = "' . addslashes( wfMsg('hidetoc') ) . '"; '
 728+ . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
 729+ . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
730730 . ' showTocToggle();'
731731 . ' } '
732732 . "</script>\n";
Index: trunk/phase3/includes/EditPage.php
@@ -1028,7 +1028,7 @@
10291029 $image=$wgStylePath.'/common/images/'.$tool['image'];
10301030 $open=$tool['open'];
10311031 $close=$tool['close'];
1032 - $sample = addslashes( $tool['sample'] );
 1032+ $sample = wfEscapeJsString( $tool['sample'] );
10331033
10341034 // Note that we use the tip both for the ALT tag and the TITLE tag of the image.
10351035 // Older browsers show a "speedtip" type message only for ALT.
@@ -1041,7 +1041,8 @@
10421042 $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
10431043 }
10441044
1045 - $toolbar.="addInfobox('" . addslashes( wfMsg( "infobox" ) ) . "','" . addslashes(wfMsg("infobox_alert")) . "');\n";
 1045+ $toolbar.="addInfobox('" . wfEscapeJsString( wfMsg( "infobox" ) ) .
 1046+ "','" . wfEscapeJsString( wfMsg( "infobox_alert" ) ) . "');\n";
10461047 $toolbar.="document.writeln(\"</div>\");\n";
10471048
10481049 $toolbar.="/*]]>*/\n</script>";

Status & tagging log