| 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\NodeData\DataParsoid; |
| 10 |
use Wikimedia\Parsoid\Utils\DiffDOMUtils; |
| 11 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 12 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 13 |
|
| 14 |
class TRHandler extends DOMHandler { |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
parent::__construct( true ); |
| 18 |
} |
| 19 |
|
| 20 |
/** @inheritDoc */ |
| 21 |
public function handle( |
| 22 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 23 |
): ?Node { |
| 24 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 25 |
|
| 26 |
if ( $this->trWikitextNeeded( $node, $dp ) ) { |
| 27 |
$state->emitChunk( |
| 28 |
$this->serializeTableTag( |
| 29 |
$dp->startTagSrc ?? '|-', '', $state, |
| 30 |
$node, $wrapperUnmodified |
| 31 |
), |
| 32 |
$node |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
$state->serializeChildren( $node ); |
| 37 |
return $node->nextSibling; |
| 38 |
} |
| 39 |
|
| 40 |
/** @inheritDoc */ |
| 41 |
public function before( Element $node, Node $otherNode, SerializerState $state ): array { |
| 42 |
if ( $this->trWikitextNeeded( $node, DOMDataUtils::getDataParsoid( $node ) ) ) { |
| 43 |
return [ 'min' => 1, 'max' => $this->maxNLsInTable( $node, $otherNode ) ]; |
| 44 |
} else { |
| 45 |
return [ 'min' => 0, 'max' => $this->maxNLsInTable( $node, $otherNode ) ]; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** @inheritDoc */ |
| 50 |
public function after( Element $node, Node $otherNode, SerializerState $state ): array { |
| 51 |
return [ 'min' => 0, 'max' => $this->maxNLsInTable( $node, $otherNode ) ]; |
| 52 |
} |
| 53 |
|
| 54 |
private function trWikitextNeeded( Element $node, DataParsoid $dp ): bool { |
| 55 |
// If the token has 'startTagSrc' set, it means that the tr |
| 56 |
// was present in the source wikitext and we emit it -- if not, |
| 57 |
// we ignore it. |
| 58 |
// ignore comments and ws |
| 59 |
if ( ( $dp->startTagSrc ?? null ) || DiffDOMUtils::previousNonSepSibling( $node ) ) { |
| 60 |
return true; |
| 61 |
} else { |
| 62 |
// If parent has a thead/tbody previous sibling, then |
| 63 |
// we need the |- separation. But, a caption preceded |
| 64 |
// this node's parent, all is good. |
| 65 |
$parentSibling = DiffDOMUtils::previousNonSepSibling( $node->parentNode ); |
| 66 |
|
| 67 |
// thead/tbody/tfoot is always present around tr tags in the DOM. |
| 68 |
return $parentSibling && DOMCompat::nodeName( $parentSibling ) !== 'caption'; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
} |