Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddAnnotationIds
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 handler
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wt2Html\DOM\Handlers;
5
6use Wikimedia\Parsoid\DOM\Element;
7use Wikimedia\Parsoid\DOM\Node;
8use Wikimedia\Parsoid\Utils\DOMCompat;
9use Wikimedia\Parsoid\Utils\DOMDataUtils;
10use Wikimedia\Parsoid\Utils\DTState;
11use Wikimedia\Parsoid\Utils\WTUtils;
12
13class AddAnnotationIds {
14    /**
15     * @return bool|Node
16     */
17    public static function handler( Element $node, DTState $state ) {
18        $abouts = &$state->abouts;
19        $abouts ??= [];
20        $isStart = false;
21        // isStart gets modified (not read) by extractAnnotationType
22        $t = WTUtils::extractAnnotationType( $node, $isStart );
23        if ( $t !== null ) {
24            $about = null;
25            if ( $isStart ) {
26                // The 'mwa' prefix is specific to annotations;
27                // if other DOM ranges are to use this mechanism, another prefix
28                // should be used.
29                $about = $state->env->newAnnotationId();
30                if ( !array_key_exists( $t, $abouts ) ) {
31                    $abouts[$t] = [];
32                }
33                array_push( $abouts[$t], $about );
34            } else {
35                if ( array_key_exists( $t, $abouts ) ) {
36                    $about = array_pop( $abouts[$t] );
37                }
38            }
39            if ( $about === null ) {
40                // this doesn't have a start tag, so we don't handle it when creating
41                // annotation ranges, and we replace it with a string
42                $textAnn = $node->ownerDocument->createTextNode( '</' . $t . '>' );
43                $parentNode = $node->parentNode;
44                $parentNode->insertBefore( $textAnn, $node );
45                DOMCompat::remove( $node );
46                return $textAnn;
47            }
48            DOMDataUtils::getDataMw( $node )->rangeId = $about;
49        }
50        return true;
51    }
52}