Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 32 additions & 45 deletions tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,56 @@
class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase {
/**
* TEXTAREA elements ignore the first newline in their content.
* Setting the modifiable text with a leading newline should ensure that the leading newline
* is present in the resulting TEXTAREA.
* Setting the modifiable text with a leading newline (or carriage return variants)
* should ensure that the leading newline is present in the resulting TEXTAREA.
*
* @ticket 64609
*/
public function test_modifiable_text_special_textarea() {
$processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' );
$processor->next_token();
$processor->set_modifiable_text( "\nAFTER NEWLINE" );
$this->assertSame(
"\nAFTER NEWLINE",
$processor->get_modifiable_text(),
'Should have preserved the leading newline in the TEXTAREA content.'
);
}

/**
* TEXTAREA elements ignore the first newline in their content.
* Setting the modifiable text with a leading carriage return should be normalized
* and ensure the leading newline is present in the resulting TEXTAREA.
*
* @ticket 64609
* @dataProvider data_modifiable_text_special_textarea
*
* @param string $set_text Text to set.
* @param string $expected_text Expected text after normalization.
* @param string $expected_html Expected HTML output.
*/
public function test_modifiable_text_special_textarea_carriage_return() {
public function test_modifiable_text_special_textarea( string $set_text, string $expected_text, string $expected_html ) {
$processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' );
$processor->next_token();
$processor->set_modifiable_text( "\rCR" );
// Newline normalization transforms \r into \n, and special handling should preserve it.
$processor->set_modifiable_text( $set_text );
$this->assertSame(
"\nCR",
$expected_text,
$processor->get_modifiable_text(),
'Should have normalized carriage return and preserved the leading newline in the TEXTAREA content.'
'Should have preserved or normalized the leading newline in the TEXTAREA content.'
);
$this->assertEqualHTML(
"<textarea>\n\nCR</textarea>",
$expected_html,
$processor->get_updated_html(),
'<body>',
'Should have doubled the newline in the output HTML to preserve the leading newline.'
'Should have correctly output the TEXTAREA HTML.'
);
}

/**
* TEXTAREA elements ignore the first newline in their content.
* Setting the modifiable text with a leading carriage return + newline should be normalized
* and ensure the leading newline is present in the resulting TEXTAREA.
* Data provider for test_modifiable_text_special_textarea().
*
* @ticket 64609
* @return array[]
*/
public function test_modifiable_text_special_textarea_carriage_return_newline() {
$processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' );
$processor->next_token();
$processor->set_modifiable_text( "\r\nCR-N" );
// Newline normalization transforms \r\n into \n, and special handling should preserve it.
$this->assertSame(
"\nCR-N",
$processor->get_modifiable_text(),
'Should have normalized carriage return + newline and preserved the leading newline in the TEXTAREA content.'
);
$this->assertEqualHTML(
"<textarea>\n\nCR-N</textarea>",
$processor->get_updated_html(),
'<body>',
'Should have doubled the newline in the output HTML to preserve the leading newline.'
public static function data_modifiable_text_special_textarea() {
return array(
'Leading newline' => array(
"\nAFTER NEWLINE",
"\nAFTER NEWLINE",
"<textarea>\n\nAFTER NEWLINE</textarea>",
),
'Leading carriage return' => array(
"\rCR",
"\nCR",
"<textarea>\n\nCR</textarea>",
),
'Leading carriage return + newline' => array(
"\r\nCR-N",
"\nCR-N",
"<textarea>\n\nCR-N</textarea>",
),
);
}

Expand Down