Changeset 63566
- Timestamp:
- 09/09/2026 07:38:38 PM (2 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/general-template.php (modified) (9 diffs)
-
tests/phpunit/tests/general/wpGetTooltip.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/general-template.php
r63491 r63566 391 391 * generated unique ID. 392 392 * @type string $button Existing `button` or `a` markup. Used instead of generated button. 393 * Default standard button HTML.393 * Default empty string. 394 394 * @type string $label Not used for tooltips. 395 395 * @type string $close_label Not used for tooltips. … … 421 421 * generated unique ID. 422 422 * @type string $button Existing `button` markup. Used instead of generated button. 423 * Default standard button HTML.423 * Default empty string. 424 424 * @type string $label Accessible label for the toggle button. 425 425 * Default 'Help', matching the default icon. … … 456 456 * generated unique ID. 457 457 * @type string $button Existing `button` or `a` markup. Used instead of generated button. 458 * Default standard button HTML.458 * Default empty string. 459 459 * @type string $label Accessible label for the toggle button. 460 460 * Default 'Help', matching the default icon. … … 480 480 $defaults = array( 481 481 'id' => wp_unique_id( 'wp-tooltip-' ), 482 'button' => ' <button type="button" aria-label="%3$s"><span class="dashicons %4$s" aria-hidden="true"></span></button>',482 'button' => '', 483 483 'label' => __( 'Help' ), 484 484 'close_label' => __( 'Close' ), … … 495 495 } 496 496 497 $icon = ( $args['icon'] ) ? trim( $args['icon'] ) : $defaults['icon']; 498 $id = ( $args['id'] ) ? $args['id'] : $defaults['id']; 499 $button = ( $args['button'] ) ? $args['button'] : $defaults['button']; 500 $processed = false; 497 $icon = ( $args['icon'] ) ? trim( $args['icon'] ) : $defaults['icon']; 498 $id = ( $args['id'] ) ? $args['id'] : $defaults['id']; 499 500 // Tooltips use the content as the accessible name; toggletips use the label. 501 $label = ( 'tooltip' === $args['type'] ) ? wp_strip_all_tags( $content, true ) : $args['label']; 502 503 /* 504 * The generated button is a plain skeleton. Every dynamic attribute is 505 * added through the tag processor below, so caller-supplied markup is 506 * never scanned or substituted and a percent sign in custom markup, 507 * such as a percent-encoded URL, is never treated as a conversion 508 * specification. 509 */ 510 $default_button = '<button type="button"><span></span></button>'; 511 512 $is_default = ! $args['button']; 513 $button = ( $args['button'] ) ? $args['button'] : $default_button; 514 515 // The accepted root element is a `button`, or an `a` for tooltips. 516 $tag = false; 501 517 $processor = new WP_HTML_Tag_Processor( $button ); 502 518 if ( true === $processor->next_tag( 'button' ) ) { 503 $processor->add_class( 'wp-tooltip__toggle' ); 504 if ( 'tooltip' !== $args['type'] ) { 505 $processor->set_attribute( 'popovertarget', '%2$s' ); 506 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 507 } 508 $button = $processor->get_updated_html(); 509 $processed = true; 519 $tag = 'button'; 510 520 } else { 511 // Reset processor.512 521 $processor = new WP_HTML_Tag_Processor( $button ); 513 if ( true === $processor->next_tag( 'a' ) && 'tooltip' === $args['type'] ) { 514 $processor->add_class( 'wp-tooltip__toggle' ); 515 $button = $processor->get_updated_html(); 516 $processed = true; 517 } 518 } 519 if ( ! $processed ) { 520 // Button HTML passed was not valid. 521 $processor = new WP_HTML_Tag_Processor( $defaults['button'] ); 522 $processor->add_class( 'wp-tooltip__toggle' ); 523 if ( 'tooltip' !== $args['type'] ) { 524 $processor->set_attribute( 'popovertarget', '%2$s' ); 525 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 526 } 527 $button = $processor->get_updated_html(); 528 } 522 if ( 'tooltip' === $args['type'] && true === $processor->next_tag( 'a' ) ) { 523 $tag = 'a'; 524 } 525 } 526 527 if ( false === $tag ) { 528 // Button HTML passed was not valid. Reset to default. 529 $is_default = true; 530 $button = $default_button; 531 $processor = new WP_HTML_Tag_Processor( $button ); 532 $processor->next_tag( 'button' ); 533 $tag = 'button'; 534 } 535 536 /* 537 * Attributes that apply to every accepted button are added in one pass. 538 * Attributes that name the control are only added when the caller's 539 * markup did not already provide them. 540 */ 541 if ( null === $processor->get_attribute( 'aria-label' ) ) { 542 $processor->set_attribute( 'aria-label', $label ); 543 } 544 $processor->add_class( 'wp-tooltip__toggle' ); 545 if ( 'button' === $tag && 'tooltip' !== $args['type'] ) { 546 $processor->set_attribute( 'popovertarget', $id ); 547 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 548 } 549 550 // The generated button also carries the dashicon on its inner span. 551 if ( $is_default && true === $processor->next_tag( 'span' ) ) { 552 $processor->set_attribute( 'class', 'dashicons ' . $icon ); 553 $processor->set_attribute( 'aria-hidden', 'true' ); 554 } 555 556 $button = $processor->get_updated_html(); 529 557 530 558 /* … … 535 563 */ 536 564 if ( 'tooltip' === $args['type'] ) { 537 // Tooltips are only used to visually display labels.538 $label = wp_strip_all_tags( $content, true );539 565 $markup = sprintf( 540 566 '<span class="%1$s"> 541 ' . $button . '567 %6$s 542 568 <span popover="hint" id="%2$s" class="wp-tooltip__bubble" role="tooltip">' . 543 569 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span>' . … … 549 575 esc_attr( $icon ), 550 576 esc_html( $content ), 577 $button, 551 578 ); 552 579 } else { … … 558 585 $markup = sprintf( 559 586 '<span class="%1$s"> 560 ' . $button . '587 %7$s 561 588 <span popover="auto" id="%2$s" class="wp-tooltip__bubble" role="dialog" aria-label="%3$s" tabindex="-1" autofocus>' . 562 589 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span>' . … … 568 595 esc_attr( $classes ), 569 596 esc_attr( $id ), 570 esc_attr( $ args['label']),597 esc_attr( $label ), 571 598 esc_attr( $icon ), 572 599 esc_html( $content ), 573 600 esc_attr( $args['close_label'] ), 601 $button, 574 602 ); 575 603 } -
trunk/tests/phpunit/tests/general/wpGetTooltip.php
r62816 r63566 112 112 113 113 /** 114 * Tests that a custom button without an accessible name gains one, and 115 * that a caller-supplied aria-label is never overwritten. 116 * 117 * @ticket 65914 118 */ 119 public function test_wp_get_tooltip_custom_button_aria_label() { 120 $html = wp_get_tooltip( 121 'Helpful text.', 122 array( 123 'button' => '<button type="button"><span aria-hidden="true">?</span></button>', 124 ) 125 ); 126 127 $this->assertStringContainsString( 'aria-label="Helpful text."', $html, 'A custom button without an accessible name should gain one from the content.' ); 128 129 $html2 = wp_get_tooltip( 130 'Helpful text.', 131 array( 132 'button' => '<button type="button" aria-label="Custom name">?</button>', 133 ) 134 ); 135 136 $this->assertStringContainsString( 'aria-label="Custom name"', $html2, 'A caller-supplied aria-label should be preserved.' ); 137 $this->assertStringNotContainsString( 'aria-label="Helpful text."', $html2, 'The generated label should not overwrite the caller-supplied one.' ); 138 } 139 140 /** 114 141 * Tests that the accessible labels are output and escaped in attributes. 115 142 * … … 208 235 209 236 /** 237 * Tests that a percent sign in the button markup is not treated as a 238 * `sprintf()` conversion specification. 239 * 240 * @ticket 65914 241 * 242 * @dataProvider data_button_markup_containing_percent_signs 243 * 244 * @param string $button Button markup containing a percent sign. 245 * @param string $expected Substring that must be preserved in the output. 246 */ 247 public function test_wp_get_tooltip_preserves_percent_signs_in_button_markup( $button, $expected ) { 248 $this->assertStringContainsString( 249 $expected, 250 wp_get_tooltip( 'Helpful text.', array( 'button' => $button ) ), 251 'The tooltip did not preserve the percent sign.' 252 ); 253 $this->assertStringContainsString( 254 $expected, 255 wp_get_toggletip( 'Helpful text.', array( 'button' => $button ) ), 256 'The toggletip did not preserve the percent sign.' 257 ); 258 } 259 260 /** 261 * Tests that a percent-encoded URL in anchor markup is not rewritten. 262 * 263 * @ticket 65914 264 * 265 * @dataProvider data_percent_encoded_hrefs 266 * 267 * @param string $href A percent-encoded URL. 268 */ 269 public function test_wp_get_tooltip_preserves_percent_encoding_in_anchor_href( $href ) { 270 $html = wp_get_tooltip( 271 'Helpful text.', 272 array( 'button' => '<a href="' . $href . '">Search</a>' ) 273 ); 274 275 $this->assertStringContainsString( 'href="' . $href . '"', $html ); 276 } 277 278 /** 279 * Data provider. 280 * 281 * @return array[] 282 */ 283 public function data_percent_encoded_hrefs() { 284 return array( 285 'an encoded space' => array( '/wp-admin/edit.php?s=hello%20world' ), 286 'an encoded slash' => array( '/x?p=a%2Fb' ), 287 ); 288 } 289 290 /** 291 * Data provider. 292 * 293 * @return array[] 294 */ 295 public function data_button_markup_containing_percent_signs() { 296 return array( 297 'a literal percent sign' => array( 298 '<button type="button">100% done</button>', 299 '100% done', 300 ), 301 'a percent sign then a word' => array( 302 '<button type="button">Save 20%!</button>', 303 'Save 20%!', 304 ), 305 'an unknown format specifier' => array( 306 '<button type="button">Buy %q now</button>', 307 'Buy %q now', 308 ), 309 'a percent sign in an ID' => array( 310 '<button type="button" aria-describedby="box_100%_complete-title">Move up</button>', 311 'aria-describedby="box_100%_complete-title"', 312 ), 313 'a percent-encoded ID' => array( 314 '<button type="button" aria-describedby="meta-%d0%b8%d0%b3%d1%80%d0%b0-title">Move up</button>', 315 'aria-describedby="meta-%d0%b8%d0%b3%d1%80%d0%b0-title"', 316 ), 317 'text resembling a argnum' => array( 318 '<button type="button">Use %2$s in your code</button>', 319 'Use %2$s in your code', 320 ), 321 ); 322 } 323 324 /** 210 325 * Data provider. 211 326 *
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)