Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
AddLinkAttributes | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
9.76 | |
0.00% |
0 / 1 |
handler | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
9.76 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Wt2Html\DOM\Handlers; |
5 | |
6 | use Wikimedia\Parsoid\DOM\Element; |
7 | use Wikimedia\Parsoid\Utils\DOMCompat; |
8 | use Wikimedia\Parsoid\Utils\DOMUtils; |
9 | use Wikimedia\Parsoid\Utils\DTState; |
10 | use Wikimedia\Parsoid\Utils\WTUtils; |
11 | |
12 | class AddLinkAttributes { |
13 | |
14 | /** |
15 | * Adds classes to external links and interwiki links |
16 | */ |
17 | public static function handler( Element $a, DTState $state ): bool { |
18 | if ( DOMUtils::hasRel( $a, "mw:ExtLink" ) ) { |
19 | if ( $a->firstChild ) { |
20 | // The "external free" class is reserved for links which |
21 | // are syntactically unbracketed; see commit |
22 | // 65fcb7a94528ea56d461b3c7b9cb4d4fe4e99211 in core. |
23 | if ( WTUtils::isATagFromURLLinkSyntax( $a ) ) { |
24 | $classInfoText = 'external free'; |
25 | } elseif ( WTUtils::isATagFromMagicLinkSyntax( $a ) ) { |
26 | // PHP uses specific suffixes for RFC/PMID/ISBN (the last of |
27 | // which is an internal link, not an mw:ExtLink), but we'll |
28 | // keep it simple since magic links are deprecated. |
29 | $classInfoText = 'external mw-magiclink'; |
30 | } else { |
31 | $classInfoText = 'external text'; |
32 | } |
33 | } else { |
34 | $classInfoText = 'external autonumber'; |
35 | } |
36 | $a->setAttribute( 'class', $classInfoText ); |
37 | $href = DOMCompat::getAttribute( $a, 'href' ) ?? ''; |
38 | $attribs = $state->env->getExternalLinkAttribs( $href ); |
39 | foreach ( $attribs as $key => $val ) { |
40 | if ( $key === 'rel' ) { |
41 | foreach ( $val as $v ) { |
42 | DOMUtils::addRel( $a, $v ); |
43 | } |
44 | } else { |
45 | $a->setAttribute( $key, $val ); |
46 | } |
47 | } |
48 | } elseif ( DOMUtils::hasRel( $a, 'mw:WikiLink/Interwiki' ) ) { |
49 | DOMCompat::getClassList( $a )->add( 'extiw' ); |
50 | } |
51 | return true; |
52 | } |
53 | } |