From 96b6afb31dfcff463d1d56e0842c3faa093699cb Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 15 Aug 2016 11:26:44 -0400 Subject: [PATCH] TextContent: Normalize newlines in preSaveTransform() This does the same normalization of newlines that Parser::preSaveTransform() does. This should be appropriate for any text content type, especially considering that EditPage uses WebRequest::getText() which does a less-strict version of this same transformation. This also cleans up the code for doing that newline replacement to be a bit less verbose. Bug: T142805 Change-Id: I462afcda502f031a8b0360d982ce2398a0383a96 --- includes/content/TextContent.php | 3 ++- includes/parser/Parser.php | 6 +----- tests/phpunit/includes/content/TextContentTest.php | 5 +++++ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index 225522e92d..de650b9194 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -149,7 +149,7 @@ class TextContent extends AbstractContent { /** * Returns a Content object with pre-save transformations applied. - * This implementation just trims trailing whitespace. + * This implementation just trims trailing whitespace and normalizes newlines. * * @param Title $title * @param User $user @@ -160,6 +160,7 @@ class TextContent extends AbstractContent { public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { $text = $this->getNativeData(); $pst = rtrim( $text ); + $pst = str_replace( [ "\r\n", "\r" ], "\n", $pst ); return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() ); } diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 206ad00ef6..4f579a93be 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4364,11 +4364,7 @@ class Parser { $this->startParse( $title, $options, self::OT_WIKI, $clearState ); $this->setUser( $user ); - $pairs = [ - "\r\n" => "\n", - "\r" => "\n", - ]; - $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text ); + $text = str_replace( [ "\r\n", "\r" ], "\n", $text ); if ( $options->getPreSaveTransform() ) { $text = $this->pstPass2( $text, $user ); } diff --git a/tests/phpunit/includes/content/TextContentTest.php b/tests/phpunit/includes/content/TextContentTest.php index ac8342826c..b4ae765f72 100644 --- a/tests/phpunit/includes/content/TextContentTest.php +++ b/tests/phpunit/includes/content/TextContentTest.php @@ -102,6 +102,11 @@ class TextContentTest extends MediaWikiLangTestCase { " Foo \n ", ' Foo', ], + [ + # 2: newline normalization + "LF\n\nCRLF\r\n\r\nCR\r\rEND", + "LF\n\nCRLF\n\nCR\n\nEND", + ], ]; } -- 2.20.1