| 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 TableHandler extends DOMHandler { |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
parent::__construct( false ); |
| 19 |
} |
| 20 |
|
| 21 |
/** @inheritDoc */ |
| 22 |
public function handle( |
| 23 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 24 |
): ?Node { |
| 25 |
$dp = DOMDataUtils::getDataParsoid( $node ); |
| 26 |
$wt = $dp->startTagSrc ?? '{|'; |
| 27 |
$indentTable = DOMCompat::nodeName( $node->parentNode ) === 'dd' |
| 28 |
&& DiffDOMUtils::previousNonSepSibling( $node ) === null; |
| 29 |
if ( $indentTable ) { |
| 30 |
$state->singleLineContext->disable(); |
| 31 |
} |
| 32 |
$state->emitChunk( |
| 33 |
$this->serializeTableTag( $wt, '', $state, $node, $wrapperUnmodified ), |
| 34 |
$node |
| 35 |
); |
| 36 |
if ( !WTUtils::isLiteralHTMLNode( $node ) ) { |
| 37 |
$state->wikiTableNesting++; |
| 38 |
} |
| 39 |
$state->serializeChildren( $node ); |
| 40 |
if ( !WTUtils::isLiteralHTMLNode( $node ) ) { |
| 41 |
$state->wikiTableNesting--; |
| 42 |
} |
| 43 |
if ( $state->sep->constraints === null ) { |
| 44 |
// Special case hack for "{|\n|}" since state.sep is |
| 45 |
// cleared in SSP.emitSep after a separator is emitted. |
| 46 |
// However, for {|\n|}, the <table> tag has no element |
| 47 |
// children which means lastchild -> parent constraint |
| 48 |
// is never computed and set here. |
| 49 |
$state->sep->constraints = [ 'min' => 1, 'max' => 2, 'constraintInfo' => [] ]; |
| 50 |
} |
| 51 |
$state->emitChunk( $dp->endTagSrc ?? '|}', $node ); |
| 52 |
if ( $indentTable ) { |
| 53 |
$state->singleLineContext->pop(); |
| 54 |
} |
| 55 |
return $node->nextSibling; |
| 56 |
} |
| 57 |
|
| 58 |
/** @inheritDoc */ |
| 59 |
public function before( Element $node, Node $otherNode, SerializerState $state ): array { |
| 60 |
// Handle special table indentation case! |
| 61 |
if ( $node->parentNode === $otherNode && DOMCompat::nodeName( $otherNode ) === 'dd' ) { |
| 62 |
return [ 'min' => 0, 'max' => 2 ]; |
| 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 |
if ( ( WTUtils::isNewElt( $node ) || WTUtils::isNewElt( $otherNode ) ) |
| 71 |
&& !DOMUtils::atTheTop( $otherNode ) |
| 72 |
) { |
| 73 |
return [ 'min' => 1, 'max' => 2 ]; |
| 74 |
} else { |
| 75 |
return [ 'min' => 0, 'max' => 2 ]; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** @inheritDoc */ |
| 80 |
public function firstChild( Node $node, Node $otherNode, SerializerState $state ): array { |
| 81 |
return [ 'min' => 1, 'max' => $this->maxNLsInTable( $node, $otherNode ) ]; |
| 82 |
} |
| 83 |
|
| 84 |
/** @inheritDoc */ |
| 85 |
public function lastChild( Node $node, Node $otherNode, SerializerState $state ): array { |
| 86 |
return [ 'min' => 1, 'max' => $this->maxNLsInTable( $node, $otherNode ) ]; |
| 87 |
} |
| 88 |
|
| 89 |
} |