Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
HeadingHandler
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 before
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 after
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Html2Wt\DOMHandlers;
5
6use Wikimedia\Parsoid\DOM\Element;
7use Wikimedia\Parsoid\DOM\Node;
8use Wikimedia\Parsoid\Html2Wt\SerializerState;
9use Wikimedia\Parsoid\Utils\DOMUtils;
10use Wikimedia\Parsoid\Utils\WTUtils;
11
12class 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, DOMUtils::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 ) && DOMUtils::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            && DOMUtils::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}