Make WordPress Core


Ignore:
Timestamp:
09/06/2016 09:05:45 AM (10 years ago)
Author:
peterwilsoncc
Message:

Menus: Add white space option to wp_nav_menu() and wp_list_pages().

Adds an item_spacing option to the arguments array for the functions wp_nav_menu(), wp_list_pages(), and wp_page_menu(). item_spacing is a boolean accepting either preserve or discard.

Previously, certain CSS choices could result in a site's layout changing if wp_nav_menu() fell back to the default wp_list_pages() due to differences in the whitespace within the HTML. The new argument ensures a function outputs consistant HTML while maintaining backward compatibility.

Fixes #35206.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r38400 r38523  
    278278
    279279        /**
     280         * @ticket 35206
     281         */
     282        function test_wp_nav_menu_whitespace_options() {
     283                $post_id1 = self::factory()->post->create();
     284                $post_id2 = self::factory()->post->create();
     285                $post_id3 = self::factory()->post->create();
     286                $post_id4 = self::factory()->post->create();
     287
     288                $post_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     289                        'menu-item-type' => 'post_type',
     290                        'menu-item-object' => 'post',
     291                        'menu-item-object-id' => $post_id1,
     292                        'menu-item-status' => 'publish'
     293                ) );
     294
     295                $post_inser2 = wp_update_nav_menu_item( $this->menu_id, 0, array(
     296                        'menu-item-type' => 'post_type',
     297                        'menu-item-object' => 'post',
     298                        'menu-item-object-id' => $post_id2,
     299                        'menu-item-status' => 'publish'
     300                ) );
     301
     302                $post_insert3 = wp_update_nav_menu_item( $this->menu_id, 0, array(
     303                        'menu-item-type' => 'post_type',
     304                        'menu-item-object' => 'post',
     305                        'menu-item-parent-id' => $post_insert,
     306                        'menu-item-object-id' => $post_id3,
     307                        'menu-item-status' => 'publish'
     308                ) );
     309
     310                $post_insert4 = wp_update_nav_menu_item( $this->menu_id, 0, array(
     311                        'menu-item-type' => 'post_type',
     312                        'menu-item-object' => 'post',
     313                        'menu-item-parent-id' => $post_insert,
     314                        'menu-item-object-id' => $post_id4,
     315                        'menu-item-status' => 'publish'
     316                ) );
     317
     318                // No whitespace suppression.
     319                $menu = wp_nav_menu( array(
     320                        'echo' => false,
     321                        'menu' => $this->menu_id,
     322                ) );
     323
     324                // The markup should include whitespace between <li>s
     325                $this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
     326                $this->assertNotRegExp( '/<\/li><li.*>/U', $menu );
     327
     328
     329                // Whitepsace suppressed.
     330                $menu = wp_nav_menu( array(
     331                        'echo'         => false,
     332                        'item_spacing' => 'discard',
     333                        'menu'         => $this->menu_id,
     334                ) );
     335
     336                // The markup should not include whitespace around <li>s
     337                $this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
     338                $this->assertRegExp( '/><li.*>|<\/li></U', $menu );
     339        }
     340
     341        /*
    280342         * Confirm `wp_nav_menu()` and `Walker_Nav_Menu` passes an $args object to filters.
    281343         *
Note: See TracChangeset for help on using the changeset viewer.