From e3e24db38224a6dcc40da04c6859df3a740c5a34 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 20 Jun 2010 23:43:39 +0000 Subject: [PATCH] (bug 17857) Make {{anchorencode}} work in a manner more similar to the way that section links are created by the parser. Also useful for (bug 18431). --- RELEASE-NOTES | 1 + includes/parser/CoreParserFunctions.php | 6 +--- includes/parser/Parser.php | 15 ++-------- maintenance/parserTests.txt | 38 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 71d1fa622e..1310543a54 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index 92897428db..58c57714a8 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -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 ) { diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index fc9303c548..7b28c14519 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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' ); } /** diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index f4dc7e1a30..e3612b179f 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -6920,6 +6920,44 @@ anchorencode

!! end +!! test +anchorencode trims spaces +!! input +{{anchorencode: __pretty__please__}} +!! result +

pretty_please +

+!! end + +!! test +anchorencode deals with links +!! input +{{anchorencode: [[hello|world]] [[hi]]}} +!! result +

world_hi +

+!! end + +!! test +anchorencode deals with templates +!! input +{{anchorencode: {{Foo}} }} +!! result +

FOO +

+!! end + +!! test +anchorencode encodes like the TOC generator: (bug 18431) +!! input +=== _ +:.3A%3A&&]] === +{{anchorencode: _ +:.3A%3A&&]] }} +__NOEDITSECTION__ +!! result +

_ +:.3A%3A&&]]

+

.2B:.3A.253A.26.26.5D.5D +

+!! end !! test Bug 8293: Use of center tag ruins paragraph formatting -- 2.20.1