Skip to content
Permalink
Browse files

Menus: In `Walker_Nav_Menu`, `Walker_Category`, and `Walker_Page`, pr…

…operly output link attributes having a legitimate "empty" value, for example an HTML data attribute with a value of zero (0).

Props nevma, AkSDvP, greenshady, SergeyBiryukov.
Fixes #47720.

git-svn-id: https://develop.svn.wordpress.org/trunk@46413 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Oct 6, 2019
1 parent ed1a535 commit f6267a04eee1bd82b4d283290e5ccb442eafb2ef
@@ -139,7 +139,7 @@ public function start_el( &$output, $category, $depth = 0, $args = array(), $id
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
@@ -203,7 +203,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
@@ -182,7 +182,7 @@ public function start_el( &$output, $page, $depth = 0, $args = array(), $current
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
@@ -0,0 +1,92 @@
<?php
/**
* @group taxonomy
* @group walker
*/
class Tests_Category_Walker_Category extends WP_UnitTestCase {
/**
* @var \Walker_Category The instance of the walker.
*/
public $walker;
/**
* Setup.
*/
public function setUp() {
parent::setUp();
/** Walker_Category class */
require_once ABSPATH . 'wp-includes/class-walker-category.php';
$this->walker = new Walker_Category();
}
/**
* @ticket 47720
*
* @dataProvider data_start_el_with_empty_attributes
*/
public function test_start_el_with_empty_attributes( $value, $expected ) {
$output = '';
$category = $this->factory->category->create_and_get();
$link = get_term_link( $category );
$args = array(
'use_desc_for_title' => 0,
'style' => 'list',
);
add_filter(
'category_list_link_attributes',
function( $atts ) use ( $value ) {
$atts['data-test'] = $value;
return $atts;
}
);
$this->walker->start_el( $output, $category, 0, $args );
if ( '' !== $expected ) {
$expected = sprintf( ' data-test="%s"', $expected );
}
$this->assertSame( "<li class=\"cat-item cat-item-{$category->term_id}\"><a href=\"{$link}\"{$expected}>{$category->name}</a>", trim( $output ) );
}
public function data_start_el_with_empty_attributes() {
return array(
array(
'',
'',
),
array(
0,
'0',
),
array(
0.0,
'0',
),
array(
'0',
'0',
),
array(
null,
'',
),
array(
false,
'',
),
array(
true,
'1',
),
array(
array(),
'',
),
);
}
}
@@ -4,7 +4,7 @@
* @group navmenus
* @group walker
*/
class Tests_Walker_Nav_Menu_Edit extends WP_UnitTestCase {
class Tests_Menu_Walker_Nav_Menu_Edit extends WP_UnitTestCase {
protected $_wp_nav_menu_max_depth;
function setUp() {
@@ -3,7 +3,7 @@
* @group navmenus
* @group walker
*/
class Tests_Walker_Nav_Menu extends WP_UnitTestCase {
class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
/**
* @var \Walker_Nav_Menu The instance of the walker.
@@ -18,7 +18,7 @@ public function setUp() {
parent::setUp();
/** Walker_Nav_Menu_Edit class */
/** Walker_Nav_Menu class */
require_once ABSPATH . 'wp-includes/class-walker-nav-menu.php';
$this->walker = new Walker_Nav_Menu();
@@ -37,9 +37,9 @@ public function tearDown() {
}
/**
* Tests when an items target it _blank, that rel="'noopener noreferrer" is added.
* Tests when an item's target is _blank, that rel="noopener noreferrer" is added.
*
* @ticket #43290
* @ticket 43290
*/
public function test_noopener_no_referrer_for_target_blank() {
$expected = '';
@@ -66,4 +66,84 @@ public function test_noopener_no_referrer_for_target_blank() {
$this->assertSame( "<li id=\"menu-item-{$post_id}\" class=\"menu-item-{$post_id}\"><a target=\"_blank\" rel=\"noopener noreferrer\">{$post_title}</a>", $expected );
}
/**
* @ticket 47720
*
* @dataProvider data_start_el_with_empty_attributes
*/
public function test_start_el_with_empty_attributes( $value, $expected ) {
$output = '';
$post_id = $this->factory->post->create();
$post_title = get_the_title( $post_id );
$item = array(
'ID' => $post_id,
'object_id' => $post_id,
'title' => $post_title,
'target' => '',
'xfn' => '',
'current' => false,
);
$args = array(
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
);
add_filter(
'nav_menu_link_attributes',
function( $atts ) use ( $value ) {
$atts['data-test'] = $value;
return $atts;
}
);
$this->walker->start_el( $output, (object) $item, 0, (object) $args );
if ( '' !== $expected ) {
$expected = sprintf( ' data-test="%s"', $expected );
}
$this->assertSame( "<li id=\"menu-item-{$post_id}\" class=\"menu-item-{$post_id}\"><a{$expected}>{$post_title}</a>", $output );
}
public function data_start_el_with_empty_attributes() {
return array(
array(
'',
'',
),
array(
0,
'0',
),
array(
0.0,
'0',
),
array(
'0',
'0',
),
array(
null,
'',
),
array(
false,
'',
),
array(
true,
'1',
),
array(
array(),
'',
),
);
}
}
@@ -0,0 +1,87 @@
<?php
/**
* @group post
* @group walker
*/
class Tests_Post_Walker_Page extends WP_UnitTestCase {
/**
* @var \Walker_Page The instance of the walker.
*/
public $walker;
/**
* Setup.
*/
public function setUp() {
parent::setUp();
/** Walker_Page class */
require_once ABSPATH . 'wp-includes/class-walker-page.php';
$this->walker = new Walker_Page();
}
/**
* @ticket 47720
*
* @dataProvider data_start_el_with_empty_attributes
*/
public function test_start_el_with_empty_attributes( $value, $expected ) {
$output = '';
$page = $this->factory->post->create_and_get( array( 'post_type' => 'page' ) );
$link = get_permalink( $page );
add_filter(
'page_menu_link_attributes',
function( $atts ) use ( $value ) {
$atts['data-test'] = $value;
return $atts;
}
);
$this->walker->start_el( $output, $page, 0 );
if ( '' !== $expected ) {
$expected = sprintf( ' data-test="%s"', $expected );
}
$this->assertSame( "<li class=\"page_item page-item-{$page->ID}\"><a href=\"{$link}\"{$expected}>{$page->post_title}</a>", $output );
}
public function data_start_el_with_empty_attributes() {
return array(
array(
'',
'',
),
array(
0,
'0',
),
array(
0.0,
'0',
),
array(
'0',
'0',
),
array(
null,
'',
),
array(
false,
'',
),
array(
true,
'1',
),
array(
array(),
'',
),
);
}
}

0 comments on commit f6267a0

Please sign in to comment.
You can’t perform that action at this time.