Jhernandez has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/275879

Change subject: MobileFormatter: Move lazyLoadImages rewrite to be per-section
......................................................................

MobileFormatter: Move lazyLoadImages rewrite to be per-section

Move the lazy load images rewrite to be applied per section instead of to the
whole document.

* Also remove $removeDefaults param in filterContent since what it is for can
be guessed from the fields inside MobileFormatter. No need to pass it around.

Bug: T127128
Change-Id: I61c0ace4ac34f2f8a45eb2f28bad67ad64a2bec8
---
M includes/MobileFormatter.php
M includes/MobileFrontend.body.php
2 files changed, 45 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/79/275879/1

diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php
index 1aa4fdb..ff49f09 100644
--- a/includes/MobileFormatter.php
+++ b/includes/MobileFormatter.php
@@ -89,55 +89,67 @@
 
        /**
         * Performs various transformations to the content to make it 
appropiate for mobile devices.
-        * @param bool $removeDefaults Whether default settings at 
$wgMFRemovableClasses should be used
         * @return array
         */
-       public function filterContent( $removeDefaults = true ) {
-               $ctx = MobileContext::singleton();
+       public function filterContent() {
                $doc = $this->getDoc();
 
-               // Initial pass to sectionify the content if it is not the main 
page and
-               // transform headings
-               if ( !$this->mainPage && $this->expandableSections) {
-                       list( $headings, $subheadings ) = $this->getHeadings( 
$doc );
-                       $this->makeSections( $doc, $headings );
-                       $this->makeHeadingsEditable( $subheadings );
-
+               $ctx = MobileContext::singleton();
+               $isSpecialPage = $this->title->isSpecialPage();
+               $mfRemovableClasses = $ctx->getMFConfig()->get( 
'MFRemovableClasses' );
+               $removableClasses = $mfRemovableClasses['base'];
+               if ( $ctx->isBetaGroupMember() ) {
+                       $removableClasses = array_merge( $removableClasses, 
$mfRemovableClasses['beta'] );
                }
 
-               $mfRemovableClasses = $ctx->getMFConfig()
-                       ->get( 'MFRemovableClasses' );
-               if ( $removeDefaults ) {
-                       $this->remove( $mfRemovableClasses['base'] );
-                       if ( $ctx->isBetaGroupMember() ) {
-                               $this->remove( $mfRemovableClasses['beta'] );
-                       }
+               // Don't remove elements in special pages
+               if ( !$isSpecialPage ) {
+                       $this->remove( $removableClasses );
                }
 
                if ( $this->removeMedia ) {
                        $this->doRemoveImages();
-               } else {
-                       $mfLazyLoadImages = $ctx->getMFConfig()
-                               ->get( 'MFLazyLoadImages' );
+               }
 
-                       if (
-                                       $mfLazyLoadImages['base'] ||
-                                       ( $ctx->isBetaGroupMember() && 
$mfLazyLoadImages['beta'] )
-                       ) {
-                               $this->doRewriteImagesForLazyLoading();
-                       }
+               // Sectionify the content and transform it if necessary per 
section
+               if ( !$this->mainPage && $this->expandableSections) {
+                       list( $headings, $subheadings ) = $this->getHeadings( 
$doc );
+                       $this->makeSections( $doc, $headings );
+                       $this->makeHeadingsEditable( $subheadings );
+               } else {
+                       // Otherwise apply the per-section transformations to 
the document as a whole
+                       $this->filterContentInSection( $doc, $doc, 0 );
                }
 
                return parent::filterContent();
        }
 
        /**
-        * Enables images to be loaded asynchronously
+        * Apply filtering per element (section) in a document.
+        * @param DOMElement|DOMDocument $el
+        * @param DOMDocument $doc
+        * @param number $sectionNumber Which section is it on the document
         */
-       private function doRewriteImagesForLazyLoading() {
-               $doc = $this->getDoc();
+       private function filterContentInSection( $el, DOMDocument $doc, 
$sectionNumber ) {
+               $ctx = MobileContext::singleton();
+               $mfLazyLoadImages = $ctx->getMFConfig()->get( 
'MFLazyLoadImages' );
+               $lazyLoadImages = $mfLazyLoadImages['base'] ||
+                       ( $ctx->isBetaGroupMember() && 
$mfLazyLoadImages['beta'] );
 
-               foreach ( $doc->getElementsByTagName( 'img' ) as $img ) {
+               if ( !$this->removeMedia && $lazyLoadImages) {
+                       $this->doRewriteImagesForLazyLoading( $el, $doc );
+               }
+       }
+
+       /**
+        * Enables images to be loaded asynchronously
+        *
+        * @param DOMElement|DOMDocument $el Element or document to rewrite 
images in.
+        * @param DOMDocument $doc Document to create elements in
+        */
+       private function doRewriteImagesForLazyLoading( $el, DOMDocument $doc ) 
{
+
+               foreach ( $el->getElementsByTagName( 'img' ) as $img ) {
                        $parent = $img->parentNode;
                        $width = $img->getAttribute( 'width' );
                        $height = $img->getAttribute( 'height' );
@@ -328,6 +340,8 @@
                        // DOMText - then accessing #tagName will trigger an 
error.
                        if ( $node->nodeName === $firstHeading->nodeName ) {
                                if ( $sectionBody->hasChildNodes() ) {
+                                       // Apply transformations to the section 
body
+                                       $this->filterContentInSection( 
$sectionBody, $doc, $sectionNumber );
                                        // Insert the previous section body and 
reset it for the new section
                                        $body->insertBefore( $sectionBody, 
$node );
                                        $sectionNumber += 1;
diff --git a/includes/MobileFrontend.body.php b/includes/MobileFrontend.body.php
index 6cf0bfb..2a4fc9c 100644
--- a/includes/MobileFrontend.body.php
+++ b/includes/MobileFrontend.body.php
@@ -41,7 +41,6 @@
                Hooks::run( 'MobileFrontendBeforeDOM', array( $context, 
$formatter ) );
 
                $title = $out->getTitle();
-               $isSpecialPage = $title->isSpecialPage();
                $formatter->enableExpandableSections(
                        // Don't collapse sections e.g. on JS pages
                        $out->canUseWikiPage()
@@ -55,8 +54,7 @@
                        && $context->getRequest()->getText( 'action', 'view' ) 
== 'view'
                );
                if ( $context->getContentTransformations() ) {
-                       // Remove images if they're disabled from special 
pages, but don't transform otherwise
-                       $formatter->filterContent( /* remove defaults */ 
!$isSpecialPage );
+                       $formatter->filterContent();
                }
 
                $contentHtml = $formatter->getText();

-- 
To view, visit https://gerrit.wikimedia.org/r/275879
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I61c0ace4ac34f2f8a45eb2f28bad67ad64a2bec8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jhernandez <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to