Skip to content

Commit

Permalink
62546 do not add duplicate css classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Nov 25, 2024
1 parent 27c566f commit f48013f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/wp-admin/includes/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,14 @@
* @return string The string with the CSS class added.
*/
function add_cssclass( $class_to_add, $classes ) {
if ( empty( $classes ) ) {
if ( '' === $classes ) {
return $class_to_add;
}

if ( preg_match( '/(?>^|\s)' . preg_quote( $class_to_add, '/' ) . '(?>\s|$)/', $classes ) ) {
return $classes;
}

return $classes . ' ' . $class_to_add;
}

Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/tests/menu/menu-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @group menu
*/
class Tests_Menu_Functions extends WP_UnitTestCase {
/**
* @dataProvider data_add_cssclass
*
* @covers add_cssclass
*/
public function test_add_cssclass( $input, $expected ) {
$this->assertSame( $expected, add_cssclass( 'menu-top-first', $input ) );
}

public function data_add_cssclass() {
return array(
'class should not be added one' => array(

Check warning on line 18 in tests/phpunit/tests/menu/menu-functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 3 space(s) between "'class should not be added one'" and double arrow, but found 1.
'input' => 'menu-top menu-top-first menu-icon-dashboard',
'expected' => 'menu-top menu-top-first menu-icon-dashboard',
),
'class should not be added two' => array(

Check warning on line 22 in tests/phpunit/tests/menu/menu-functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 3 space(s) between "'class should not be added two'" and double arrow, but found 1.
'input' => 'menu-top menu-top-first',
'expected' => 'menu-top menu-top-first',
),
'class should not be added three' => array(
'input' => 'menu-top-first',
'expected' => 'menu-top-first',
),
'class should not be added four' => array(

Check warning on line 30 in tests/phpunit/tests/menu/menu-functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 2 space(s) between "'class should not be added four'" and double arrow, but found 1.
'input' => 'menu-top-first menu-icon-dashboard',
'expected' => 'menu-top-first menu-icon-dashboard',
),
'class should be added one' => array(

Check warning on line 34 in tests/phpunit/tests/menu/menu-functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 7 space(s) between "'class should be added one'" and double arrow, but found 1.
'input' => 'menu-top menu-top-first-foo menu-icon-dashboard',
'expected' => 'menu-top menu-top-first-foo menu-icon-dashboard menu-top-first',
),
'class should be added two' => array(

Check warning on line 38 in tests/phpunit/tests/menu/menu-functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array double arrow not aligned correctly; expected 7 space(s) between "'class should be added two'" and double arrow, but found 1.
'input' => 'menu-top foo-menu-top-first menu-icon-dashboard',
'expected' => 'menu-top foo-menu-top-first menu-icon-dashboard menu-top-first',
),
);
}
}

0 comments on commit f48013f

Please sign in to comment.