From ef0c6abad7e54f021e0575ee2769e5aa4e760e02 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Thu, 24 Oct 2024 02:47:45 +0530 Subject: [PATCH 1/3] Change the empty check to empty string check --- src/wp-includes/shortcodes.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/shortcodes.php b/src/wp-includes/shortcodes.php index bddf63ada28a3..e1d37215546cd 100644 --- a/src/wp-includes/shortcodes.php +++ b/src/wp-includes/shortcodes.php @@ -616,11 +616,11 @@ function shortcode_parse_atts( $text ) { $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text ); if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) { foreach ( $match as $m ) { - if ( ! empty( $m[1] ) ) { + if ( isset( $m[1] ) && '' !== $m[1] ) { $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] ); - } elseif ( ! empty( $m[3] ) ) { + } elseif ( isset( $m[3] ) && '' !== $m[3] ) { $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] ); - } elseif ( ! empty( $m[5] ) ) { + } elseif ( isset( $m[5] ) && '' !== $m[5] ) { $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] ); } elseif ( isset( $m[7] ) && strlen( $m[7] ) ) { $atts[] = stripcslashes( $m[7] ); From b7379438c18bce2861ba5da2276e73a7576d9b05 Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Fri, 1 Nov 2024 01:47:18 +0530 Subject: [PATCH 2/3] Add tests cases --- tests/phpunit/tests/shortcode.php | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php index 2c5b04d02e147..cb7d0a33dbf04 100644 --- a/tests/phpunit/tests/shortcode.php +++ b/tests/phpunit/tests/shortcode.php @@ -1019,4 +1019,81 @@ public function test_shortcode_parse_atts_empty() { $this->assertIsArray( $out, 'Return value is not an array' ); $this->assertEmpty( $out, 'Returned array is not empty' ); } + + /** + * @dataProvider data_should_return_expected_attributes_when_shortcode_is_parsed + */ + public function test_should_return_expected_attributes_when_shortcode_is_parsed($input, $expected) { + $this->assertSame($expected, shortcode_parse_atts($input)); + } + + public function data_should_return_expected_attributes_when_shortcode_is_parsed() { + return array( + 'basic_attributes' => array( + 'attr1="value1" attr2="value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2' + ) + ), + 'mixed_case_attributes' => array( + 'Attr1="value1" ATTR2="value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2' + ) + ), + 'single_quotes' => array( + "attr1='value1' attr2='value2'", + array( + 'attr1' => 'value1', + 'attr2' => 'value2' + ) + ), + 'unclosed_html_elements' => array( + 'attr1="
value1"', + array( + 'attr1' => '
value1' + ) + ), + 'no_attributes' => array( + '', + array() + ), + 'extra_whitespace' => array( + ' attr1 = "value1" attr2 = "value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2' + ) + ), + 'html_in_attributes' => array( + 'attr1="Bold"', + array( + 'attr1' => 'Bold' + ) + ), + 'special_characters' => array( + 'attr1="value!@#$%^&*()_+"', + array( + 'attr1' => 'value!@#$%^&*()_+' + ) + ), + 'numeric_attribute_keys' => array( + '0="value0" 1="value1" 2="value2"', + array( + '0' => 'value0', + '1' => 'value1', + '2' => 'value2' + ) + ), + 'malformed_attribute' => array( + 'attr1="value1" attr2', + array( + 'attr1' => 'value1', + 0 => 'attr2' + ) + ), + ); + } } From 640933892249b2863ba34b7c1a46bf9c35feadec Mon Sep 17 00:00:00 2001 From: Debarghya Banerjee Date: Fri, 1 Nov 2024 02:14:36 +0530 Subject: [PATCH 3/3] Fix coding standards --- tests/phpunit/tests/shortcode.php | 150 +++++++++++++++--------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php index cb7d0a33dbf04..4f01b327f2710 100644 --- a/tests/phpunit/tests/shortcode.php +++ b/tests/phpunit/tests/shortcode.php @@ -1021,79 +1021,79 @@ public function test_shortcode_parse_atts_empty() { } /** - * @dataProvider data_should_return_expected_attributes_when_shortcode_is_parsed - */ - public function test_should_return_expected_attributes_when_shortcode_is_parsed($input, $expected) { - $this->assertSame($expected, shortcode_parse_atts($input)); - } - - public function data_should_return_expected_attributes_when_shortcode_is_parsed() { - return array( - 'basic_attributes' => array( - 'attr1="value1" attr2="value2"', - array( - 'attr1' => 'value1', - 'attr2' => 'value2' - ) - ), - 'mixed_case_attributes' => array( - 'Attr1="value1" ATTR2="value2"', - array( - 'attr1' => 'value1', - 'attr2' => 'value2' - ) - ), - 'single_quotes' => array( - "attr1='value1' attr2='value2'", - array( - 'attr1' => 'value1', - 'attr2' => 'value2' - ) - ), - 'unclosed_html_elements' => array( - 'attr1="
value1"', - array( - 'attr1' => '
value1' - ) - ), - 'no_attributes' => array( - '', - array() - ), - 'extra_whitespace' => array( - ' attr1 = "value1" attr2 = "value2"', - array( - 'attr1' => 'value1', - 'attr2' => 'value2' - ) - ), - 'html_in_attributes' => array( - 'attr1="Bold"', - array( - 'attr1' => 'Bold' - ) - ), - 'special_characters' => array( - 'attr1="value!@#$%^&*()_+"', - array( - 'attr1' => 'value!@#$%^&*()_+' - ) - ), - 'numeric_attribute_keys' => array( - '0="value0" 1="value1" 2="value2"', - array( - '0' => 'value0', - '1' => 'value1', - '2' => 'value2' - ) - ), - 'malformed_attribute' => array( - 'attr1="value1" attr2', - array( - 'attr1' => 'value1', - 0 => 'attr2' - ) - ), - ); - } + * @dataProvider data_should_return_expected_attributes_when_shortcode_is_parsed + */ + public function test_should_return_expected_attributes_when_shortcode_is_parsed( $input, $expected ) { + $this->assertSame( $expected, shortcode_parse_atts( $input ) ); + } + + public function data_should_return_expected_attributes_when_shortcode_is_parsed() { + return array( + 'basic_attributes' => array( + 'attr1="value1" attr2="value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2', + ), + ), + 'mixed_case_attributes' => array( + 'Attr1="value1" ATTR2="value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2', + ), + ), + 'single_quotes' => array( + "attr1='value1' attr2='value2'", + array( + 'attr1' => 'value1', + 'attr2' => 'value2', + ), + ), + 'unclosed_html_elements' => array( + 'attr1="
value1"', + array( + 'attr1' => '
value1', + ), + ), + 'no_attributes' => array( + '', + array(), + ), + 'extra_whitespace' => array( + ' attr1 = "value1" attr2 = "value2"', + array( + 'attr1' => 'value1', + 'attr2' => 'value2', + ), + ), + 'html_in_attributes' => array( + 'attr1="Bold"', + array( + 'attr1' => 'Bold', + ), + ), + 'special_characters' => array( + 'attr1="value!@#$%^&*()_+"', + array( + 'attr1' => 'value!@#$%^&*()_+', + ), + ), + 'numeric_attribute_keys' => array( + '0="value0" 1="value1" 2="value2"', + array( + '0' => 'value0', + '1' => 'value1', + '2' => 'value2', + ), + ), + 'malformed_attribute' => array( + 'attr1="value1" attr2', + array( + 'attr1' => 'value1', + 0 => 'attr2', + ), + ), + ); + } }