Wrap parser output in <div class="mw-parser-output">
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 27 Apr 2017 16:58:17 +0000 (12:58 -0400)
committerTim Starling <tstarling@wikimedia.org>
Mon, 8 May 2017 05:32:03 +0000 (05:32 +0000)
This will allow CSS to target just the parser output, without also
accidentally targeting the edit form, diff tables, and so on.

Bug: T37247
Change-Id: If4eb5bf71f94fa366ec4eddb6964e8f4df6b824a
Depends-On: I330c6aa4aaee045614b1801ed34bc9e03be69650
Depends-On: I52a518fa44e017841fe78474012cd69823e0a41d

13 files changed:
RELEASE-NOTES-1.30
includes/api/ApiParse.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
includes/cache/MessageCache.php
includes/parser/Parser.php
includes/parser/ParserOptions.php
tests/parser/ParserTestRunner.php
tests/parser/parserTests.txt
tests/phpunit/includes/ExtraParserTest.php
tests/phpunit/includes/content/WikitextContentTest.php
tests/phpunit/includes/page/WikiPageTest.php
tests/phpunit/includes/parser/TagHooksTest.php

index cdf8ba4..1351c00 100644 (file)
@@ -14,7 +14,9 @@ production.
   documentation of $wgShellLocale for details.
 
 === New features in 1.30 ===
-* …
+* (T37247) Output from Parser::parse() will now be wrapped in a div with
+  class="mw-parser-output" by default. This may be changed or disabled using
+  ParserOptions::setWrapOutputClass().
 
 === External library changes in 1.30 ===
 
@@ -31,7 +33,9 @@ production.
 * …
 
 === Action API changes in 1.30 ===
-* …
+* (T37247) action=parse output will be wrapped in a div with
+  class="mw-parser-output" by default. This may be changed or disabled using
+  the new 'wrapoutputclass' parameter.
 
 === Action API internal changes in 1.30 ===
 * …
index d648968..7d22d9c 100644 (file)
@@ -478,6 +478,9 @@ class ApiParse extends ApiBase {
                if ( $params['disabletidy'] ) {
                        $popts->setTidy( false );
                }
+               $popts->setWrapOutputClass(
+                       $params['wrapoutputclass'] === '' ? false : $params['wrapoutputclass']
+               );
 
                $reset = null;
                $suppressCache = false;
@@ -788,6 +791,7 @@ class ApiParse extends ApiBase {
                                        'parsetree' => [ 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT ],
                                ],
                        ],
+                       'wrapoutputclass' => 'mw-parser-output',
                        'pst' => false,
                        'onlypst' => false,
                        'effectivelanglinks' => false,
index 7a04caf..387e4b6 100644 (file)
        "apihelp-parse-paramvalue-prop-limitreporthtml": "Gives the HTML version of the limit report. Gives no data, when <var>$1disablelimitreport</var> is set.",
        "apihelp-parse-paramvalue-prop-parsetree": "The XML parse tree of revision content (requires content model <code>$1</code>)",
        "apihelp-parse-paramvalue-prop-parsewarnings": "Gives the warnings that occurred while parsing content.",
+       "apihelp-parse-param-wrapoutputclass": "CSS class to use to wrap the parser output.",
        "apihelp-parse-param-pst": "Do a pre-save transform on the input before parsing it. Only valid when used with text.",
        "apihelp-parse-param-onlypst": "Do a pre-save transform (PST) on the input, but don't parse it. Returns the same wikitext, after a PST has been applied. Only valid when used with <var>$1text</var>.",
        "apihelp-parse-param-effectivelanglinks": "Includes language links supplied by extensions (for use with <kbd>$1prop=langlinks</kbd>).",
index 6e70653..81adeec 100644 (file)
        "apihelp-parse-paramvalue-prop-limitreporthtml": "{{doc-apihelp-paramvalue|parse|prop|limitreporthtml}}",
        "apihelp-parse-paramvalue-prop-parsetree": "{{doc-apihelp-paramvalue|parse|prop|parsetree|params=* $1 - Value of the constant CONTENT_MODEL_WIKITEXT|paramstart=2}}",
        "apihelp-parse-paramvalue-prop-parsewarnings": "{{doc-apihelp-paramvalue|parse|prop|parsewarnings}}",
+       "apihelp-parse-param-wrapoutputclass": "{{doc-apihelp-param|parse|wrapoutputclass}}",
        "apihelp-parse-param-pst": "{{doc-apihelp-param|parse|pst}}",
        "apihelp-parse-param-onlypst": "{{doc-apihelp-param|parse|onlypst}}",
        "apihelp-parse-param-effectivelanglinks": "{{doc-apihelp-param|parse|effectivelanglinks}}",
index 355aff4..5caa7d5 100644 (file)
@@ -193,6 +193,7 @@ class MessageCache {
                                $po = ParserOptions::newFromAnon();
                                $po->setEditSection( false );
                                $po->setAllowUnsafeRawHtml( false );
+                               $po->setWrapOutputClass( false );
                                return $po;
                        }
 
@@ -202,6 +203,11 @@ class MessageCache {
                        // from malicious sources. As a precaution, disable
                        // the <html> parser tag when parsing messages.
                        $this->mParserOptions->setAllowUnsafeRawHtml( false );
+                       // Wrapping messages in an extra <div> is probably not expected. If
+                       // they're outside the content area they probably shouldn't be
+                       // targeted by CSS that's targeting the parser output, and if
+                       // they're inside they already are from the outer div.
+                       $this->mParserOptions->setWrapOutputClass( false );
                }
 
                return $this->mParserOptions;
index 5b1e86d..ecee0e2 100644 (file)
@@ -589,6 +589,14 @@ class Parser {
                                        $this->mTitle->getPrefixedDBkey() );
                        }
                }
+
+               # Wrap non-interface parser output in a <div> so it can be targeted
+               # with CSS (T37247)
+               $class = $this->mOptions->getWrapOutputClass();
+               if ( $class !== false && !$this->mOptions->getInterfaceMessage() ) {
+                       $text = Html::rawElement( 'div', [ 'class' => $class ], $text );
+               }
+
                $this->mOutput->setText( $text );
 
                $this->mRevisionId = $oldRevisionId;
index 2cdd8c7..d4d1042 100644 (file)
@@ -258,6 +258,12 @@ class ParserOptions {
         */
        private $allowUnsafeRawHtml = true;
 
+       /**
+        * CSS class to use to wrap output from Parser::parse().
+        * @var string|false
+        */
+       private $wrapOutputClass = 'mw-parser-output';
+
        public function getInterwikiMagic() {
                return $this->mInterwikiMagic;
        }
@@ -481,6 +487,15 @@ class ParserOptions {
                return $this->allowUnsafeRawHtml;
        }
 
+       /**
+        * Class to use to wrap output from Parser::parse()
+        * @since 1.30
+        * @return string|bool
+        */
+       public function getWrapOutputClass() {
+               return $this->wrapOutputClass;
+       }
+
        public function setInterwikiMagic( $x ) {
                return wfSetVar( $this->mInterwikiMagic, $x );
        }
@@ -629,6 +644,19 @@ class ParserOptions {
                return wfSetVar( $this->allowUnsafeRawHtml, $x );
        }
 
+       /**
+        * CSS class to use to wrap output from Parser::parse()
+        * @since 1.30
+        * @param string|bool $className Set false to disable wrapping.
+        * @return string|bool Current value
+        */
+       public function setWrapOutputClass( $className ) {
+               if ( $className === true ) { // DWIM, they probably want the default class name
+                       $className = 'mw-parser-output';
+               }
+               return wfSetVar( $this->wrapOutputClass, $className );
+       }
+
        /**
         * Set the redirect target.
         *
index f100411..f44b0d5 100644 (file)
@@ -747,6 +747,10 @@ class ParserTestRunner {
                $user = $context->getUser();
                $options = ParserOptions::newFromContext( $context );
 
+               if ( !isset( $opts['wrap'] ) ) {
+                       $options->setWrapOutputClass( false );
+               }
+
                if ( isset( $opts['tidy'] ) ) {
                        if ( !$this->tidySupport->isEnabled() ) {
                                $this->recorder->skipped( $test, 'tidy extension is not installed' );
index e12c136..6477356 100644 (file)
@@ -32,6 +32,7 @@
 # local         format section links in edit comment text as local links
 # notoc         disable table of contents
 # thumbsize=NNN set the default thumb size to NNNpx for this test
+# wrap          include the normal wrapper <div class="mw-parser-output"> (since 1.30)
 #
 # You can also set the following parser properties via test options:
 #  wgEnableUploads, wgAllowExternalImages, wgMaxTocLevel,
index 4e95a30..a4e3bb9 100644 (file)
@@ -26,6 +26,7 @@ class ExtraParserTest extends MediaWikiTestCase {
                // FIXME: This test should pass without setting global content language
                $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
                $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
+               $this->options->setWrapOutputClass( false );
                $this->parser = new Parser;
 
                MagicWord::clearCache();
@@ -40,6 +41,7 @@ class ExtraParserTest extends MediaWikiTestCase {
 
                $title = Title::newFromText( 'Unit test' );
                $options = ParserOptions::newFromUser( new User() );
+               $options->setWrapOutputClass( false );
                $this->assertEquals( "<p>$longLine</p>",
                        $this->parser->parse( $longLine, $title, $options )->getText() );
        }
index 4c69d87..b9ce997 100644 (file)
@@ -29,7 +29,7 @@ more stuff
                                "WikitextContentTest_testGetParserOutput",
                                CONTENT_MODEL_WIKITEXT,
                                "hello ''world''\n",
-                               "<p>hello <i>world</i>\n</p>"
+                               "<div class=\"mw-parser-output\"><p>hello <i>world</i>\n</p>\n\n\n</div>"
                        ],
                        // TODO: more...?
                ];
index 6b911bf..556a348 100644 (file)
@@ -549,7 +549,11 @@ class WikiPageTest extends MediaWikiLangTestCase {
 
        public static function provideGetParserOutput() {
                return [
-                       [ CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ],
+                       [
+                               CONTENT_MODEL_WIKITEXT,
+                               "hello ''world''\n",
+                               "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
+                       ],
                        // @todo more...?
                ];
        }
@@ -566,7 +570,7 @@ class WikiPageTest extends MediaWikiLangTestCase {
                $text = $po->getText();
 
                $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
-               $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
+               $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
 
                $this->assertEquals( $expectedHtml, $text );
 
index 12936ee..06fe272 100644 (file)
@@ -43,18 +43,25 @@ class TagHookTest extends MediaWikiTestCase {
                return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
        }
 
+       private function getParserOptions() {
+               global $wgContLang;
+               $popt = ParserOptions::newFromUserAndLang( new User, $wgContLang );
+               $popt->setWrapOutputClass( false );
+               return $popt;
+       }
+
        /**
         * @dataProvider provideValidNames
         */
        public function testTagHooks( $tag ) {
-               global $wgParserConf, $wgContLang;
+               global $wgParserConf;
                $parser = new Parser( $wgParserConf );
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parserOutput = $parser->parse(
                        "Foo<$tag>Bar</$tag>Baz",
                        Title::newFromText( 'Test' ),
-                       ParserOptions::newFromUserAndLang( new User, $wgContLang )
+                       $this->getParserOptions()
                );
                $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
 
@@ -66,14 +73,14 @@ class TagHookTest extends MediaWikiTestCase {
         * @expectedException MWException
         */
        public function testBadTagHooks( $tag ) {
-               global $wgParserConf, $wgContLang;
+               global $wgParserConf;
                $parser = new Parser( $wgParserConf );
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parser->parse(
                        "Foo<$tag>Bar</$tag>Baz",
                        Title::newFromText( 'Test' ),
-                       ParserOptions::newFromUserAndLang( new User, $wgContLang )
+                       $this->getParserOptions()
                );
                $this->fail( 'Exception not thrown.' );
        }
@@ -82,14 +89,14 @@ class TagHookTest extends MediaWikiTestCase {
         * @dataProvider provideValidNames
         */
        public function testFunctionTagHooks( $tag ) {
-               global $wgParserConf, $wgContLang;
+               global $wgParserConf;
                $parser = new Parser( $wgParserConf );
 
                $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
                $parserOutput = $parser->parse(
                        "Foo<$tag>Bar</$tag>Baz",
                        Title::newFromText( 'Test' ),
-                       ParserOptions::newFromUserAndLang( new User, $wgContLang )
+                       $this->getParserOptions()
                );
                $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
 
@@ -101,7 +108,7 @@ class TagHookTest extends MediaWikiTestCase {
         * @expectedException MWException
         */
        public function testBadFunctionTagHooks( $tag ) {
-               global $wgParserConf, $wgContLang;
+               global $wgParserConf;
                $parser = new Parser( $wgParserConf );
 
                $parser->setFunctionTagHook(
@@ -112,7 +119,7 @@ class TagHookTest extends MediaWikiTestCase {
                $parser->parse(
                        "Foo<$tag>Bar</$tag>Baz",
                        Title::newFromText( 'Test' ),
-                       ParserOptions::newFromUserAndLang( new User, $wgContLang )
+                       $this->getParserOptions()
                );
                $this->fail( 'Exception not thrown.' );
        }