From: Brad Jorsch Date: Wed, 9 Oct 2013 15:03:40 +0000 (-0400) Subject: Handle raw when calculating $rawtoc X-Git-Tag: 1.31.0-rc.0~18548^2 X-Git-Url: http://git.cyclocoop.org/%40spipnet%40?a=commitdiff_plain;h=12bc4f1c5b7cef7d9e7e96cea0957f4336fa2a66;p=lhc%2Fweb%2Fwiklou.git Handle raw when calculating $rawtoc When the parser is constructing $rawtoc, it needs the sectionIndex number to be able to calculate the byteoffset. This number is only available for wikitext headings ("== foo =="), HTML headings ("

foo

") do not have it and the lack makes byteoffset be wrong for all subsequent headings in the page. To fix this, we just omit output of byteoffset in this situation. Bug: 25203 Change-Id: I39e5faa4ac22d915f06125aac36ced11607b94a3 --- diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index eac2202ec1..0603a9bc77 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4447,7 +4447,8 @@ class Parser { # Add the section to the section tree # Find the DOM node for this header - while ( $node && !$isTemplate ) { + $noOffset = ( $isTemplate || $sectionIndex === false ); + while ( $node && !$noOffset ) { if ( $node->getName() === 'h' ) { $bits = $node->splitHeading(); if ( $bits['i'] == $sectionIndex ) { @@ -4465,7 +4466,7 @@ class Parser { 'number' => $numbering, 'index' => ( $isTemplate ? 'T-' : '' ) . $sectionIndex, 'fromtitle' => $titleText, - 'byteoffset' => ( $isTemplate ? null : $byteOffset ), + 'byteoffset' => ( $noOffset ? null : $byteOffset ), 'anchor' => $anchor, ); diff --git a/tests/phpunit/includes/parser/ParserMethodsTest.php b/tests/phpunit/includes/parser/ParserMethodsTest.php index cacbb850df..3cdbf15fc8 100644 --- a/tests/phpunit/includes/parser/ParserMethodsTest.php +++ b/tests/phpunit/includes/parser/ParserMethodsTest.php @@ -44,5 +44,44 @@ class ParserMethodsTest extends MediaWikiLangTestCase { 'text' => '
foo
', ), $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' ); } + + public function testGetSections() { + global $wgParser; + + $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) ); + $out = $wgParser->parse( "==foo==\n

bar

\n==baz==\n", $title, new ParserOptions() ); + $this->assertSame( array( + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'foo', + 'number' => '1', + 'index' => '1', + 'fromtitle' => $title->getPrefixedDBkey(), + 'byteoffset' => 0, + 'anchor' => 'foo', + ), + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'bar', + 'number' => '2', + 'index' => '', + 'fromtitle' => false, + 'byteoffset' => null, + 'anchor' => 'bar', + ), + array( + 'toclevel' => 1, + 'level' => '2', + 'line' => 'baz', + 'number' => '3', + 'index' => '2', + 'fromtitle' => $title->getPrefixedDBkey(), + 'byteoffset' => 21, + 'anchor' => 'baz', + ), + ), $out->getSections(), 'getSections() with proper value when

is used' ); + } // TODO: Add tests for cleanSig() / cleanSigInSig(), getSection(), replaceSection(), getPreloadText() }