Skip to content

Commit

Permalink
Formatting: Preserve target="_blank" in Biographical Info and Categ…
Browse files Browse the repository at this point in the history
…ory Description.

This changeset ensures the `target="_blank"` attribute is preserved when adding links in the Biographical Info and Category Description fields. Previously, this attribute was being stripped by the KSES sanitization process.

Additionally, new unit tests have been added to verify the preservation of the `target="_blank"` attribute in these specific contexts.

Props lovewpmu, miqrogroove, bsutcliffe, sjefen6, nofearinc, nacin, harmr, blogitsolutions, stefahn, nirajgirixd, martinkrcho, spacedmonkey, sukhendu2002, audrasjb, gaellebesson, nuryko, guillaumeturpin, maximemeganck, ranafge, azaozz, joedolson, rinkalpagdar, mikinc860.
Fixes #12056.




git-svn-id: https://develop.svn.wordpress.org/trunk@59677 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
audrasjb committed Jan 21, 2025
1 parent eb50dd7 commit 0f2334d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,9 +895,11 @@ function wp_kses_allowed_html( $context = '' ) {
return $tags;

case 'user_description':
case 'pre_term_description':
case 'pre_user_description':
$tags = $allowedtags;
$tags['a']['rel'] = true;
$tags = $allowedtags;
$tags['a']['rel'] = true;
$tags['a']['target'] = true;
/** This filter is documented in wp-includes/kses.php */
return apply_filters( 'wp_kses_allowed_html', $tags, $context );

Expand Down
72 changes: 72 additions & 0 deletions tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2244,4 +2244,76 @@ public function data_kses_globals_are_defined() {

return $this->text_array_to_dataprovider( $required_kses_globals );
}

/**
* Tests that the target attribute is preserved in various contexts.
*
* @dataProvider data_target_attribute_preserved_in_descriptions
*
* @ticket 12056
*
* @param string $context The context to test ('user_description' or 'pre_term_description').
* @param string $input The input HTML string.
* @param string $expected The expected output HTML string.
*/
public function test_target_attribute_preserved_in_context( $context, $input, $expected ) {
$allowed = wp_kses_allowed_html( $context );
$this->assertTrue( isset( $allowed['a']['target'] ), "Target attribute not allowed in {$context}" );
$this->assertEquals( $expected, wp_kses( $input, $context ) );
}

/**
* Data provider for test_target_attribute_preserved_in_context.
*
* @return array
*/
public function data_target_attribute_preserved_in_descriptions() {
return array(
array(
'user_description',
'<a href="https://example.com" target="_blank">Example</a>',
'<a href="https://example.com" target="_blank">Example</a>',
),
array(
'pre_term_description',
'<a href="https://example.com" target="_blank">Example</a>',
'<a href="https://example.com" target="_blank">Example</a>',
),
);
}

/**
* Tests that specific attributes are preserved in various contexts.
*
* @dataProvider data_allowed_attributes_in_descriptions
*
* @ticket 12056
*
* @param string $context The context to test ('user_description' or 'pre_term_description').
* @param array $attributes List of attributes to check for.
*/
public function test_specific_attributes_preserved_in_context( $context, $attributes ) {
$allowed = wp_kses_allowed_html( $context );
foreach ( $attributes as $attribute ) {
$this->assertTrue( isset( $allowed['a'][ $attribute ] ), "{$attribute} attribute not allowed in {$context}" );
}
}

/**
* Data provider for test_specific_attributes_preserved_in_context.
*
* @return array
*/
public function data_allowed_attributes_in_descriptions() {
return array(
array(
'user_description',
array( 'target', 'href', 'rel' ),
),
array(
'pre_term_description',
array( 'target', 'href', 'rel' ),
),
);
}
}

0 comments on commit 0f2334d

Please sign in to comment.