Pmiazga has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/350329 )

Change subject: Move paragraph and immediate list element before infobox
......................................................................

Move paragraph and immediate list element before infobox

Changes:
 - if list (ol or ul element) is an immediate first paragraph
sibling move with paragraph before table.infobox element

Reason for not using pure xpath query:
It is possible to query immediate following ol/ul element with
xPath query: following-sibling::*[position()=1][name()='ol' or name()='ul']
but this approach is 20% slower than getting first sibling and
verifing the tagName.
Testing scenario:
5 rounds - call MobileFormatter::filterContent() 1 million times
xPath only = ~181 seconds each round
xPath + tagName = ~138 seconds each round
CPU: i7-4800MQ@2.7GHz

Bug: T149852
Change-Id: I4257dbdf05cdb99634bdbda0f25ddde05e7cb9c5
---
M includes/MobileFormatter.php
M tests/phpunit/MobileFormatterTest.php
2 files changed, 36 insertions(+), 2 deletions(-)


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

diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php
index a4603a6..cb66b87 100644
--- a/includes/MobileFormatter.php
+++ b/includes/MobileFormatter.php
@@ -229,6 +229,9 @@
         * Note that the first paragraph is not moved before hatnotes, or mbox 
or other
         * elements that are not infoboxes.
         *
+        * Additionally if paragraph immediate sibling is a list (ol or ul 
element), the list
+        * is also moved along with paragraph above infobox.
+        *
         * @param DOMElement $leadSectionBody
         * @param DOMDocument $doc Document to which the section belongs
         */
@@ -252,8 +255,23 @@
                                        break;
                                }
                        }
+
                        if ( $firstP ) {
-                               $leadSectionBody->insertBefore( $firstP, 
$infoboxAndParagraphs->item( 0 ) );
+                               $listElementAfterParagraph = null;
+                               $where = $infoboxAndParagraphs->item( 0 );
+
+                               $elementAfterParagraphQuery = $xPath->query( 
'following-sibling::*[1]', $firstP );
+                               if ( $elementAfterParagraphQuery->length > 0 ) {
+                                       $elem = 
$elementAfterParagraphQuery->item( 0 );
+                                       if ( $elem->tagName === 'ol' || 
$elem->tagName === 'ul' ) {
+                                               $listElementAfterParagraph = 
$elem;
+                                       }
+                               }
+
+                               $leadSectionBody->insertBefore( $firstP, $where 
);
+                               if ( $listElementAfterParagraph !== null ) {
+                                       $leadSectionBody->insertBefore( 
$listElementAfterParagraph, $where );
+                               }
                        }
                }
                /**
diff --git a/tests/phpunit/MobileFormatterTest.php 
b/tests/phpunit/MobileFormatterTest.php
index c7a3169..bece1bd 100644
--- a/tests/phpunit/MobileFormatterTest.php
+++ b/tests/phpunit/MobileFormatterTest.php
@@ -585,6 +585,20 @@
                        ],
 
                        [
+                               // infobox, a paragraph, list element
+                               // @see 
https://phabricator.wikimedia.org/T149852
+                               '<table class="' . self::INFOBOX_CLASSNAME . 
'"><tr><td>infobox</td></tr></table>' .
+                               '<p>paragraph</p>' .
+                               '<ol><li>item 1</li><li>item 2</li></ol>',
+
+                               $this->makeSectionHtml(
+                                       0,
+                                       '<p>paragraph</p><ol><li>item 
1</li><li>item 2</li></ol>' .
+                                       '<table class="' . 
self::INFOBOX_CLASSNAME . '"><tr><td>infobox</td></tr></table>'
+                                       ),
+                               $enableSections, false, false, false, true,
+                       ],
+                       [
                                // 2 hat-notes, ambox, 2 infoboxes, 2 
paragraphs, another section
                                '<div class="' . self::HATNOTE_CLASSNAME . 
'">hatnote</div>' .
                                '<div class="' . self::HATNOTE_CLASSNAME . 
'">hatnote</div>' .
@@ -593,6 +607,7 @@
                                '<table class="' . self::INFOBOX_CLASSNAME . 
'"><tr><td>infobox 2</td></tr></table>' .
                                '<p>paragraph 1</p>' .
                                '<p>paragraph 2</p>' .
+                               '<ul><li>item</li></ul>'.
                                '<h2>Heading 1</h2>' .
                                '<p>paragraph 3</p>',
 
@@ -604,7 +619,7 @@
                                        '<p>paragraph 1</p>' .
                                        '<table class="' . 
self::INFOBOX_CLASSNAME . '"><tr><td>infobox 1</td></tr></table>' .
                                        '<table class="' . 
self::INFOBOX_CLASSNAME . '"><tr><td>infobox 2</td></tr></table>' .
-                                       '<p>paragraph 2</p>'
+                                       '<p>paragraph 
2</p><ul><li>item</li></ul>'
                                ) .
                                $this->makeSectionHeading( 'h2', 'Heading 1' ) .
                                $this->makeSectionHtml(
@@ -936,4 +951,5 @@
                $this->setLogger( 'mobile', $loggerMock );
                $formatter->filterContent( false, false, false, true );
        }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4257dbdf05cdb99634bdbda0f25ddde05e7cb9c5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Pmiazga <pmia...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to