The code that normalizes line endings ("\r\n" and "\r" to "\n") and
trims trailing whitespace is buried in Parser::preSaveTransform(), and
was duplicated to TextContent in
96b6afb31dfcff, as non-wikitext content
models should still be normalizing line endings.
This splits the duplicated code into
TextContent::normalizeLineEndings(), and utilize it in the Parser.
Additionally, expand the documentation of
TextContent::preSaveTransform() to document that subclasses should make
sure they normalize line endings during the PST stage.
And remove a useless rtrim() call from WikitextContent that did nothing.
Change-Id: I9094c671d4bbd23d75436f8f1d682d6dd6e6d2fc
return $this;
}
- return new static( $this->beautifyJSON() );
+ return new static( self::normalizeLineEndings( $this->beautifyJSON() ) );
}
/**
}
}
+ /**
+ * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation
+ * as well as trim trailing whitespace
+ *
+ * This was formerly part of Parser::preSaveTransform, but
+ * for non-wikitext content models they probably still want
+ * to normalize line endings without all of the other PST
+ * changes.
+ *
+ * @since 1.28
+ * @param $text
+ * @return string
+ */
+ public static function normalizeLineEndings( $text ) {
+ return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
+ }
+
/**
* Returns a Content object with pre-save transformations applied.
- * This implementation just trims trailing whitespace and normalizes newlines.
+ *
+ * At a minimum, subclasses should make sure to call TextContent::normalizeLineEndings()
+ * either directly or part of Parser::preSaveTransform().
*
* @param Title $title
* @param User $user
*/
public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
$text = $this->getNativeData();
- $pst = rtrim( $text );
- $pst = str_replace( [ "\r\n", "\r" ], "\n", $pst );
+ $pst = self::normalizeLineEndings( $text );
return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
}
$text = $this->getNativeData();
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
- rtrim( $pst );
return ( $text === $pst ) ? $this : new static( $pst );
}
$this->startParse( $title, $options, self::OT_WIKI, $clearState );
$this->setUser( $user );
- $text = str_replace( [ "\r\n", "\r" ], "\n", $text );
+ // We still normalize line endings for backwards-compatibility
+ // with other code that just calls PST, but this should already
+ // be handled in TextContent subclasses
+ $text = TextContent::normalizeLineEndings( $text );
+
if ( $options->getPreSaveTransform() ) {
$text = $this->pstPass2( $text, $user );
}
$text = preg_replace( $p2, '[[\\1]]', $text );
}
- # Trim trailing whitespace
- $text = rtrim( $text );
-
return $text;
}
$this->assertEquals( $expectedNative, $converted->getNativeData() );
}
}
+
+ /**
+ * @covers TextContent::normalizeLineEndings
+ * @dataProvider provideNormalizeLineEndings
+ */
+ public function testNormalizeLineEndings( $input, $expected ) {
+ $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
+ }
+
+ public static function provideNormalizeLineEndings() {
+ return [
+ [
+ "Foo\r\nbar",
+ "Foo\nbar"
+ ],
+ [
+ "Foo\rbar",
+ "Foo\nbar"
+ ],
+ [
+ "Foobar\n ",
+ "Foobar"
+ ]
+ ];
+ }
+
}