Skip to content

Commit 28d6b7f

Browse files
committed
Add carriage return tests
1 parent cc8537d commit 28d6b7f

1 file changed

Lines changed: 47 additions & 7 deletions

File tree

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

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,39 @@ public static function data_modifiable_text_special_pre_tags() {
8383
*
8484
* @dataProvider data_modifiable_text_special_leading_whitespace
8585
*
86-
* @param string $html HTML containing the element to test.
86+
* @param string $html HTML containing the element to test.
8787
* @param int $advance_n_tokens Count of times to run `next_token()` after `next_tag()`.
88-
* @param string $initial_text Expected modifiable text before the update.
89-
* @param string $expected_html Expected HTML output after setting modifiable text.
88+
* @param string $stopped_on_text Expected modifiable text before the update.
89+
* @param string $set_text Text to set.
90+
* @param string $expected_html Expected HTML output after setting modifiable text.
9091
*/
91-
public function test_modifiable_text_special_leading_whitespace( string $html, int $advance_n_tokens, string $initial_text, string $expected_html ) {
92-
$set_text = "\nAFTER NEWLINE.";
92+
public function test_modifiable_text_special_leading_whitespace(
93+
string $html,
94+
int $advance_n_tokens,
95+
string $stopped_on_text,
96+
string $set_text,
97+
string $expected_html
98+
) {
9399
$processor = WP_HTML_Processor::create_fragment( $html );
94100
$processor->next_tag();
95101
while ( --$advance_n_tokens >= 0 ) {
96102
$processor->next_token();
97103
}
98104
$this->assertSame( '#text', $processor->get_token_type() );
99-
$this->assertSame( $initial_text, $processor->get_modifiable_text() );
105+
$this->assertSame( $stopped_on_text, $processor->get_modifiable_text() );
100106
$processor->set_modifiable_text( $set_text );
101-
$this->assertSame( $set_text, $processor->get_modifiable_text() );
107+
108+
// Newline normalization transforms \r and \r\n into \n.
109+
$this->assertSame(
110+
strtr(
111+
$set_text,
112+
array(
113+
"\r\n" => "\n",
114+
"\r" => "\n",
115+
)
116+
),
117+
$processor->get_modifiable_text()
118+
);
102119
$this->assertEqualHTML(
103120
$expected_html,
104121
$processor->get_updated_html(),
@@ -121,26 +138,49 @@ public static function data_modifiable_text_special_leading_whitespace() {
121138
"<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>",
122139
1,
123140
'',
141+
$set_text,
124142
"<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>",
125143
);
144+
126145
yield "{$tag_label} with leading newline, second text node" => array(
127146
"<{$tag_name}>\nREPLACEME<!--x--></{$tag_name}>",
128147
2,
129148
'REPLACEME',
149+
$set_text,
130150
"<{$tag_name}>\n{$set_text}<!--x--></{$tag_name}>",
131151
);
152+
132153
yield "{$tag_label} with leading space, first text node" => array(
133154
"<{$tag_name}> REPLACEME<!--x--></{$tag_name}>",
134155
1,
135156
' ',
157+
$set_text,
136158
"<{$tag_name}>\n{$set_text}REPLACEME<!--x--></{$tag_name}>",
137159
);
160+
138161
yield "{$tag_label} with leading space, second text node" => array(
139162
"<{$tag_name}> REPLACEME<!--x--></{$tag_name}>",
140163
2,
141164
'REPLACEME',
165+
$set_text,
142166
"<{$tag_name}>\n {$set_text}<!--x--></{$tag_name}>",
143167
);
168+
169+
yield "{$tag_label} insert with leading carriage return" => array(
170+
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
171+
1,
172+
'REPLACEME',
173+
"\rCR",
174+
"<{$tag_name}>\n\nCR<!--x--></{$tag_name}>",
175+
);
176+
177+
yield "{$tag_label} insert with leading carriage return + newline" => array(
178+
"<{$tag_name}>REPLACEME<!--x--></{$tag_name}>",
179+
1,
180+
'REPLACEME',
181+
"\r\nCR-N",
182+
"<{$tag_name}>\n\nCR-N<!--x--></{$tag_name}>",
183+
);
144184
}
145185
}
146186
}

0 commit comments

Comments
 (0)