Parser: Avoid producing <span></span> in the TOC
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 15 Apr 2015 17:44:28 +0000 (13:44 -0400)
committerUmherirrender <umherirrender_de.wp@web.de>
Wed, 8 Jul 2015 17:11:21 +0000 (17:11 +0000)
If someone renames a section but wants old targeted links to still work,
<span id="old-anchor"></span> is the usual solution. And sometimes
people put it inside the section header markup, like

 == <span id="old-anchor"></span>New name ==

since putting it before makes it be considered part of the previous section
while putting it after causes the browser to scroll the section header
off the screen.

But this has the unfortunate side effect that the TOC text for that
section will be "<span></span>New name". We should strip that useless
empty span.

Bug: T96153
Change-Id: I47a33ceb79d48f6d0c38fa3b3814a378feb5e31e

includes/parser/Parser.php
tests/parser/parserTests.txt

index 1603fc6..5506684 100644 (file)
@@ -4554,6 +4554,12 @@ class Parser {
                                array( '', '<$1>' ),
                                $safeHeadline
                        );
+
+                       # Strip '<span></span>', which is the result from the above if
+                       # <span id="foo"></span> is used to produce an additional anchor
+                       # for a section.
+                       $tocline = str_replace( '<span></span>', '', $tocline );
+
                        $tocline = trim( $tocline );
 
                        # For the anchor, strip out HTML-y stuff period
index 18d9aa8..1670fe4 100644 (file)
@@ -13744,6 +13744,22 @@ TOC regression (T11764)
 
 !! end
 
+!! test
+TOC for heading containing <span id="..."></span> (T96153)
+!! wikitext
+__FORCETOC__
+==<span id="old-anchor"></span>New title==
+!! html
+<div id="toc" class="toc"><div id="toctitle"><h2>Contents</h2></div>
+<ul>
+<li class="toclevel-1 tocsection-1"><a href="#New_title"><span class="tocnumber">1</span> <span class="toctext">New title</span></a></li>
+</ul>
+</div>
+
+<h2><span class="mw-headline" id="New_title"><span id="old-anchor"></span>New title</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/index.php?title=Parser_test&amp;action=edit&amp;section=1" title="Edit section: New title">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
+
+!! end
+
 !! test
 TOC with wgMaxTocLevel=3 (bug 6204)
 !! options