Debian Codegraph @

vendor/wikimedia/parsoid/src/Html2Wt/DOMHandlers/DDHandler.php

raw
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\DiffUtils;
9 use Wikimedia\Parsoid\Html2Wt\SerializerState;
10 use Wikimedia\Parsoid\Utils\DiffDOMUtils;
11 use Wikimedia\Parsoid\Utils\DOMUtils;
12 use Wikimedia\Parsoid\Utils\WTUtils;
13
14 class DDHandler extends DOMHandler {
15
16 /** @var ?string Syntax */
17 private $stx;
18
19 public function __construct( ?string $stx = null ) {
20 parent::__construct( $stx !== 'row' );
21 $this->stx = $stx;
22 }
23
24 /** @inheritDoc */
25 public function handle(
26 Element $node, SerializerState $state, bool $wrapperUnmodified = false
27 ): ?Node {
28 $firstChildElement = DiffDOMUtils::firstNonSepChild( $node );
29 $chunk = ( $this->stx === 'row' ) ? ':' : $this->getListBullets( $state, $node );
30 if ( !DOMUtils::isList( $firstChildElement )
31 || WTUtils::isLiteralHTMLNode( $firstChildElement )
32 ) {
33 $state->emitChunk( $chunk, $node );
34 }
35 $liHandler = static function ( $state, $text, $opts ) use ( $node ) {
36 return $state->serializer->wteHandlers->liHandler( $node, $state, $text, $opts );
37 };
38 $state->singleLineContext->enforce();
39 $state->serializeChildren( $node, $liHandler );
40
41 // Recover trailing whitespace (only on unmodified innermost <dd> nodes);
42 // Consider "::: foo ". Since WS is only trimmed on the innermost <dd> node,
43 // it makes sense to recover this only for the innermost <dd> node.
44 // [ Given current DSR offsets, without this check, we'll recover one space for
45 // every nested <li> node which makes for lotsa dirty diffs. ]
46 $lastChild = DiffDOMUtils::lastNonSepChild( $node );
47 if ( $lastChild && !DOMUtils::isList( $lastChild ) &&
48 !DiffUtils::hasDiffMarkers( $lastChild ) &&
49 !( $lastChild instanceof Element && $lastChild->hasAttribute( 'data-mw-selser-wrapper' ) )
50 ) {
51 $trailingSpace = $state->recoverTrimmedWhitespace( $node, false );
52 $state->appendSep( $trailingSpace ?? '' );
53 }
54
55 $state->singleLineContext->pop();
56 return $node->nextSibling;
57 }
58
59 /** @inheritDoc */
60 public function before( Element $node, Node $otherNode, SerializerState $state ): array {
61 if ( $this->stx === 'row' ) {
62 return [ 'min' => 0, 'max' => 0 ];
63 } else {
64 return [ 'min' => 1, 'max' => 2 ];
65 }
66 }
67
68 /** @inheritDoc */
69 public function after( Element $node, Node $otherNode, SerializerState $state ): array {
70 return $this->wtListEOL( $node, $otherNode );
71 }
72
73 /** @inheritDoc */
74 public function firstChild( Node $node, Node $otherNode, SerializerState $state ): array {
75 if ( !DOMUtils::isList( $otherNode ) ) {
76 return [ 'min' => 0, 'max' => 0 ];
77 } else {
78 return [];
79 }
80 }
81
82 }