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\Core\DOMCompat;
7use Wikimedia\Parsoid\DOM\Element;
8use Wikimedia\Parsoid\DOM\Node;
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                $about = $state->env->newAnnotationId();
27                if ( !array_key_exists( $t, $abouts ) ) {
28                    $abouts[$t] = [];
29                }
30                array_push( $abouts[$t], $about );
31            } else {
32                if ( array_key_exists( $t, $abouts ) ) {
33                    $about = array_pop( $abouts[$t] );
34                }
35            }
36            if ( $about === null ) {
37                // this doesn't have a start tag, so we don't handle it when creating
38                // annotation ranges, and we replace it with a string
39                $textAnn = $node->ownerDocument->createTextNode( '</' . $t . '>' );
40                $parentNode = $node->parentNode;
41                $parentNode->insertBefore( $textAnn, $node );
42                DOMCompat::remove( $node );
43                return $textAnn;
44            }
45            DOMDataUtils::getDataMw( $node )->rangeId = $about;
46        }
47        return true;
48    }
49}