Skip to content

Commit 781fb28

Browse files
Script Loader: Consistently escape the style handle in WP_Styles::do_item().
Includes moving most of the escaping as late as possible when the `<link>` tag is being constructed. Follow-up to [29956], [36550], [43564], [46164]. Props georgestephanis, westonruter, azaozz, jonsurrell, XecurAbhijeet, SergeyBiryukov. See #30036. git-svn-id: https://develop.svn.wordpress.org/trunk@61084 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d12b496 commit 781fb28

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/wp-includes/class-wp-styles.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function do_item( $handle, $group = false ) {
194194
}
195195

196196
if ( isset( $obj->args ) ) {
197-
$media = esc_attr( $obj->args );
197+
$media = $obj->args;
198198
} else {
199199
$media = 'all';
200200
}
@@ -218,16 +218,16 @@ public function do_item( $handle, $group = false ) {
218218
}
219219

220220
$rel = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
221-
$title = isset( $obj->extra['title'] ) ? sprintf( " title='%s'", esc_attr( $obj->extra['title'] ) ) : '';
221+
$title = isset( $obj->extra['title'] ) ? $obj->extra['title'] : '';
222222

223223
$tag = sprintf(
224224
"<link rel='%s' id='%s-css'%s href='%s'%s media='%s' />\n",
225225
$rel,
226-
$handle,
227-
$title,
226+
esc_attr( $handle ),
227+
$title ? sprintf( " title='%s'", esc_attr( $title ) ) : '',
228228
$href,
229229
$this->type_attr,
230-
$media
230+
esc_attr( $media )
231231
);
232232

233233
/**
@@ -255,11 +255,11 @@ public function do_item( $handle, $group = false ) {
255255
$rtl_tag = sprintf(
256256
"<link rel='%s' id='%s-rtl-css'%s href='%s'%s media='%s' />\n",
257257
$rel,
258-
$handle,
259-
$title,
258+
esc_attr( $handle ),
259+
$title ? sprintf( " title='%s'", esc_attr( $title ) ) : '',
260260
$rtl_href,
261261
$this->type_attr,
262-
$media
262+
esc_attr( $media )
263263
);
264264

265265
/** This filter is documented in wp-includes/class-wp-styles.php */

tests/phpunit/tests/dependencies/styles.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ public function test_wp_enqueue_style_with_html5_support_does_not_contain_type_a
9191
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
9292
}
9393

94+
/**
95+
* Test assorted handles to make sure they are output correctly.
96+
*
97+
* @dataProvider data_awkward_handles_are_supported_consistently
98+
*
99+
* @ticket 30036
100+
*/
101+
public function test_awkward_handles_are_supported_consistently( $handle ) {
102+
wp_enqueue_style( $handle, 'example.com', array(), null );
103+
104+
$expected = "<link rel='stylesheet' id='$handle-css' href='http://example.com' type='text/css' media='all' />\n";
105+
106+
$this->assertSame( $expected, get_echo( 'wp_print_styles' ) );
107+
}
108+
109+
/**
110+
* Data provider.
111+
*
112+
* @return array<string, string[]>
113+
*/
114+
public function data_awkward_handles_are_supported_consistently() {
115+
return array(
116+
'some spaces' => array( 'with some spaces' ),
117+
'snowman' => array( 'with-☃-snowman' ),
118+
'trailing space' => array( 'with-trailing-space ' ),
119+
'leading space' => array( ' with-leading-space' ),
120+
'an "ironic" title' => array( 'an &quot;ironic&quot; title' ),
121+
);
122+
}
123+
94124
/**
95125
* Test the different protocol references in wp_enqueue_style
96126
*

0 commit comments

Comments
 (0)