(bug 17857) Make {{anchorencode}} work in a manner more similar to the way that
authorConrad Irwin <conrad@users.mediawiki.org>
Sun, 20 Jun 2010 23:43:39 +0000 (23:43 +0000)
committerConrad Irwin <conrad@users.mediawiki.org>
Sun, 20 Jun 2010 23:43:39 +0000 (23:43 +0000)
section links are created by the parser. Also useful for (bug 18431).

RELEASE-NOTES
includes/parser/CoreParserFunctions.php
includes/parser/Parser.php
maintenance/parserTests.txt

index 71d1fa6..1310543 100644 (file)
@@ -86,6 +86,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 23621) New Special:ComparePages to compare (diff) two articles.
 * (bug 4597) Provide support in Special:Contributions to show only "current"
   contributions
+* (bug 17857) {{anchorencode}} acts more like how the parser creates section ids.
 
 === Bug fixes in 1.17 ===
 * (bug 17560) Half-broken deletion moved image files to deletion archive
index 9289742..58c5771 100644 (file)
@@ -582,11 +582,7 @@ class CoreParserFunctions {
        }
 
        static function anchorencode( $parser, $text ) {
-               $a = urlencode( $text );
-               $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
-               # leave colons alone, however
-               $a = str_replace( '.3A', ':', $a );
-               return $a;
+               return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
        }
 
        static function special( $parser, $text ) {
index fc9303c..7b28c14 100644 (file)
@@ -5172,19 +5172,8 @@ class Parser {
        public function guessSectionNameFromWikiText( $text ) {
                # Strip out wikitext links(they break the anchor)
                $text = $this->stripSectionName( $text );
-               $headline = Sanitizer::decodeCharReferences( $text );
-               # strip out HTML
-               $headline = StringUtils::delimiterReplace( '<', '>', '', $headline );
-               $headline = trim( $headline );
-               $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
-               $replacearray = array(
-                       '%3A' => ':',
-                       '%' => '.'
-               );
-               return str_replace(
-                       array_keys( $replacearray ),
-                       array_values( $replacearray ),
-                       $sectionanchor );
+               $text = trim( preg_replace( '/[ _]+/', ' ', $text ) );
+               return '#' . Sanitizer::escapeId( $text, 'noninitial' );
        }
 
        /**
index f4dc7e1..e3612b1 100644 (file)
@@ -6920,6 +6920,44 @@ anchorencode
 </p>
 !! end
 
+!! test
+anchorencode trims spaces
+!! input
+{{anchorencode: __pretty__please__}}
+!! result
+<p>pretty_please
+</p>
+!! end
+
+!! test
+anchorencode deals with links
+!! input
+{{anchorencode: [[hello|world]] [[hi]]}}
+!! result
+<p>world_hi
+</p>
+!! end
+
+!! test
+anchorencode deals with templates
+!! input
+{{anchorencode: {{Foo}} }}
+!! result
+<p>FOO
+</p>
+!! end
+
+!! test
+anchorencode encodes like the TOC generator: (bug 18431)
+!! input
+=== _ +:.3A%3A&&amp;]] ===
+{{anchorencode: _ +:.3A%3A&&amp;]] }}
+__NOEDITSECTION__
+!! result
+<h3> <span class="mw-headline" id=".2B:.3A.253A.26.26.5D.5D"> _ +:.3A%3A&amp;&amp;]] </span></h3>
+<p>.2B:.3A.253A.26.26.5D.5D
+</p>
+!! end
 
 !! test
 Bug 8293: Use of center tag ruins paragraph formatting