| 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\WTUtils; |
| 11 |
|
| 12 |
class HeadingHandler extends DOMHandler { |
| 13 |
|
| 14 |
/** @var string Heading open/close wikitext (e.g. '===') */ |
| 15 |
public $headingWT; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param string $headingWT Heading open/close wikitext (e.g. '===') |
| 19 |
*/ |
| 20 |
public function __construct( string $headingWT ) { |
| 21 |
parent::__construct( true ); |
| 22 |
$this->headingWT = $headingWT; |
| 23 |
} |
| 24 |
|
| 25 |
/** @inheritDoc */ |
| 26 |
public function handle( |
| 27 |
Element $node, SerializerState $state, bool $wrapperUnmodified = false |
| 28 |
): ?Node { |
| 29 |
// For new elements, for prettier wikitext serialization, |
| 30 |
// emit a space after the last '=' char. |
| 31 |
$space = $this->getLeadingSpace( $state, $node, ' ' ); |
| 32 |
$state->emitChunk( $this->headingWT . $space, $node ); |
| 33 |
$state->singleLineContext->enforce(); |
| 34 |
|
| 35 |
if ( $node->hasChildNodes() ) { |
| 36 |
$state->serializeChildren( $node, null, DiffDOMUtils::firstNonDeletedChild( $node ) ); |
| 37 |
} else { |
| 38 |
// Deal with empty headings |
| 39 |
$state->emitChunk( '<nowiki/>', $node ); |
| 40 |
} |
| 41 |
|
| 42 |
// For new elements, for prettier wikitext serialization, |
| 43 |
// emit a space before the first '=' char. |
| 44 |
$space = $this->getTrailingSpace( $state, $node, ' ' ); |
| 45 |
$state->emitChunk( $space . $this->headingWT, $node ); // Why emitChunk here?? |
| 46 |
$state->singleLineContext->pop(); |
| 47 |
return $node->nextSibling; |
| 48 |
} |
| 49 |
|
| 50 |
/** @inheritDoc */ |
| 51 |
public function before( Element $node, Node $otherNode, SerializerState $state ): array { |
| 52 |
if ( WTUtils::isNewElt( $node ) && DiffDOMUtils::previousNonSepSibling( $node ) && |
| 53 |
!WTUtils::isAnnotationStartMarkerMeta( $otherNode ) |
| 54 |
) { |
| 55 |
// Default to two preceding newlines for new content |
| 56 |
return [ 'min' => 2, 'max' => 2 ]; |
| 57 |
} elseif ( WTUtils::isNewElt( $otherNode ) |
| 58 |
&& DiffDOMUtils::previousNonSepSibling( $node ) === $otherNode |
| 59 |
) { |
| 60 |
// T72791: The previous node was newly inserted, separate |
| 61 |
// them for readability, except if it's an annotation tag |
| 62 |
if ( WTUtils::isAnnotationStartMarkerMeta( $otherNode ) ) { |
| 63 |
return [ 'min' => 1, 'max' => 2 ]; |
| 64 |
} |
| 65 |
return [ 'min' => 2, 'max' => 2 ]; |
| 66 |
} else { |
| 67 |
return [ 'min' => 1, 'max' => 2 ]; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** @inheritDoc */ |
| 72 |
public function after( Element $node, Node $otherNode, SerializerState $state ): array { |
| 73 |
return [ 'min' => 1, 'max' => 2 ]; |
| 74 |
} |
| 75 |
|
| 76 |
} |