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 ce571a9
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(
'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(
'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(
'input' => 'menu-top-first menu-icon-dashboard',
'expected' => 'menu-top-first menu-icon-dashboard',
),
'class should be added one' => array(
'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(
'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 ce571a9

Please sign in to comment.