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.


git-svn-id: https://develop.svn.wordpress.org/trunk@38523 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2016-09-06 09:05:45 +00:00
parent 18654d2778
commit 0c88ec217d
7 changed files with 213 additions and 41 deletions
+11
View File
@@ -17,6 +17,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
'link_before' => '',
'link_after' => '',
'walker' => '',
'item_spacing' => 'preserve',
'include' => '',
'post_type' => 'page',
'post_status' => 'publish',
@@ -342,4 +343,14 @@ class Tests_List_Pages extends WP_UnitTestCase {
$actual = wp_list_pages( $args );
$this->AssertEquals( $expected['exclude'], $actual );
}
function test_wp_list_pages_discarded_whitespace() {
$args = array(
'echo' => false,
'item_spacing' => 'discard',
);
$expected['default'] = '<li class="pagenav">Pages<ul><li class="page_item page-item-1 page_item_has_children"><a href="' . get_permalink( 1 ) . '">Parent 1</a><ul class=\'children\'><li class="page_item page-item-4"><a href="' . get_permalink( 4 ) . '">Child 1</a></li><li class="page_item page-item-5"><a href="' . get_permalink( 5 ) . '">Child 2</a></li><li class="page_item page-item-6"><a href="' . get_permalink( 6 ) . '">Child 3</a></li></ul></li><li class="page_item page-item-2 page_item_has_children"><a href="' . get_permalink( 2 ) . '">Parent 2</a><ul class=\'children\'><li class="page_item page-item-7"><a href="' . get_permalink( 7 ) . '">Child 1</a></li><li class="page_item page-item-8"><a href="' . get_permalink( 8 ) . '">Child 2</a></li><li class="page_item page-item-9"><a href="' . get_permalink( 9 ) . '">Child 3</a></li></ul></li><li class="page_item page-item-3 page_item_has_children"><a href="' . get_permalink( 3 ) . '">Parent 3</a><ul class=\'children\'><li class="page_item page-item-10"><a href="' . get_permalink( 10 ) . '">Child 1</a></li><li class="page_item page-item-11"><a href="' . get_permalink( 11 ) . '">Child 2</a></li><li class="page_item page-item-12"><a href="' . get_permalink( 12 ) . '">Child 3</a></li></ul></li></ul></li>';
$actual = wp_list_pages( $args );
$this->AssertEquals( $expected['default'], $actual );
}
}
+62
View File
@@ -277,6 +277,68 @@ class Test_Nav_Menus extends WP_UnitTestCase {
}
/**
* @ticket 35206
*/
function test_wp_nav_menu_whitespace_options() {
$post_id1 = self::factory()->post->create();
$post_id2 = self::factory()->post->create();
$post_id3 = self::factory()->post->create();
$post_id4 = self::factory()->post->create();
$post_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-object-id' => $post_id1,
'menu-item-status' => 'publish'
) );
$post_inser2 = wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-object-id' => $post_id2,
'menu-item-status' => 'publish'
) );
$post_insert3 = wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-parent-id' => $post_insert,
'menu-item-object-id' => $post_id3,
'menu-item-status' => 'publish'
) );
$post_insert4 = wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-parent-id' => $post_insert,
'menu-item-object-id' => $post_id4,
'menu-item-status' => 'publish'
) );
// No whitespace suppression.
$menu = wp_nav_menu( array(
'echo' => false,
'menu' => $this->menu_id,
) );
// The markup should include whitespace between <li>s
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/<\/li><li.*>/U', $menu );
// Whitepsace suppressed.
$menu = wp_nav_menu( array(
'echo' => false,
'item_spacing' => 'discard',
'menu' => $this->menu_id,
) );
// The markup should not include whitespace around <li>s
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
}
/*
* Confirm `wp_nav_menu()` and `Walker_Nav_Menu` passes an $args object to filters.
*
* `wp_nav_menu()` is unique in that it uses an $args object rather than an array.
+15
View File
@@ -299,6 +299,10 @@ NO;
// After falling back, the 'after' argument should be set and output as '</ul>'.
$this->assertRegExp( '/<\/ul><\/div>/', $menu );
// After falling back, the markup should include whitespace around <li>s
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/><li.*>|<\/li></U', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without a container.
$menu = wp_nav_menu( array(
'echo' => false,
@@ -307,5 +311,16 @@ NO;
// After falling back, the empty 'container' argument should still return a container element.
$this->assertRegExp( '/<div class="menu">/', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without white-space.
$menu = wp_nav_menu( array(
'echo' => false,
'item_spacing' => 'discard',
) );
// After falling back, the markup should not include whitespace around <li>s
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
}
}