Pmiazga has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/403223 )
Change subject: Hygiene: Improve infobox detection in MoveLeadParagraphTransform
......................................................................
Hygiene: Improve infobox detection in MoveLeadParagraphTransform
Changes:
- introduced new function matchElement() to match DOM elements by
tagname and classname
- YAGNI: instead of regex use simple text comparision, for now it
should fullfil all our requirements
- added missing DOMDocument import (for PHPDoc blocks)
Bug: T170006
Change-Id: I2457c5b2eff992810cdfaa1b6bbb201bd659409c
---
M includes/transforms/MoveLeadParagraphTransform.php
1 file changed, 18 insertions(+), 8 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/23/403223/1
diff --git a/includes/transforms/MoveLeadParagraphTransform.php
b/includes/transforms/MoveLeadParagraphTransform.php
index b2b1159..d2d5b35 100644
--- a/includes/transforms/MoveLeadParagraphTransform.php
+++ b/includes/transforms/MoveLeadParagraphTransform.php
@@ -5,6 +5,7 @@
use DOMXPath;
use MobileContext;
use DOMElement;
+use DOMDocument;
class MoveLeadParagraphTransform implements IMobileTransform {
/**
@@ -26,22 +27,31 @@
}
/**
+ * Helper function to verify that passed $node matched nodename and has
set required classname
+ * @param DOMElement $node Node to verify
+ * @param string $requiredNodeName Required tag name, has to be
lowercase
+ * @param string $requiredClass Required class name
+ * @return bool
+ */
+ private static function matchElement( DOMElement $node,
$requiredNodeName, $requiredClass ) {
+ $classes = explode( ' ', $node->getAttribute( 'class' ) );
+ return strtolower( $node->nodeName ) === $requiredNodeName
+ && in_array( $requiredClass, $classes );
+ }
+
+ /**
* Works out if the infobox is wrapped
* @param DomElement $node of infobox
- * @param string $wrapperClasses (optional) regular expression for
matching potential classes
+ * @param string $wrapperClass (optional) a required classname for
wrapper
* @return DomElement representing an unwrapped infobox or an element
that wraps the infobox
*/
- public static function getInfoboxContainer( $node, $wrapperClasses =
'/mw-stack/' ) {
+ public static function getInfoboxContainer( $node, $wrapperClass =
'mw-stack' ) {
$infobox = false;
// iterate to the top.
while ( $node->parentNode ) {
- $className = $node->getAttribute( 'class' );
- preg_match( $wrapperClasses, $className, $matches );
- if (
- strpos( $className, 'infobox' ) !== false ||
- !empty( $matches )
- ) {
+ if ( self::matchElement( $node, 'table', 'infobox' ) ||
+ self::matchElement( $node, 'div', $wrapperClass
) ) {
$infobox = $node;
}
$node = $node->parentNode;
--
To view, visit https://gerrit.wikimedia.org/r/403223
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2457c5b2eff992810cdfaa1b6bbb201bd659409c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: specialpages
Gerrit-Owner: Pmiazga <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits