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
/**
* 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
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() );
}
$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 );
}
" Foo \n ",
' Foo',
],
+ [
+ # 2: newline normalization
+ "LF\n\nCRLF\r\n\r\nCR\r\rEND",
+ "LF\n\nCRLF\n\nCR\n\nEND",
+ ],
];
}