| 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\DOMUtils; |
| 12 |
use Wikimedia\Parsoid\Utils\WTUtils; |
| 13 |
|
| 14 |
class ListHandler extends DOMHandler { |
| 15 |
|
| 16 |
/** @var string[] List of tag names which van be first children of the list */ |
| 17 |
public $firstChildNames; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string[] $firstChildNames List of tag names which van be first children of the list |
| 21 |
*/ |
| 22 |
public function __construct( array $firstChildNames ) { |
| 23 |
parent::__construct( true ); |
| 24 |
$this->firstChildNames = $firstChildNames; |
| 25 |
} |
| 26 |
|
| 27 |
/** @inheritDoc */ |
| 28 |
public function handle( |
| 29 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 30 |
): ?Node { |
| 31 |
// Disable single-line context here so that separators aren't |
| 32 |
// suppressed between nested list elements. |
| 33 |
$state->singleLineContext->disable(); |
| 34 |
|
| 35 |
$firstChildElt = DiffDOMUtils::firstNonSepChild( $node ); |
| 36 |
|
| 37 |
// Skip builder-inserted wrappers |
| 38 |
// Ex: <ul><s auto-inserted-start-and-end-><li>..</li><li>..</li></s>...</ul> |
| 39 |
// output from: <s>\n*a\n*b\n*c</s> |
| 40 |
while ( $firstChildElt && $this->isBuilderInsertedElt( $firstChildElt ) ) { |
| 41 |
$firstChildElt = DiffDOMUtils::firstNonSepChild( $firstChildElt ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( !$firstChildElt || !in_array( DOMCompat::nodeName( $firstChildElt ), $this->firstChildNames, true ) |
| 45 |
|| WTUtils::isLiteralHTMLNode( $firstChildElt ) |
| 46 |
) { |
| 47 |
$state->emitChunk( $this->getListBullets( $state, $node ), $node ); |
| 48 |
} |
| 49 |
|
| 50 |
$liHandler = static function ( $state, $text, $opts ) use ( $node ) { |
| 51 |
return $state->serializer->wteHandlers->liHandler( $node, $state, $text, $opts ); |
| 52 |
}; |
| 53 |
$state->serializeChildren( $node, $liHandler ); |
| 54 |
$state->singleLineContext->pop(); |
| 55 |
return $node->nextSibling; |
| 56 |
} |
| 57 |
|
| 58 |
/** @inheritDoc */ |
| 59 |
public function before( Element $node, Node $otherNode, SerializerState $state ): array { |
| 60 |
if ( DOMUtils::atTheTop( $otherNode ) ) { |
| 61 |
return [ 'min' => 0, 'max' => 0 ]; |
| 62 |
} |
| 63 |
|
| 64 |
// node is in a list & otherNode has the same list parent |
| 65 |
// => exactly 1 newline |
| 66 |
if ( DOMUtils::isListItem( $node->parentNode ) && $otherNode->parentNode === $node->parentNode ) { |
| 67 |
return [ 'min' => 1, 'max' => 1 ]; |
| 68 |
} |
| 69 |
|
| 70 |
// A list in a block node (<div>, <td>, etc) doesn't need a leading empty line |
| 71 |
// if it is the first non-separator child (ex: <div><ul>...</div>) |
| 72 |
if ( |
| 73 |
DOMUtils::isWikitextBlockNode( $node->parentNode ) && |
| 74 |
DiffDOMUtils::firstNonSepChild( $node->parentNode ) === $node |
| 75 |
) { |
| 76 |
return [ 'min' => 1, 'max' => 2 ]; |
| 77 |
} elseif ( DOMUtils::isFormattingElt( $otherNode ) ) { |
| 78 |
return [ 'min' => 1, 'max' => 1 ]; |
| 79 |
} else { |
| 80 |
return [ |
| 81 |
'min' => WTUtils::isNewElt( $node ) && !WTUtils::isMarkerAnnotation( $otherNode ) |
| 82 |
? 2 : 1, |
| 83 |
'max' => 2 |
| 84 |
]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** @inheritDoc */ |
| 89 |
public function after( Element $node, Node $otherNode, SerializerState $state ): array { |
| 90 |
return $this->wtListEOL( $node, $otherNode ); |
| 91 |
} |
| 92 |
|
| 93 |
} |