Merge "Parser: Add mw-selflink-fragment class to links to hash fragments in the same page"

This commit is contained in:
jenkins-bot 2023-01-11 21:49:14 +00:00 committed by Gerrit Code Review
commit 369a7255c9
3 changed files with 17 additions and 6 deletions

View file

@ -181,12 +181,23 @@ class Linker {
* @param string $query
* @param string $trail
* @param string $prefix
* @param string $hash hash fragment since 1.40.
*
* @return string
*/
public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '' ) {
public static function makeSelfLinkObj( $nt, $html = '', $query = '', $trail = '', $prefix = '', $hash = '' ) {
$nt = Title::newFromLinkTarget( $nt );
$ret = "<a class=\"mw-selflink selflink\">{$prefix}{$html}</a>{$trail}";
$attrs = [
'class' => 'mw-selflink',
];
if ( $hash ) {
$attrs['href'] = '#' . $hash;
$attrs['class'] = 'mw-selflink-fragment';
} else {
// For backwards compatibility with gadgets we add selflink as well.
$attrs['class'] = 'mw-selflink selflink';
}
$ret = Html::rawElement( 'a', $attrs, $prefix . $html ) . $trail;
if ( !Hooks::runner()->onSelfLinkBegin( $nt, $html, $trail, $prefix, $ret ) ) {
return $ret;
}
@ -195,7 +206,7 @@ class Linker {
$html = htmlspecialchars( $nt->getPrefixedText() );
}
[ $inside, $trail ] = self::splitTrail( $trail );
return "<a class=\"mw-selflink selflink\">{$prefix}{$html}{$inside}</a>{$trail}";
return Html::rawElement( 'a', $attrs, $prefix . $html . $inside ) . $trail;
}
/**

View file

@ -2731,8 +2731,8 @@ class Parser {
# Self-link checking. For some languages, variants of the title are checked in
# LinkHolderArray::doVariants() to allow batching the existence checks necessary
# for linking to a different variant.
if ( $ns !== NS_SPECIAL && $nt->equals( $this->getTitle() ) && !$nt->hasFragment() ) {
$s .= $prefix . Linker::makeSelfLinkObj( $nt, $text, '', $trail );
if ( $ns !== NS_SPECIAL && $nt->equals( $this->getTitle() ) ) {
$s .= $prefix . Linker::makeSelfLinkObj( $nt, $text, '', $trail, '', $nt->getFragment() );
continue;
}

View file

@ -2582,7 +2582,7 @@ title=[[Main Page]]
!! wikitext
[[Main Page#section]]
!! html
<p><a href="/wiki/Main_Page#section" title="Main Page">Main Page#section</a>
<p><a class="mw-selflink-fragment" href="#section">Main Page#section</a>
</p>
!! end