Jdlrobson has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/353293 )
Change subject: Only wrap top level headings ...................................................................... Only wrap top level headings Changes: * prepareHeadings is now prepareHeading * A heading is only transformed when it is encountered during the DOM traversal Bug: T152055 Change-Id: Id26632fe276c0f8c5720df39137f8a75648a2ba5 --- M includes/MobileFormatter.php M tests/phpunit/MobileFormatterTest.php 2 files changed, 29 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/93/353293/1 diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php index 9d7db0a..b20503f 100644 --- a/includes/MobileFormatter.php +++ b/includes/MobileFormatter.php @@ -660,7 +660,6 @@ $firstHeading = reset( $headings ); $sectionNumber = 0; $sectionBody = $this->createSectionBodyElement( $doc, $sectionNumber, false ); - $this->prepareHeadings( $doc, $headings, $this->scriptsEnabled ); while ( $containerChild ) { $node = $containerChild; @@ -671,7 +670,10 @@ // Note well the use of DOMNode#nodeName here. Only DOMElement defines // DOMElement#tagName. So, if there's trailing text - represented by // DOMText - then accessing #tagName will trigger an error. - if ( $headings && $node->nodeName === $firstHeading->nodeName ) { + if ( $firstHeading && $node->nodeName === $firstHeading->nodeName ) { + // The heading we are transforming is always 1 section ahead of the + // section we are currently processing + $this->prepareHeading( $doc, $node, $sectionNumber + 1, $this->scriptsEnabled ); if ( $sectionBody->hasChildNodes() ) { // Apply transformations to the section body $this->filterContentInSection( $sectionBody, $doc, $sectionNumber, $transformOptions ); @@ -720,25 +722,21 @@ * Prepare section headings, add required classes and onclick actions * * @param DOMDocument $doc - * @param array $headings + * @param DOMElement $heading + * @param Integer $sectionNumber * @param bool $isCollapsible */ - private function prepareHeadings( DOMDocument $doc, array $headings, $isCollapsible ) { - $sectionNumber = 0; - // Mark the top level headings which could control collapsing - foreach ( $headings as $heading ) { - $sectionNumber += 1; - $className = $heading->hasAttribute( 'class' ) ? $heading->getAttribute( 'class' ) . ' ' : ''; - $heading->setAttribute( 'class', $className . 'section-heading' ); - if ( $isCollapsible ) { - $heading->setAttribute( 'onclick', 'javascript:mfTempOpenSection(' . $sectionNumber . ')' ); - } - - // prepend indicator - $indicator = $doc->createElement( 'div' ); - $indicator->setAttribute( 'class', MobileUI::iconClass( '', 'element', 'indicator' ) ); - $heading->insertBefore( $indicator, $heading->firstChild ); + private function prepareHeading( DOMDocument $doc, $heading, $sectionNumber, $isCollapsible ) { + $className = $heading->hasAttribute( 'class' ) ? $heading->getAttribute( 'class' ) . ' ' : ''; + $heading->setAttribute( 'class', $className . 'section-heading' ); + if ( $isCollapsible ) { + $heading->setAttribute( 'onclick', 'javascript:mfTempOpenSection(' . $sectionNumber . ')' ); } + + // prepend indicator + $indicator = $doc->createElement( 'div' ); + $indicator->setAttribute( 'class', MobileUI::iconClass( '', 'element', 'indicator' ) ); + $heading->insertBefore( $indicator, $heading->firstChild ); } /** diff --git a/tests/phpunit/MobileFormatterTest.php b/tests/phpunit/MobileFormatterTest.php index a3b7800..2de4339 100644 --- a/tests/phpunit/MobileFormatterTest.php +++ b/tests/phpunit/MobileFormatterTest.php @@ -142,6 +142,19 @@ . $this->makeSectionHtml( 1, $refplaceholder, true ); return [ + // Nested headings are not wrapped + [ + '<div class="wrapper"><p>Text goes here i think 2testestestestest</p>' + . '<h2>Heading</h2>I am awesome</div>' + . 'Text<h2>test</h2><p>more text</p>', + $this->makeSectionHtml( 0, + '<div class="wrapper"><p>Text goes here i think 2testestestestest</p>' + . '<h2>Heading</h2>I am awesome</div>Text' ) + . $this->makeSectionHeading( 'h2', 'test' ) + . $this->makeSectionHtml( 1, '<p>more text</p>' ), + $enableSections, + false, false, false + ], // # Lazy loading images // Main page not impacted [ -- To view, visit https://gerrit.wikimedia.org/r/353293 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id26632fe276c0f8c5720df39137f8a75648a2ba5 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits