From f4a1242db80362f791982587282393a7ac024856 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 18 Oct 2012 20:14:46 +0200 Subject: [PATCH] (Bug 41169) Apply rtrim on on preSaveTransfrom. Wikitext should not have trailing whitespace. Change-Id: I75eb1c4bef7217ec2b7440594e3fc4b68dc3c022 --- includes/content/TextContent.php | 16 ++++++++++++++++ includes/content/WikitextContent.php | 3 ++- tests/phpunit/includes/JavascriptContentTest.php | 3 +++ tests/phpunit/includes/TextContentTest.php | 9 +++++++-- tests/phpunit/includes/WikitextContentTest.php | 8 ++++++-- 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index b561b909e3..18dcebd905 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -116,6 +116,22 @@ class TextContent extends AbstractContent { return $this->getNativeData(); } + /** + * Returns a Content object with pre-save transformations applied. + * This implementation just trims trailing whitespace. + * + * @param $title Title + * @param $user User + * @param $popts ParserOptions + * @return Content + */ + public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { + $text = $this->getNativeData(); + $pst = rtrim( $text ); + + return ( $text === $pst ) ? $this : new WikitextContent( $pst ); + } + /** * Diff this content object with another content object.. * diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index 89a9fe9f53..8f1381fdce 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -116,8 +116,9 @@ class WikitextContent extends TextContent { $text = $this->getNativeData(); $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); + rtrim( $pst ); - return new WikitextContent( $pst ); + return ( $text === $pst ) ? $this : new WikitextContent( $pst ); } /** diff --git a/tests/phpunit/includes/JavascriptContentTest.php b/tests/phpunit/includes/JavascriptContentTest.php index b45caa2991..74652e6e0f 100644 --- a/tests/phpunit/includes/JavascriptContentTest.php +++ b/tests/phpunit/includes/JavascriptContentTest.php @@ -89,6 +89,9 @@ class JavascriptContentTest extends TextContentTest { array( 'hello \'\'this\'\' is ~~~', 'hello \'\'this\'\' is ~~~', ), + array( " Foo \n ", + " Foo", + ), ); } diff --git a/tests/phpunit/includes/TextContentTest.php b/tests/phpunit/includes/TextContentTest.php index ee17a75a98..6cd436ecae 100644 --- a/tests/phpunit/includes/TextContentTest.php +++ b/tests/phpunit/includes/TextContentTest.php @@ -50,8 +50,13 @@ class TextContentTest extends MediaWikiTestCase { public function dataPreSaveTransform() { return array( - array( 'hello this is ~~~', - "hello this is ~~~", + array( #0: no signature resolution + "hello this is ~~~", + "hello this is ~~~", + ), + array( #1: rtrim + " Foo \n ", + " Foo", ), ); } diff --git a/tests/phpunit/includes/WikitextContentTest.php b/tests/phpunit/includes/WikitextContentTest.php index b2d3bdff0e..c1332a6052 100644 --- a/tests/phpunit/includes/WikitextContentTest.php +++ b/tests/phpunit/includes/WikitextContentTest.php @@ -21,12 +21,12 @@ class WikitextContentTest extends TextContentTest { public function dataGetSecondaryDataUpdates() { return array( - array("WikitextContentTest_testGetSecondaryDataUpdates_1", + array( "WikitextContentTest_testGetSecondaryDataUpdates_1", CONTENT_MODEL_WIKITEXT, "hello ''world''\n", array( 'LinksUpdate' => array( 'mRecursive' => true, 'mLinks' => array() ) ) ), - array("WikitextContentTest_testGetSecondaryDataUpdates_2", + array( "WikitextContentTest_testGetSecondaryDataUpdates_2", CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n", array( 'LinksUpdate' => array( 'mRecursive' => true, 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) ) @@ -174,6 +174,10 @@ just a test" array( 'hello \'\'this\'\' is ~~~', 'hello \'\'this\'\' is ~~~', ), + array( // rtrim + " Foo \n ", + " Foo", + ), ); } -- 2.20.1