Handle raw <h#> when calculating $rawtoc
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 9 Oct 2013 15:03:40 +0000 (11:03 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 9 Oct 2013 17:48:03 +0000 (13:48 -0400)
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
("<h2>foo</h2>") 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

includes/parser/Parser.php
tests/phpunit/includes/parser/ParserMethodsTest.php

index eac2202..0603a9b 100644 (file)
@@ -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,
                        );
 
index cacbb85..3cdbf15 100644 (file)
@@ -44,5 +44,44 @@ class ParserMethodsTest extends MediaWikiLangTestCase {
                        'text' => '<pre style="margin-left: 1.6em">foo</pre>',
                ), $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<h2>bar</h2>\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 <h2> is used' );
+       }
        // TODO: Add tests for cleanSig() / cleanSigInSig(), getSection(), replaceSection(), getPreloadText()
 }