Page MenuHomePhabricator

Headings selection needs to be updated in PageHTMLParser
Closed, DuplicatePublic

Description

Dedicated task for a comment I posted on T13555:

While doing some code browsing about an unrelated task, I noticed that, in MobileFrontend, some update may be needed in PageHTMLParser.js.

First, see line 3:

HEADING_SELECTOR = mw.config.get( 'wgMFMobileFormatterHeadings', [ 'h1', 'h2', 'h3', 'h4', 'h5' ] ).join( ',' ),

(At the time of writing, wgMFMobileFormatterHeadings is [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].)

Then, see in findSectionHeadingByIndex() method, line 45:

return this.$headings
	// Headings must strictly be a child element of a section element
	// or the parser-output.
	// Not an ancestor!
	.filter( '.mw-parser-output > *, [class^="mf-section-"] > *' ).eq( sectionIndex - 1 );

That filter() no longer matches the headings, because of the new markup.

(By the way, note that find( 'h1, h2, h3, h4, h5, h6' ), which uses querySelectorAll() underneath, is extremely slow, as elaborated in T220751. A more efficient code would be to loop over h1, then h2, etc. as it would make use of getElementsByTagName().)

Subsequently, I also posted a comment on T220751:

It seems that this code needs to be updated, please have a look at T13555#10603383.

I don't remember if "raw headings" are counted in the &section=N indexes. If they are not counted, that's good news for the case at hand: provided we are fine with targetting h1 ... h6 levels, we could replace everything with just .find( '.mw-heading' ) (and it wouldn't even need the subsequent .filter()).

Also, refs this page: Heading HTML changes.

Event Timeline

About the wgMFMobileFormatterHeadings config item, which comes from $config->get( 'MFMobileFormatterOptions' )['headings'] and is documented here, see T110436.

JScherer-WMF changed the task status from Open to In Progress.Mar 10 2025, 5:36 PM
JScherer-WMF assigned this task to bwang.
JScherer-WMF added a project: Web-Team.
bwang triaged this task as Medium priority.Mar 11 2025, 3:43 PM

I think this is a duplicate of T386873, a solution to T386873 is to update wgMFMobileFormatterHeadings. but that is not the only solution