section links are created by the parser. Also useful for (bug 18431).
* (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
}
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 ) {
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' );
}
/**
</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&&]] ===
+{{anchorencode: _ +:.3A%3A&&]] }}
+__NOEDITSECTION__
+!! result
+<h3> <span class="mw-headline" id=".2B:.3A.253A.26.26.5D.5D"> _ +:.3A%3A&&]] </span></h3>
+<p>.2B:.3A.253A.26.26.5D.5D
+</p>
+!! end
!! test
Bug 8293: Use of center tag ruins paragraph formatting