Skip to content

Commit 954dbf5

Browse files
committed
Further simplify tests
1 parent b61fc6d commit 954dbf5

1 file changed

Lines changed: 34 additions & 68 deletions

File tree

tests/phpunit/tests/html-api/wpHtmlProcessorModifiableText.php

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase {
1414
* Setting the modifiable text with a leading newline (or carriage return variants)
1515
* should ensure that the leading newline is present in the resulting TEXTAREA.
1616
*
17+
* TEXTAREA are treated as atomic tags by the tag processor, so `set_modifiable_text()`
18+
* is called directly on the TEXTAREA token, making them different from PRE and LISTING
19+
* tags that also have special newline handling in HTML.
20+
*
1721
* @ticket 64609
1822
*
1923
* @dataProvider data_modifiable_text_special_textarea
@@ -51,15 +55,15 @@ public function test_modifiable_text_special_textarea( string $set_text, string
5155
*/
5256
public static function data_modifiable_text_special_textarea() {
5357
return array(
54-
'Leading newline' => array(
58+
'Leading newline' => array(
5559
"\nAFTER NEWLINE",
5660
"<textarea>\n\nAFTER NEWLINE</textarea>",
5761
),
58-
'Leading carriage return' => array(
62+
'Leading carriage return' => array(
5963
"\rCR",
6064
"<textarea>\n\nCR</textarea>",
6165
),
62-
'Leading carriage return + newline' => array(
66+
'Leading carriage return + newline' => array(
6367
"\r\nCR-N",
6468
"<textarea>\n\nCR-N</textarea>",
6569
),
@@ -68,56 +72,14 @@ public static function data_modifiable_text_special_textarea() {
6872

6973
/**
7074
* PRE and LISTING elements ignore the first newline in their content.
71-
* Setting the modifiable text with a leading newline should ensure that the leading newline
72-
* is present in the resulting element.
73-
*
74-
* @ticket 64609
75-
*
76-
* @dataProvider data_modifiable_text_special_pre_tags
77-
*
78-
* @param string $tag_name The tag name to test (e.g. 'pre', 'listing').
79-
*/
80-
public function test_modifiable_text_special_pre_tags( string $tag_name ) {
81-
$set_text = "\nAFTER NEWLINE";
82-
$processor = WP_HTML_Processor::create_fragment( "<{$tag_name}>REPLACEME<!--x--></{$tag_name}>" );
83-
$processor->next_tag();
84-
$processor->next_token();
85-
$this->assertSame( '#text', $processor->get_token_type() );
86-
$processor->set_modifiable_text( $set_text );
87-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
88-
$this->assertEqualHTML(
89-
<<<HTML
90-
<{$tag_name}>
91-
{$set_text}<!--x--></{$tag_name}>
92-
HTML,
93-
$processor->get_updated_html(),
94-
'<body>',
95-
"Should have preserved the leading newline in the {$tag_name} content."
96-
);
97-
}
98-
99-
/**
100-
* Data provider.
75+
* Leading whitespace may split into multiple text nodes in the HTML Processor.
76+
* Setting the modifiable text with a leading newline should ensure that the
77+
* leading newline is present in the resulting element.
10178
*
102-
* @return array[]
103-
*/
104-
public static function data_modifiable_text_special_pre_tags() {
105-
return array(
106-
'PRE' => array( 'pre' ),
107-
'LISTING' => array( 'listing' ),
108-
);
109-
}
110-
111-
/**
11279
* The HTML Processor has special behavior when a text node starts with whitespace.
11380
* Test that PRE and LISTING `::set_modifiable_text()` handling works correctly
11481
* with leading whitespace.
11582
*
116-
* PRE and LISTING elements ignore the first newline in their content.
117-
* Leading whitespace may split into multiple text nodes in the HTML Processor.
118-
* Setting the modifiable text with a leading newline should ensure that the
119-
* leading newline is present in the resulting element.
120-
*
12183
* @ticket 64609
12284
*
12385
* @dataProvider data_modifiable_text_special_leading_whitespace
@@ -167,61 +129,65 @@ public function test_modifiable_text_special_leading_whitespace(
167129
* Data provider.
168130
*/
169131
public static function data_modifiable_text_special_leading_whitespace() {
170-
$set_text = "\nAFTER NEWLINE.";
171-
172-
foreach ( self::data_modifiable_text_special_pre_tags() as $tag_data ) {
173-
$tag_name = $tag_data[0];
174-
$tag_label = strtoupper( $tag_name );
132+
$tags = array( 'pre', 'listing' );
133+
foreach ( $tags as $tag_name ) {
134+
yield "<{$tag_name}> with no leading newline" => array(
135+
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
136+
1,
137+
"REPLACEME",
138+
"\nAFTER NEWLINE.",
139+
"<{$tag_name}>\n\nAFTER NEWLINE.<!--x--></{$tag_name}>",
140+
);
175141

176-
yield "{$tag_label} with leading newline, first text node" => array(
142+
yield "<{$tag_name}> with leading newline, first text node" => array(
177143
"<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>",
178144
1,
179145
'',
180-
$set_text,
181-
"<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>",
146+
"\nAFTER NEWLINE.",
147+
"<{$tag_name}>\n\nAFTER NEWLINE.REPLACEME<!--x--></{$tag_name}>",
182148
);
183149

184-
yield "{$tag_label} with leading newline, second text node" => array(
150+
yield "<{$tag_name}> with leading newline, second text node" => array(
185151
"<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>",
186152
2,
187153
'REPLACEME',
188-
$set_text,
189-
"<{$tag_name}>\n{$set_text}<!--x--></{$tag_name}>",
154+
"\nAFTER NEWLINE.",
155+
"<{$tag_name}>\n\nAFTER NEWLINE.<!--x--></{$tag_name}>",
190156
);
191157

192-
yield "{$tag_label} with leading space, first text node" => array(
158+
yield "<{$tag_name}> with leading space, first text node" => array(
193159
"<{$tag_name}> REPLACEME<!--x--></{$tag_name}>",
194160
1,
195161
' ',
196-
$set_text,
197-
"<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>",
162+
"\nAFTER NEWLINE.",
163+
"<{$tag_name}>\n\nAFTER NEWLINE.REPLACEME<!--x--></{$tag_name}>",
198164
);
199165

200-
yield "{$tag_label} with leading space, second text node" => array(
166+
yield "<{$tag_name}> with leading space, second text node" => array(
201167
"<{$tag_name}> REPLACEME<!--x--></{$tag_name}>",
202168
2,
203169
'REPLACEME',
204-
$set_text,
205-
"<{$tag_name}>\n {$set_text}<!--x--></{$tag_name}>",
170+
"\nAFTER NEWLINE.",
171+
"<{$tag_name}>\n \nAFTER NEWLINE.<!--x--></{$tag_name}>",
206172
);
207173

208-
yield "{$tag_label} insert with leading carriage return" => array(
174+
yield "<{$tag_name}> insert with leading carriage return" => array(
209175
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
210176
1,
211177
'REPLACEME',
212178
"\rCR",
213179
"<{$tag_name}>\n\nCR<!--x--></{$tag_name}>",
214180
);
215181

216-
yield "{$tag_label} insert with leading carriage return + newline" => array(
182+
yield "<{$tag_name}> insert with leading carriage return + newline" => array(
217183
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
218184
1,
219185
'REPLACEME',
220186
"\r\nCR-N",
221187
"<{$tag_name}>\n\nCR-N<!--x--></{$tag_name}>",
222188
);
223189

224-
yield "{$tag_label} clear text" => array(
190+
yield "<{$tag_name}> clear text" => array(
225191
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
226192
1,
227193
'REPLACEME',

0 commit comments

Comments
 (0)