wp_html_custom_data_attribute_name( string $js_dataset_name ): string|null

Returns a corresponding HTML attribute name for the given name, if that name were found in a JS element’s dataset property.

Description

Example:

'data-post-id'        === wp_html_custom_data_attribute_name( 'postId' );
'data--before'        === wp_html_custom_data_attribute_name( 'Before' );
'data---one---two---' === wp_html_custom_data_attribute_name( '-One--Two---' );

// Not every attribute name will be interpreted as a custom data attribute.
null === wp_html_custom_data_attribute_name( '/not-an-attribute/' );
null === wp_html_custom_data_attribute_name( 'no spaces' );

// Some very surprising names will; for example, a property whose name is the empty string.
'data-' === wp_html_custom_data_attribute_name( '' );

See also

Parameters

$js_dataset_namestringrequired
Name of JS dataset property to transform.

Return

string|null Corresponding name of an HTML custom data attribute for the given dataset name, if possible to represent in HTML, otherwise null.

Source

function wp_html_custom_data_attribute_name( string $js_dataset_name ): ?string {
	$end = strlen( $js_dataset_name );
	if ( 0 === $end ) {
		return 'data-';
	}

	/*
	 * If it contains characters which would end the attribute name parsing then
	 * something it’s not possible to represent this in HTML.
	 */
	if ( strcspn( $js_dataset_name, "=/> \t\f\r\n" ) !== $end ) {
		return null;
	}

	$html_name = 'data-';
	$at        = 0;
	$was_at    = $at;

	while ( $at < $end ) {
		$next_upper_after = strcspn( $js_dataset_name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', $at );
		$next_upper_at    = $at + $next_upper_after;
		if ( $next_upper_at >= $end ) {
			break;
		}

		$prefix     = substr( $js_dataset_name, $was_at, $next_upper_at - $was_at );
		$html_name .= strtolower( $prefix );
		$html_name .= '-' . strtolower( $js_dataset_name[ $next_upper_at ] );
		$at         = $next_upper_at + 1;
		$was_at     = $at;
	}

	if ( $was_at < $end ) {
		$html_name .= strtolower( substr( $js_dataset_name, $was_at ) );
	}

	return $html_name;
}

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.