forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpHtmlProcessorModifiableText.php
More file actions
128 lines (123 loc) · 4.4 KB
/
wpHtmlProcessorModifiableText.php
File metadata and controls
128 lines (123 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Unit tests covering WP_HTML_Processor modifiable text functionality.
*
* @package WordPress
* @subpackage HTML-API
* @group html-api
*
* @coversDefaultClass WP_HTML_Processor
*/
class Tests_HtmlApi_WpHtmlProcessorModifiableText extends WP_UnitTestCase {
/**
* TEXTAREA elements ignore the first newline in their content.
* Setting the modifiable text with a leading newline (or carriage return variants)
* should ensure that the leading newline is present in the resulting TEXTAREA.
*
* TEXTAREA are treated as atomic tags by the tag processor, so `set_modifiable_text()`
* is called directly on the TEXTAREA token.
*
* @ticket 64609
*
* @dataProvider data_modifiable_text_special_textarea
*
* @param string $set_text Text to set.
* @param string $expected_html Expected HTML output.
*/
public function test_modifiable_text_special_textarea( string $set_text, string $expected_html ) {
$processor = WP_HTML_Processor::create_fragment( '<textarea></textarea>' );
$processor->next_token();
$processor->set_modifiable_text( $set_text );
$this->assertSame(
strtr(
$set_text,
array(
"\r\n" => "\n",
"\r" => "\n",
)
),
$processor->get_modifiable_text(),
'Should have preserved or normalized the leading newline in the TEXTAREA content.'
);
$this->assertEqualHTML(
$expected_html,
$processor->get_updated_html(),
'<body>',
'Should have correctly output the TEXTAREA HTML.'
);
}
/**
* Data provider.
*
* @return array[]
*/
public static function data_modifiable_text_special_textarea() {
return array(
'Leading newline' => array(
"\nAFTER NEWLINE",
"<textarea>\n\nAFTER NEWLINE</textarea>",
),
'Leading carriage return' => array(
"\rCR",
"<textarea>\n\nCR</textarea>",
),
'Leading carriage return + newline' => array(
"\r\nCR-N",
"<textarea>\n\nCR-N</textarea>",
),
);
}
/**
* Ensures that `set_modifiable_text()` returns false for elements that are not special "atomic" elements.
*
* This includes atomic-like foreign elements (`<svg><textarea>`) as well as arbitrary HTML
* elements (`<div>`).
*
* @ticket 64751
* @dataProvider data_set_modifiable_fails_non_atomic_tags
*/
public function test_set_modifiable_fails_non_atomic_tags(
string $html,
string $expected_namespace,
string $expected_tag
) {
$processor = WP_HTML_Processor::create_fragment( $html );
while ( $processor->next_tag() && $expected_tag !== $processor->get_tag() ) {
continue;
}
$this->assertSame( $expected_tag, $processor->get_tag(), 'Failed to find target tag.' );
$this->assertSame( $expected_namespace, $processor->get_namespace(), 'Unexpected namespace.' );
$this->assertFalse(
$processor->set_modifiable_text( 'test' ),
"set_modifiable_text() should return false for {$expected_namespace}:{$expected_tag}."
);
$this->assertSame(
$html,
$processor->get_updated_html(),
'HTML should be unchanged after rejected set_modifiable_text().'
);
}
/**
* Data provider.
*/
public static function data_set_modifiable_fails_non_atomic_tags(): array {
return array(
// Plain HTML tags.
'html DIV' => array( '<div>', 'html', 'DIV' ),
// Foreign elements with non-atomic tags.
'svg PATH' => array( '<svg><path></path></svg>', 'svg', 'PATH' ),
'svg PATH (self-closing)' => array( '<svg><path /></svg>', 'svg', 'PATH' ),
'math MTEXT' => array( '<math><mtext></mtext></math>', 'math', 'MTEXT' ),
'math MSPACE (self-closing)' => array( '<math><mspace /></math>', 'math', 'MSPACE' ),
// Foreign elements with atomic-like tags.
'svg TEXTAREA' => array( '<svg><textarea></textarea></svg>', 'svg', 'TEXTAREA' ),
'svg TITLE' => array( '<svg><title></title></svg>', 'svg', 'TITLE' ),
'svg STYLE' => array( '<svg><style></style></svg>', 'svg', 'STYLE' ),
'svg SCRIPT' => array( '<svg><script></script></svg>', 'svg', 'SCRIPT' ),
'math TEXTAREA' => array( '<math><textarea></textarea></math>', 'math', 'TEXTAREA' ),
'math TITLE' => array( '<math><title></title></math>', 'math', 'TITLE' ),
'math STYLE' => array( '<math><style></style></math>', 'math', 'STYLE' ),
'math SCRIPT' => array( '<math><script></script></math>', 'math', 'SCRIPT' ),
);
}
}