r57182 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57181‎ | r57182 | r57183 >
Date:01:30, 1 October 2009
Author:simetrical
Status:ok
Tags:
Comment:
Escape '<' in attribute values for well-formed XML

This fixes r56407, which fixed bug 20655. Now $wgWellFormedXml is used,
not $wgHtml5. The previous code was outputting malformed XML if
$wgHtml5 and $wgWellFormedXml were both true.

I wish we had unit tests for this. :(
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Html.php
@@ -353,17 +353,23 @@
354354 # and we don't need <> escaped here, we may as well not call
355355 # htmlspecialchars(). FIXME: verify that we actually need to
356356 # escape \n\r\t here, and explain why, exactly.
357 - if ( $wgHtml5 ) {
358 - $ret .= " $key=$quote" . strtr( $value, array(
359 - '&' => '&amp;',
360 - '"' => '&quot;',
361 - "\n" => '&#10;',
362 - "\r" => '&#13;',
363 - "\t" => '&#9;'
364 - ) ) . $quote;
365 - } else {
366 - $ret .= " $key=$quote" . Sanitizer::encodeAttribute( $value ) . $quote;
 357+ #
 358+ # We could call Sanitizer::encodeAttribute() for this, but we
 359+ # don't because we're stubborn and like our marginal savings on
 360+ # byte size from not having to encode unnecessary quotes.
 361+ $map = array(
 362+ '&' => '&amp;',
 363+ '"' => '&quot;',
 364+ "\n" => '&#10;',
 365+ "\r" => '&#13;',
 366+ "\t" => '&#9;'
 367+ );
 368+ if ( $wgWellFormedXml ) {
 369+ # '<' must be escaped in attributes for XML for some
 370+ # reason, per spec: http://www.w3.org/TR/xml/#NT-AttValue
 371+ $map['<'] = '&lt;';
367372 }
 373+ $ret .= " $key=$quote" . strtr( $value, $map ) . $quote;
368374 }
369375 }
370376 return $ret;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r56407(bug 20655) If $wgHtml5 is false, run attribute values through Sanitizer::enc...mrzman05:29, 16 September 2009

Status & tagging log