Client-side migration for empty li preservation
authorTim Starling <tstarling@wikimedia.org>
Wed, 14 Oct 2015 03:16:44 +0000 (23:16 -0400)
committerSubramanya Sastry <ssastry@wikimedia.org>
Wed, 28 Oct 2015 23:35:18 +0000 (23:35 +0000)
It is desirable in terms of user-friendly syntax to display an empty
list item if the user adds one to the source. However, we suspect that
this change will break the rendering of existing templates. So, preserve
the empty <li> element, but style it with display:none so that there is
no user-visible change. Changes can then be observed with a user script,
then eventually the CSS can be removed so that the desired behaviour will
be user visible.

This is imagined as a staged deployment of T89331, i.e. it is better to
resolve differences with Html5Depurate one at a time instead of
deploying it all at once.

The CSS module is specified in parser/MWTidy.php since the tidy driver
hierarchy is not meant to be so closely tied to the MW environment.

Bug: T49673
Change-Id: Ifb44b782c617240e3de73dcdf76c8737c7307d94

includes/parser/MWTidy.php
includes/parser/Parser.php
includes/tidy/RaggettWrapper.php
resources/Resources.php
resources/src/mediawiki/mediawiki.raggett.css [new file with mode: 0644]
tests/parser/parserTests.txt

index 3a2bb17..746e15b 100644 (file)
@@ -51,6 +51,24 @@ class MWTidy {
                return $driver->tidy( $text );
        }
 
+       /**
+        * Get CSS modules needed if HTML from the current driver is to be displayed.
+        *
+        * This is just a migration tool to allow some changes expected as part of
+        * Tidy replacement (T89331) to be exposed on the client side via user
+        * scripts, without actually replacing tidy. See T49673.
+        *
+        * @return array
+        */
+       public static function getModuleStyles() {
+               $driver = self::singleton();
+               if ( $driver && $driver instanceof MediaWiki\Tidy\RaggettBase ) {
+                       return array( 'mediawiki.raggett' );
+               } else {
+                       return array();
+               }
+       }
+
        /**
         * Check HTML for errors, used if $wgValidateAllHtml = true.
         *
index 9060756..cfbf0b4 100644 (file)
@@ -1340,6 +1340,7 @@ class Parser {
 
                if ( MWTidy::isEnabled() && $this->mOptions->getTidy() ) {
                        $text = MWTidy::tidy( $text );
+                       $this->mOutput->addModuleStyles( MWTidy::getModuleStyles() );
                } else {
                        # attempt to sanitize at least some nesting problems
                        # (bug #2702 and quite a few others)
index 3f549d0..4759023 100644 (file)
@@ -51,6 +51,11 @@ class RaggettWrapper {
                // we can trick Tidy into not stripping them out by including them in tidy's new-empty-tags config
                $wrappedtext = preg_replace( '!<(link|meta)([^>]*?)(/{0,1}>)!', '<html-$1$2$3', $wrappedtext );
 
+               // Preserve empty li elements (T49673) by abusing Tidy's datafld hack
+               // The whitespace class is as in TY_(InitMap)
+               $wrappedtext = preg_replace( "!<li>([ \r\n\t\f]*)</li>!",
+                       '<li datafld="" class="mw-empty-li">\1</li>', $wrappedtext );
+
                // Wrap the whole thing in a doctype and body for Tidy.
                $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' .
                        ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>' .
@@ -79,6 +84,9 @@ class RaggettWrapper {
                // Revert <html-{link,meta}> back to <{link,meta}>
                $text = preg_replace( '!<html-(link|meta)([^>]*?)(/{0,1}>)!', '<$1$2$3', $text );
 
+               // Remove datafld
+               $text = str_replace( '<li datafld=""', '<li', $text );
+
                // Restore the contents of placeholder tokens
                $text = $this->mTokens->replace( $text );
 
index 59ce155..b1b1541 100644 (file)
@@ -1318,6 +1318,9 @@ return array(
                'scripts' => 'resources/src/mediawiki/mediawiki.experiments.js',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'mediawiki.raggett' => array(
+               'styles' => 'resources/src/mediawiki/mediawiki.raggett.css'
+       ),
 
        /* MediaWiki Action */
 
diff --git a/resources/src/mediawiki/mediawiki.raggett.css b/resources/src/mediawiki/mediawiki.raggett.css
new file mode 100644 (file)
index 0000000..1e1e973
--- /dev/null
@@ -0,0 +1,3 @@
+.mw-empty-li {
+       display: none;
+}
index f245826..4e6c591 100644 (file)
@@ -26405,3 +26405,19 @@ B <ref group="X" name="b" />
 <ref name="b"><span id="Z">foo</span>bar</ref>
 </references>
 !! end
+
+!! test
+Empty LI (T49673)
+!! wikitext
+* a
+* 
+*
+* b
+!! html/php+tidy
+<ul>
+<li>a</li>
+<li class="mw-empty-li"></li>
+<li class="mw-empty-li"></li>
+<li>b</li>
+</ul>
+!! end