| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Html2Wt\DOMHandlers; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\DOM\Element; |
| 7 |
use Wikimedia\Parsoid\DOM\Node; |
| 8 |
use Wikimedia\Parsoid\Html2Wt\SerializerState; |
| 9 |
use Wikimedia\Parsoid\Utils\DiffDOMUtils; |
| 10 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 11 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 12 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 13 |
use Wikimedia\Parsoid\Utils\WTUtils; |
| 14 |
|
| 15 |
class DTHandler extends DOMHandler { |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
parent::__construct( true ); |
| 19 |
} |
| 20 |
|
| 21 |
/** @inheritDoc */ |
| 22 |
public function handle( |
| 23 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 24 |
): ?Node { |
| 25 |
$firstChildElement = DiffDOMUtils::firstNonSepChild( $node ); |
| 26 |
if ( !DOMUtils::isList( $firstChildElement ) |
| 27 |
|| WTUtils::isLiteralHTMLNode( $firstChildElement ) |
| 28 |
) { |
| 29 |
$state->emitChunk( $this->getListBullets( $state, $node ), $node ); |
| 30 |
} |
| 31 |
$liHandler = static function ( $state, $text, $opts ) use ( $node ) { |
| 32 |
return $state->serializer->wteHandlers->liHandler( $node, $state, $text, $opts ); |
| 33 |
}; |
| 34 |
$state->singleLineContext->enforce(); |
| 35 |
$state->serializeChildren( $node, $liHandler ); |
| 36 |
$state->singleLineContext->pop(); |
| 37 |
return $node->nextSibling; |
| 38 |
} |
| 39 |
|
| 40 |
/** @inheritDoc */ |
| 41 |
public function before( Element $node, Node $otherNode, SerializerState $state ): array { |
| 42 |
return [ 'min' => 1, 'max' => 2 ]; |
| 43 |
} |
| 44 |
|
| 45 |
/** @inheritDoc */ |
| 46 |
public function after( Element $node, Node $otherNode, SerializerState $state ): array { |
| 47 |
if ( DOMCompat::nodeName( $otherNode ) === 'dd' |
| 48 |
&& $otherNode instanceof Element // for static analyzers |
| 49 |
&& ( DOMDataUtils::getDataParsoid( $otherNode )->stx ?? null ) === 'row' |
| 50 |
) { |
| 51 |
return [ 'min' => 0, 'max' => 0 ]; |
| 52 |
} else { |
| 53 |
return $this->wtListEOL( $node, $otherNode ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** @inheritDoc */ |
| 58 |
public function firstChild( Node $node, Node $otherNode, SerializerState $state ): array { |
| 59 |
if ( !DOMUtils::isList( $otherNode ) ) { |
| 60 |
return [ 'min' => 0, 'max' => 0 ]; |
| 61 |
} else { |
| 62 |
return []; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
} |