функция since 6.9.0

wp_js_dataset_name()

Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.

Сигнатура

wp_js_dataset_name( string $html_attribute_name ): string|null

Описание

Пользовательские data-атрибуты появляются в свойстве dataset элемента в браузере, но имена преобразуются из HTML в JavaScript особым образом. Эта функция показывает, как имя выглядело бы в JavaScript, если бы браузер распознал его как пользовательский data-атрибут.
Пример:

// 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-' ) );См. такжеhttps://html.spec.whatwg.org/#concept-domstringmap-pairs

wp_html_custom_data_attribute_name()

Оригинал (английский)

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-' ) );

See also

Параметры

$html_attribute_name string обязательный
Необработанное имя атрибута в том виде, в каком оно встречается в исходном HTML.

Возвращаемое значение

string|null

dataset null

Исходный код

wp-includes/script-loader.php:4068

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+D HYPHEN-MINUS character (-)
	 * > in the name that is followed by an ASCII lower alpha, remove the
	 * > U+D 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 ) ) );
}

История изменений

ВерсияОписание
6.9.0 Introduced.

Что будем искать? Например,Продвижение

Этот сайт использует куки-файлы. Оставаясь на сайте, Вы соглашаетесь на их использование. Для получения дополнительной информации, пожалуйста, ознакомьтесь с политикой в отношении персональных данных.