mediawiki-extensions-Discus.../modules/HeadingItem.js
Bartosz Dziewoński 3cffe1190e Clean up handling of <span class="mw-headline">
The PHP code should never see a `<span class="mw-headline">` node
since MediaWiki change If04d72f427ec3c3730e757cbb3ade8840c09f7d3,
but we have to support it for our old integration tests (T363031).

Improve code comments to explain this and move the handling to
one place, so that it can be deleted more easily in the future.

Follow-up to 08f61b2609.

Change-Id: I5ab9d3373a6911c1456c30d844b66576b278a1b5
2024-04-20 00:16:45 +00:00

60 lines
1.9 KiB
JavaScript

var ThreadItem = require( './ThreadItem.js' );
// Placeholder headings must have a level higher than real headings (1-6)
var PLACEHOLDER_HEADING_LEVEL = 99;
/**
* A heading item
*
* @class HeadingItem
* @extends ThreadItem
* @constructor
* @param {Object} range
* @param {number|null} headingLevel Heading level (1-6). Use null for a placeholder heading.
*/
function HeadingItem( range, headingLevel ) {
// Parent constructor
HeadingItem.super.call( this, 'heading', 0, range );
this.placeholderHeading = headingLevel === null;
this.headingLevel = this.placeholderHeading ? PLACEHOLDER_HEADING_LEVEL : headingLevel;
}
OO.inheritClass( HeadingItem, ThreadItem );
HeadingItem.prototype.getLinkableTitle = function () {
var title = '';
// If this comment is in 0th section, there's no section title for the edit summary
if ( !this.placeholderHeading ) {
var headline = this.range.startContainer;
var id = headline.getAttribute( 'id' );
if ( id ) {
// Replace underscores with spaces to undo Sanitizer::escapeIdInternal().
// This assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ],
// otherwise the escaped IDs are super garbled and can't be unescaped reliably.
title = id.replace( /_/g, ' ' );
}
// else: Not a real section, probably just HTML markup in wikitext
}
return title;
};
/**
* Check whether this heading can be used for topic subscriptions.
*
* @return {boolean}
*/
HeadingItem.prototype.isSubscribable = function () {
return (
// Placeholder headings have nothing to attach the button to.
!this.placeholderHeading &&
// We only allow subscribing to level 2 headings, because the user interface for sub-headings
// would be difficult to present.
this.headingLevel === 2 &&
// Check if the name corresponds to a section that contain no comments (only sub-sections).
// They can't be distinguished from each other, so disallow subscribing.
this.name !== 'h-'
);
};
module.exports = HeadingItem;