Skip to content

Commit

Permalink
Test boolean and value-containing custom data attributes separately.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed May 30, 2024
1 parent dc859f1 commit 0498bad
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ public static function data_possible_custom_data_attributes_and_transformed_name
}

/**
* Ensures that only allowable custom data attributes are retained.
* Ensures that only allowable boolean custom data attributes are retained.
*
* @ticket 33121
*
Expand All @@ -1429,7 +1429,7 @@ public static function data_possible_custom_data_attributes_and_transformed_name
* @param string $attribute_name Custom data attribute, e.g. "data-wp-bind--enabled".
* @param bool $is_allowed Whether the given attribute should be allowed.
*/
public function test_wp_kses_attr_data_attribute_is_allowed( $attribute_name, $is_allowed ) {
public function test_wp_kses_attr_boolean_data_attribute_is_allowed( $attribute_name, $is_allowed ) {
$element = "<div {$attribute_name}>Pens and pencils.</div>";

$processor = new WP_HTML_Tag_Processor( $element );
Expand Down Expand Up @@ -1459,6 +1459,46 @@ public function test_wp_kses_attr_data_attribute_is_allowed( $attribute_name, $i
}
}

/**
* Ensures that only allowable custom data attributes with values are retained.
*
* @ticket 33121
*
* @dataProvider data_data_attributes_and_whether_they_are_allowed
*
* @param string $attribute_name Custom data attribute, e.g. "data-wp-bind--enabled".
* @param bool $is_allowed Whether the given attribute should be allowed.
*/
public function test_wp_kses_attr_data_attribute_is_allowed( $attribute_name, $is_allowed ) {
$element = "<div {$attribute_name}='shadows and dust'>Pens and pencils.</div>";

$processor = new WP_HTML_Tag_Processor( $element );
$processor->next_tag();

$this->assertTrue(
$processor->get_attribute( $attribute_name ),
"Failed to find expected attribute '{$attribute_name}' before filtering: check test."
);

$processor = new WP_HTML_Tag_Processor( wp_kses_post( $element ) );
$this->assertTrue(
$processor->next_tag(),
'Failed to find containing tag after filtering: check test.'
);

if ( $is_allowed ) {
$this->assertIsString(
$processor->get_attribute( $attribute_name ),
"Allowed custom data attribute '{$attribute_name}' should not have been removed."
);
} else {
$this->assertNull(
$processor->get_attribute( $attribute_name ),
"Should have removed un-allowed custom data attribute '{$attribute_name}'."
);
}
}

/**
* Data provider.
*
Expand Down

0 comments on commit 0498bad

Please sign in to comment.