Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParsoidMapLink
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 1
 sourceToDom
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3namespace Kartographer\Tag;
4
5use DOMException;
6use FormatJson;
7use Kartographer\CoordFormatter;
8use Kartographer\ParsoidUtils;
9use Wikimedia\Parsoid\DOM\DocumentFragment;
10use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
11
12/**
13 * @license MIT
14 */
15class ParsoidMapLink extends ParsoidTagHandler {
16
17    public const TAG = 'maplink';
18
19    /**
20     * @param ParsoidExtensionAPI $extApi
21     * @param string $src
22     * @param array $extArgs
23     * @return DocumentFragment
24     * @throws DOMException
25     */
26    public function sourceToDom( ParsoidExtensionAPI $extApi, string $src, array $extArgs ) {
27        $extApi->getMetadata()->addModules( [ 'ext.kartographer.link' ] );
28
29        [ $status, $args, $geometries, $srcOffsets ] = $this->parseTag( $extApi, $src, $extArgs );
30        if ( !$status->isGood() ) {
31            return $this->reportErrors( $extApi, self::TAG, $status );
32        }
33
34        $gen = new MapLinkAttributeGenerator( $args );
35        $attrs = $gen->prepareAttrs();
36
37        $text = $args->getTextWithFallback();
38        if ( $text === null ) {
39            $formatter = new CoordFormatter( $args->lat, $args->lon );
40            // TODO: for now, we're using the old wfMessage method with English hardcoded. This should not
41            // stay that way.
42            // When l10n is added to Parsoid, replace this line with
43            // ( new CoordFormatter( $this->args->lat, $this->args->lon ) )->formatParsoidSpan( $extApi, null );
44            $text = $formatter->format( 'en' );
45        } elseif ( $text !== '' && !ctype_alnum( $text ) ) {
46            // Don't parse trivial alphanumeric-only strings, e.g. counters like "A" or "99".
47            $text = $extApi->wikitextToDOM( $text, [
48                'parseOpts' => [
49                    'extTag' => 'maplink',
50                    'context' => 'inline',
51                ],
52                'srcOffsets' => $srcOffsets['text']->value ?? null
53            ], false );
54        }
55        $doc = $extApi->getTopLevelDoc();
56        if ( is_string( $text ) ) {
57            $text = ( trim( $text ) !== '' ) ? $doc->createTextNode( $text ) : null;
58        }
59
60        $dom = $doc->createDocumentFragment();
61        $a = $doc->createElement( 'a' );
62        if ( $args->groupId === null && $geometries ) {
63            $groupId = '_' . sha1( FormatJson::encode( $geometries, false,
64                    FormatJson::ALL_OK ) );
65            $args->groupId = $groupId;
66            $args->showGroups[] = $groupId;
67        }
68
69        if ( $args->showGroups ) {
70            $attrs['data-overlays'] = FormatJson::encode( $args->showGroups, false,
71                FormatJson::ALL_OK );
72            $dataKart = [
73                'groupId' => $args->groupId,
74                'showGroups' => $args->showGroups,
75                'geometries' => $geometries
76            ];
77            $a->setAttribute( 'data-kart', json_encode( $dataKart ) );
78        }
79
80        ParsoidUtils::addAttributesToNode( $attrs, $a );
81        if ( $text ) {
82            $a->appendChild( $text );
83        }
84
85        $dom->appendChild( $a );
86        return $dom;
87    }
88
89}