Changeset 63571
- Timestamp:
- 09/09/2026 10:25:57 PM (2 months ago)
- Location:
- branches/7.1
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
src/wp-includes/general-template.php (modified) (9 diffs)
-
tests/phpunit/tests/general/wpGetTooltip.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/7.1
-
branches/7.1/src/wp-includes/general-template.php
r62953 r63571 385 385 * generated unique ID. 386 386 * @type string $button Existing `button` or `a` markup. Used instead of generated button. 387 * Default standard button HTML.387 * Default empty string. 388 388 * @type string $label Not used for tooltips. 389 389 * @type string $close_label Not used for tooltips. … … 415 415 * generated unique ID. 416 416 * @type string $button Existing `button` markup. Used instead of generated button. 417 * Default standard button HTML.417 * Default empty string. 418 418 * @type string $label Accessible label for the toggle button. 419 419 * Default 'Help', matching the default icon. … … 450 450 * generated unique ID. 451 451 * @type string $button Existing `button` or `a` markup. Used instead of generated button. 452 * Default standard button HTML.452 * Default empty string. 453 453 * @type string $label Accessible label for the toggle button. 454 454 * Default 'Help', matching the default icon. … … 474 474 $defaults = array( 475 475 'id' => wp_unique_id( 'wp-tooltip-' ), 476 'button' => ' <button type="button" aria-label="%3$s"><span class="dashicons %4$s" aria-hidden="true"></span></button>',476 'button' => '', 477 477 'label' => __( 'Help' ), 478 478 'close_label' => __( 'Close' ), … … 489 489 } 490 490 491 $icon = ( $args['icon'] ) ? trim( $args['icon'] ) : $defaults['icon']; 492 $id = ( $args['id'] ) ? $args['id'] : $defaults['id']; 493 $button = ( $args['button'] ) ? $args['button'] : $defaults['button']; 494 $processed = false; 491 $icon = ( $args['icon'] ) ? trim( $args['icon'] ) : $defaults['icon']; 492 $id = ( $args['id'] ) ? $args['id'] : $defaults['id']; 493 494 // Tooltips use the content as the accessible name; toggletips use the label. 495 $label = ( 'tooltip' === $args['type'] ) ? wp_strip_all_tags( $content, true ) : $args['label']; 496 497 /* 498 * The generated button is a plain skeleton. Every dynamic attribute is 499 * added through the tag processor below, so caller-supplied markup is 500 * never scanned or substituted and a percent sign in custom markup, 501 * such as a percent-encoded URL, is never treated as a conversion 502 * specification. 503 */ 504 $default_button = '<button type="button"><span></span></button>'; 505 506 $is_default = ! $args['button']; 507 $button = ( $args['button'] ) ? $args['button'] : $default_button; 508 509 // The accepted root element is a `button`, or an `a` for tooltips. 510 $tag = false; 495 511 $processor = new WP_HTML_Tag_Processor( $button ); 496 512 if ( true === $processor->next_tag( 'button' ) ) { 497 $processor->add_class( 'wp-tooltip__toggle' ); 498 if ( 'tooltip' !== $args['type'] ) { 499 $processor->set_attribute( 'popovertarget', '%2$s' ); 500 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 501 } 502 $button = $processor->get_updated_html(); 503 $processed = true; 513 $tag = 'button'; 504 514 } else { 505 // Reset processor.506 515 $processor = new WP_HTML_Tag_Processor( $button ); 507 if ( true === $processor->next_tag( 'a' ) && 'tooltip' === $args['type'] ) { 508 $processor->add_class( 'wp-tooltip__toggle' ); 509 $button = $processor->get_updated_html(); 510 $processed = true; 511 } 512 } 513 if ( ! $processed ) { 514 // Button HTML passed was not valid. 515 $processor = new WP_HTML_Tag_Processor( $defaults['button'] ); 516 $processor->add_class( 'wp-tooltip__toggle' ); 517 if ( 'tooltip' !== $args['type'] ) { 518 $processor->set_attribute( 'popovertarget', '%2$s' ); 519 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 520 } 521 $button = $processor->get_updated_html(); 522 } 516 if ( 'tooltip' === $args['type'] && true === $processor->next_tag( 'a' ) ) { 517 $tag = 'a'; 518 } 519 } 520 521 if ( false === $tag ) { 522 // Button HTML passed was not valid. Reset to default. 523 $is_default = true; 524 $button = $default_button; 525 $processor = new WP_HTML_Tag_Processor( $button ); 526 $processor->next_tag( 'button' ); 527 $tag = 'button'; 528 } 529 530 /* 531 * Attributes that apply to every accepted button are added in one pass. 532 * Attributes that name the control are only added when the caller's 533 * markup did not already provide them. 534 */ 535 if ( null === $processor->get_attribute( 'aria-label' ) ) { 536 $processor->set_attribute( 'aria-label', $label ); 537 } 538 $processor->add_class( 'wp-tooltip__toggle' ); 539 if ( 'button' === $tag && 'tooltip' !== $args['type'] ) { 540 $processor->set_attribute( 'popovertarget', $id ); 541 $processor->set_attribute( 'aria-haspopup', 'dialog' ); 542 } 543 544 // The generated button also carries the dashicon on its inner span. 545 if ( $is_default && true === $processor->next_tag( 'span' ) ) { 546 $processor->set_attribute( 'class', 'dashicons ' . $icon ); 547 $processor->set_attribute( 'aria-hidden', 'true' ); 548 } 549 550 $button = $processor->get_updated_html(); 523 551 524 552 /* … … 529 557 */ 530 558 if ( 'tooltip' === $args['type'] ) { 531 // Tooltips are only used to visually display labels.532 $label = wp_strip_all_tags( $content, true );533 559 $markup = sprintf( 534 560 '<span class="%1$s"> 535 ' . $button . '561 %6$s 536 562 <span popover="hint" id="%2$s" class="wp-tooltip__bubble" role="tooltip">' . 537 563 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span>' . … … 543 569 esc_attr( $icon ), 544 570 esc_html( $content ), 571 $button, 545 572 ); 546 573 } else { … … 552 579 $markup = sprintf( 553 580 '<span class="%1$s"> 554 ' . $button . '581 %7$s 555 582 <span popover="auto" id="%2$s" class="wp-tooltip__bubble" role="dialog" aria-label="%3$s" tabindex="-1" autofocus>' . 556 583 '<span id="%2$s-text" class="wp-tooltip__text">%5$s</span>' . … … 562 589 esc_attr( $classes ), 563 590 esc_attr( $id ), 564 esc_attr( $ args['label']),591 esc_attr( $label ), 565 592 esc_attr( $icon ), 566 593 esc_html( $content ), 567 594 esc_attr( $args['close_label'] ), 595 $button, 568 596 ); 569 597 } -
branches/7.1/tests/phpunit/tests/general/wpGetTooltip.php
r62816 r63571 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.