Florianschmidtwelzow has uploaded a new change for review.

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

Change subject: Fix lead section handling in MobileFrontend
......................................................................

Fix lead section handling in MobileFrontend

Page#getLeadSectionElement always assumes, that the lead section is wrapped in 
the
first div element under the .content class div element. This, in fact, isn't 
true,
it depends on more settings, because the lead section is makred within the 
process
of marking the expandable sections (which isn't done on special pages, category 
pages
and so on). In fact, getLeadSectionElement never worked reliable and the worst 
case
scenario would be "don't trust the value". This especailly makes problems for 
the editor,
which uses this information to determine where to link the top ca-edit button 
to. This
could result in the problem, that the top/lead section can't be edited, if the 
first div
element is empty (because it's not the "real" lead section (because it's not 
marked)).

This change rewrites the code a little bit, which handles the marking of the 
lead section
and the expandable section headings. Now, the lead section is always marked, no 
matter,
if expandable sections are enabled on this page or not. Furthermore, instead of 
relying
on the amount of div containers (which is very fuzzy), 
Page#getLeadSectionElement now uses
a newly added class for the lead section (mw-mobilefrontend-leadsection) to 
always return
the correct lead section element.

Bug: T122471
Change-Id: I310822cffd43115d56c1b253e564f79ca0acdf79
---
M includes/MobileFormatter.php
M resources/mobile.startup/Page.js
2 files changed, 43 insertions(+), 12 deletions(-)


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

diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php
index 3a9f0d8..573aefc 100644
--- a/includes/MobileFormatter.php
+++ b/includes/MobileFormatter.php
@@ -9,7 +9,7 @@
 class MobileFormatter extends HtmlFormatter {
        /** @var string $pageTransformStart String prefixes to be
                applied at start and end of output from Parser */
-       protected $pageTransformStart = '<div>';
+       protected $pageTransformStart = '<div 
class="mw-mobilefrontend-leadsection">';
        /** @var string $pageTransformEnd String prefixes to be
                applied at start and end of output from Parser */
        protected $pageTransformEnd = '</div>';
@@ -272,14 +272,43 @@
                        );
                }
 
-               // Makes sections expandable
+               // --- Start Lead section marker and enable expandable sections 
---
+
+               // $tagRegEx is a regex filter for any heading with the passed 
tagName.
                $tagRegEx = '<' . $tagName . '.*</' . $tagName . '>';
+               // set the limit for the preg_replace call. In general, this is 
controlled by the  value,
+               // if expandable sections are enabled or not. If not, we have 
to make sure, that the lead
+               // section is marked correctly, so we loop through the matches, 
but limit it to 1 (which
+               // is the lead section marking). - bug T122471
+               $limit = $this->expandableSections ? -1 : 1;
+               // start the html with the lead section marker (div element)
                $s = $this->pageTransformStart .
-                       preg_replace(
-                               '%(' . $tagRegEx . ')%sU', 
$this->headingTransformStart . '\1' . $this->headingTransformEnd,
-                               $s
-                       ) .
-                       $this->pageTransformEnd;
+                       preg_replace_callback(
+                               '%(' . $tagRegEx . ')%sU', function ( $matches 
) {
+                                       // if the sections should be 
expandable, enable them
+                                       if ( $this->expandableSections ) {
+                                               return 
$this->headingTransformStart .
+                                                       $matches[0] . 
$this->headingTransformEnd;
+                                       } else {
+                                               // otherwise, simply close the 
lead section div (this should be the
+                                               // first and last run for this 
function) and add the heading (the matched
+                                               // text), so we don't loose one
+                                               return 
$this->headingTransformStart . $matches[0];
+                                       }
+                               },
+                               $s,
+                               // the limit we set earlier
+                               $limit
+                       );
+
+               // we need to close the div elements properly (inside the page 
html, the function inside
+               // preg_replace_callback takes care of closing the div elements 
by starting the replacement
+               // with closing the div of the round before, so the last one is 
still unclosed).
+               if ( $this->expandableSections ) {
+                       $s .= $this->pageTransformEnd;
+               }
+
+               // --- End Lead section marker and enable expandable sections 
---
 
                return $s;
        }
@@ -312,10 +341,8 @@
         * @return string
         */
        protected function onHtmlReady( $html ) {
-               if ( $this->expandableSections ) {
-                       $tagName = $this->findTopHeading( $html );
-                       $html = $this->headingTransform( $html, $tagName );
-               }
+               $tagName = $this->findTopHeading( $html );
+               $html = $this->headingTransform( $html, $tagName );
 
                return $html;
        }
diff --git a/resources/mobile.startup/Page.js b/resources/mobile.startup/Page.js
index 1fc513b..a626344a 100644
--- a/resources/mobile.startup/Page.js
+++ b/resources/mobile.startup/Page.js
@@ -113,12 +113,16 @@
                         * <div id="bodyContent">
                         *   <!-- content of the page.. -->
                         *   <div id="mw-content-text">
-                        *     <div>lead section</div>
+                        *     <div class="mw-mobilefrontend-leadsection">lead 
section</div>
                         *     <h2></h2>
                         *     <div>second section</div>
                         *   </div>
                         * </div>
                         */
+                       if ( $( '.mw-mobilefrontend-leadsection' ).length ) {
+                               return $( '.mw-mobilefrontend-leadsection' )
+                       }
+                       // FIXME: Remove this, when the cache has cleared - bug 
T122471
                        return this.$( '> div > div' ).eq( 0 );
                },
 

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

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

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

Reply via email to