Fix "MWException: Unexpected output from Pygments" in highlight fallback

Follows-up 043969f84e.

This caused a fatal MWException when saving/reading pages that contain a
<source> that couldn't be highlighted (e.g. no lang attribute, unknown
lang, or too large).

Specifically when wgWellFormedXml=false, in which case $out isn't
just from Pygments, but actually from our own Html::element.

Change-Id: Ib299a274d28021b2c7bba52d763dd1e17c1f09ec
This commit is contained in:
Timo Tijhof 2015-07-07 22:06:08 +01:00
parent 509a23af6b
commit 206f0fd585
2 changed files with 26 additions and 1 deletions

View file

@ -158,8 +158,9 @@ class SyntaxHighlight_GeSHi {
// Unwrap Pygments output to provide our own wrapper. We can't just always use the 'nowrap'
// option (pass 'inline'), since it disables other useful things like line highlighting.
// Tolerate absence of quotes for Html::element() and wgWellFormedXml=false.
$m = array();
if ( preg_match( '/^<div class="mw-highlight">(.*)<\/div>$/s', trim( $out ), $m ) ) {
if ( preg_match( '/^<div class="?mw-highlight"?>(.*)<\/div>$/s', trim( $out ), $m ) ) {
$out = trim( $m[1] );
} else {
throw new MWException( 'Unexpected output from Pygments encountered' );

View file

@ -13,6 +13,30 @@ foobar
!! end
!! test
No language specified
!! wikitext
<source>
foo
</source>
!! html
<div class="mw-highlight mw-content-ltr" dir="ltr"><pre>foo</pre></div>
!! end
!! test
No language specified (no wellformed xml)
!! config
wgWellFormedXml=false
!! wikitext
<source>
bar
</source>
!! html
<div class="mw-highlight mw-content-ltr" dir=ltr><pre>bar</pre></div>
!! end
!! test
XSS is escaped
!!input