wppaste
WordPress

wp_js_dataset_name( string $html_attribute_name ): string|null

Since
6.9.0
Source
wp-includes/script-loader.php:4074
Return the corresponding JavaScript dataset name for an attribute if it represents a custom data attribute, or null if not.

Description

Custom data attributes appear in an element's dataset property in a browser, but there's a specific way the names are translated from HTML into JavaScript. This function indicates how the name would appear in JavaScript if a browser would recognize it as a custom data attribute.

Example:

// Dash-letter pairs turn into capital letters.
'postId' === wp_js_dataset_name( 'data-post-id' );
'Before' === wp_js_dataset_name( 'data--before' );
'-One--Two---' === wp_js_dataset_name( 'data---one---two---' );

// Not every attribute name will be interpreted as a custom data attribute.
null === wp_js_dataset_name( 'post-id' );
null === wp_js_dataset_name( 'data' );

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

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$html_attribute_namestring
Raw attribute name as found in the source HTML.

Return value

string|null
Transformed dataset name, if interpretable as a custom data attribute, else null.

Performance profile

How much work a call to wp_js_dataset_name() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–41

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_js_dataset_name() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11substr_compare()
always20substr_compare(), strcspn()
always33–41substr_compare(), strcspn(), strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7411–40101 fewer instruction than PHP 8.5
8.57511–4110
8.47511–411012 fewer instructions than PHP 8.3
8.38711–4710
8.28711–4710
8.18711–47101 fewer instruction than PHP 7.4
7.48811–4710

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Source code

function wp_js_dataset_name( string $html_attribute_name ): ?string {	if ( 0 !== substr_compare( $html_attribute_name, 'data-', 0, 5, true ) ) {		return null;	} 	$end = strlen( $html_attribute_name ); 	/*	 * If it contains characters which would end the attribute name parsing then	 * something else is wrong and this contains more than just an attribute name.	 */	if ( ( $end - 5 ) !== strcspn( $html_attribute_name, "=/> \t\f\r\n", 5 ) ) {		return null;	} 	/**	 * > For each name in list, for each U+002D HYPHEN-MINUS character (-)	 * > in the name that is followed by an ASCII lower alpha, remove the	 * > U+002D HYPHEN-MINUS character (-) and replace the character that	 * > followed it by the same character converted to ASCII uppercase.	 *	 * @see https://html.spec.whatwg.org/#concept-domstringmap-pairs	 */	$custom_name = '';	$at          = 5;	$was_at      = $at; 	while ( $at < $end ) {		$next_dash_at = strpos( $html_attribute_name, '-', $at );		if ( false === $next_dash_at || $next_dash_at === $end - 1 ) {			break;		} 		// Transform `-a` to `A`, for example.		$c = $html_attribute_name[ $next_dash_at + 1 ];		if ( ( $c >= 'A' && $c <= 'Z' ) || ( $c >= 'a' && $c <= 'z' ) ) {			$prefix       = substr( $html_attribute_name, $was_at, $next_dash_at - $was_at );			$custom_name .= strtolower( $prefix );			$custom_name .= strtoupper( $c );			$at           = $next_dash_at + 2;			$was_at       = $at;			continue;		} 		$at = $next_dash_at + 1;	} 	// If nothing has been added it means there are no dash-letter pairs; return the name as-is.	return '' === $custom_name		? strtolower( substr( $html_attribute_name, 5 ) )		: ( $custom_name . strtolower( substr( $html_attribute_name, $was_at ) ) );}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/script-loader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.