Debian Codegraph @

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