|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Unit tests covering WP_HTML_Processor modifiable text functionality. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage HTML-API |
| 7 | + * @group html-api |
| 8 | + * |
| 9 | + * @coversDefaultClass WP_HTML_Processor |
| 10 | + */ |
| 11 | +class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase { |
| 12 | + /** |
| 13 | + * TEXTAREA elements ignore the first newline in their content. |
| 14 | + * Setting the modifiable text with a leading newline (or carriage return variants) |
| 15 | + * should ensure that the leading newline is present in the resulting TEXTAREA. |
| 16 | + * |
| 17 | + * TEXTAREA are treated as atomic tags by the tag processor, so `set_modifiable_text()` |
| 18 | + * is called directly on the TEXTAREA token. |
| 19 | + * |
| 20 | + * @ticket 64609 |
| 21 | + * |
| 22 | + * @dataProvider data_modifiable_text_special_textarea |
| 23 | + * |
| 24 | + * @param string $set_text Text to set. |
| 25 | + * @param string $expected_html Expected HTML output. |
| 26 | + */ |
| 27 | + public function test_modifiable_text_special_textarea( string $set_text, string $expected_html ) { |
| 28 | + $processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' ); |
| 29 | + $processor->next_token(); |
| 30 | + $processor->set_modifiable_text( $set_text ); |
| 31 | + $this->assertSame( |
| 32 | + strtr( |
| 33 | + $set_text, |
| 34 | + array( |
| 35 | + "\r\n" => "\n", |
| 36 | + "\r" => "\n", |
| 37 | + ) |
| 38 | + ), |
| 39 | + $processor->get_modifiable_text(), |
| 40 | + 'Should have preserved or normalized the leading newline in the TEXTAREA content.' |
| 41 | + ); |
| 42 | + $this->assertEqualHTML( |
| 43 | + $expected_html, |
| 44 | + $processor->get_updated_html(), |
| 45 | + '<body>', |
| 46 | + 'Should have correctly output the TEXTAREA HTML.' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Data provider. |
| 52 | + * |
| 53 | + * @return array[] |
| 54 | + */ |
| 55 | + public static function data_modifiable_text_special_textarea() { |
| 56 | + return array( |
| 57 | + 'Leading newline' => array( |
| 58 | + "\nAFTER NEWLINE", |
| 59 | + "<textarea>\n\nAFTER NEWLINE</textarea>", |
| 60 | + ), |
| 61 | + 'Leading carriage return' => array( |
| 62 | + "\rCR", |
| 63 | + "<textarea>\n\nCR</textarea>", |
| 64 | + ), |
| 65 | + 'Leading carriage return + newline' => array( |
| 66 | + "\r\nCR-N", |
| 67 | + "<textarea>\n\nCR-N</textarea>", |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments