Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentHeadingItem
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getLinkableTitle
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getHeadingLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHeadingLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isPlaceholderHeading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPlaceholderHeading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranscludedFrom
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools\ThreadItem;
4
5use MediaWiki\Extension\DiscussionTools\ImmutableRange;
6use Wikimedia\Assert\Assert;
7use Wikimedia\Parsoid\DOM\Element;
8
9class ContentHeadingItem extends ContentThreadItem implements HeadingItem {
10    use HeadingItemTrait;
11
12    private bool $placeholderHeading;
13    private int $headingLevel;
14
15    // Placeholder headings must have a level higher than real headings (1-6)
16    private const PLACEHOLDER_HEADING_LEVEL = 99;
17
18    /**
19     * @param ImmutableRange $range
20     * @param ?int $headingLevel Heading level (1-6). Use null for a placeholder heading.
21     */
22    public function __construct(
23        ImmutableRange $range, ?int $headingLevel
24    ) {
25        parent::__construct( 'heading', 0, $range );
26        $this->placeholderHeading = $headingLevel === null;
27        $this->headingLevel = $this->placeholderHeading ? static::PLACEHOLDER_HEADING_LEVEL : $headingLevel;
28    }
29
30    /**
31     * Get a title based on the hash ID, such that it can be linked to
32     *
33     * @return string Title
34     */
35    public function getLinkableTitle(): string {
36        $title = '';
37        // If this comment is in 0th section, there's no section title for the edit summary
38        if ( !$this->isPlaceholderHeading() ) {
39            // <span class="mw-headline" …>, or <hN …> in Parsoid HTML
40            $headline = $this->getRange()->startContainer;
41            Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' );
42            $id = $headline->getAttribute( 'id' );
43            if ( $id ) {
44                // Replace underscores with spaces to undo Sanitizer::escapeIdInternal().
45                // This assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ],
46                // otherwise the escaped IDs are super garbled and can't be unescaped reliably.
47                $title = str_replace( '_', ' ', $id );
48            }
49            // else: Not a real section, probably just HTML markup in wikitext
50        }
51        return $title;
52    }
53
54    /**
55     * @return int Heading level (1-6)
56     */
57    public function getHeadingLevel(): int {
58        return $this->headingLevel;
59    }
60
61    /**
62     * @param int $headingLevel Heading level (1-6)
63     */
64    public function setHeadingLevel( int $headingLevel ): void {
65        $this->headingLevel = $headingLevel;
66    }
67
68    /**
69     * @return bool
70     */
71    public function isPlaceholderHeading(): bool {
72        return $this->placeholderHeading;
73    }
74
75    /**
76     * @param bool $placeholderHeading
77     */
78    public function setPlaceholderHeading( bool $placeholderHeading ): void {
79        $this->placeholderHeading = $placeholderHeading;
80    }
81
82    /**
83     * @inheritDoc
84     */
85    public function getTranscludedFrom() {
86        // Placeholder headings break the usual logic, because their ranges are collapsed
87        if ( $this->isPlaceholderHeading() ) {
88            return false;
89        }
90        // Collapsed ranges should otherwise be impossible, but they're not (T299583)
91        // TODO: See if we can fix the root cause, and remove this?
92        if ( $this->getRange()->collapsed ) {
93            return false;
94        }
95        return parent::getTranscludedFrom();
96    }
97}