Page MenuHomePhabricator

setting $wgMaxTocLevel can cause invalid xhtml output (extra </ul> and <li> tags)
Closed, ResolvedPublic


Author: ramana.kumar

Parser.php calls $sk->tocIndent() and $sk->tocUnindent( $prevtoclevel -
$toclevel ) even if the heading in question is above the $wgMaxTocLevel.

The default for $wgMaxTocLevel is 999, so usually this problem doesn't surface.
But if it is set to something low like 2 or 3, and there are headings on the
page which are greater than this level, the resulting page has an invalid list
in the table of contents. (There are extra <ul> </ul> <li> and </li> tags and
the nesting makes no sense. It doesn't validate as valid xhtml 1.0 transitional.)

I believe a simple fix can be accomplished by adding "if( (
!isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) )" before "$toc .=
$sk->tocIndent();" and "if( ( !isset($wgMaxTocLevel) ||
$prevtoclevel<$wgMaxTocLevel ) )" before "$toc .= $sk->tocUnindent(
$prevtoclevel - $toclevel );" in Parser.php. This ensures that the tocIndent and
tocUnindent functions are only called if the heading is actually going to appear
in the toc. (these lines only appear once in Parser.php so they should be easy
to find and easy to fix.)

Version: 1.5.x
Severity: minor
OS: Linux
Platform: PC



Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 9:03 PM
bzimport added a project: MediaWiki-Parser.
bzimport set Reference to bz4834.
bzimport added a subscriber: Unknown Object (MLST).

ramana.kumar wrote:

I missed a further complication.
The unindent bit should actually end up like this:

if ( ( !isset($wgMaxTocLevel) || $prevtoclevel<$wgMaxTocLevel ) ) {
$toc .= $sk->tocUnindent( $prevtoclevel - $toclevel );
elseif ( ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
$toc .= $sk->tocUnindent( 0 );

otherwise list elements for headings which are above unshown headings will not
be closed.

Why does this setting even exist? It seems a bit silly.

ramana.kumar wrote:

It's being put to good use on my wiki. No wait - I'll explain further: I wanted
to create sections without headings (like this: "== ==") so that I could put
"[edit]" links (and horizontal rules etc.) down the page without having to
explicitly give names for the sections. But then I didn't want these sections to
show up in the TOC. That's why I'm using $wgMaxTocLevel. If there were a
different way to add an [edit] link without adding a section, I wouldn't have to
use $wgMaxTocLevel. I might have a look at the code some time (if anybody's
interested) to see how easy that would be to accomplish... (I'm not motivated
now though since my sections with spaces for titles work fine...)

Fixed in trunk@13867
Fixed in REL1_6@13868

Would be in MediaWiki 1.6.4