| 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\DOM\Node; |
| 8 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 9 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 10 |
use Wikimedia\Parsoid\Utils\DTState; |
| 11 |
use Wikimedia\Parsoid\Utils\WTUtils; |
| 12 |
|
| 13 |
class 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 |
} |