<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[61007] trunk: JS Interop: Add custom data attribute name converter pair.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/61007">61007</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/61007","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dmsnell</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-10-21 05:10:05 +0000 (Tue, 21 Oct 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>JS Interop: Add custom data attribute name converter pair.
This patch introduces two new functions: `wp_js_dataset_name()` and `wp_html_custom_data_attribute_name()`. Together, these provide reliable mapping between the HTML attribute names for custom data attributes, and the properties found in JavaScript for a given `HTMLElement`'s `.dataset` property.
These are to be used where matching names is important, such as in the Interactivity API and when WordPress is deciding whether or not to allow an attribute as a custom data attribute.
Developed in https://github.com/WordPress/wordpress-develop/pull/9953
Discussed in https://core.trac.wordpress.org/ticket/61501
Props dmsnell, westonruter.
See <a href="https://core.trac.wordpress.org/ticket/61501">#61501</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsjsinteropphp">trunk/tests/phpunit/tests/js-interop.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php 2025-10-21 04:31:04 UTC (rev 61006)
+++ trunk/src/wp-includes/script-loader.php 2025-10-21 05:10:05 UTC (rev 61007)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3468,3 +3468,153 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Return the corresponding JavaScript `dataset` name for an attribute
+ * if it represents a custom data attribute, or `null` if not.
+ *
+ * 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-' ) );
+ *
+ * @since 6.9.0
+ *
+ * @see https://html.spec.whatwg.org/#concept-domstringmap-pairs
+ * @see \wp_html_custom_data_attribute_name()
+ *
+ * @param string $html_attribute_name Raw attribute name as found in the source HTML.
+ * @return string|null Transformed `dataset` name, if interpretable as a custom data attribute, else `null`.
+ */
+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 ) ) );
+}
+
+/**
+ * Returns a corresponding HTML attribute name for the given name,
+ * if that name were found in a JS element’s `dataset` property.
+ *
+ * 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( '' );
+ *
+ * @since 6.9.0
+ *
+ * @see https://html.spec.whatwg.org/#concept-domstringmap-pairs
+ * @see \wp_js_dataset_name()
+ *
+ * @param string $js_dataset_name 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`.
+ */
+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;
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsjsinteropphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/js-interop.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/js-interop.php (rev 0)
+++ trunk/tests/phpunit/tests/js-interop.php 2025-10-21 05:10:05 UTC (rev 61007)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,131 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Tests verifying behaviors for supporting interoperability with JavaScript.
+ *
+ * @package WordPress
+ * @group js-interop
+ */
+class Tests_JS_Interop extends WP_UnitTestCase {
+ /**
+ * Ensures proper recognition of a data attribute and how to transform its
+ * name into what JavaScript code would read from an element's `dataset`.
+ *
+ * @ticket 61501
+ *
+ * @dataProvider data_possible_custom_data_attributes_and_transformed_names
+ *
+ * @param string|null $attribute_name Raw HTML attribute name, if representable.
+ * @param string|null $dataset_name Transformed attribute name, or `null` if not a custom data attribute.
+ */
+ public function test_transforms_custom_attributes_to_proper_dataset_name( ?string $attribute_name, ?string $dataset_name ) {
+ if ( ! isset( $attribute_name ) ) {
+ // Skipping leaves a warning but this test data doesn’t apply to this side of the transformer.
+ $this->assertTrue( true, 'This test only applies to the reverse transformation.' );
+ return;
+ }
+
+ $transformed_name = wp_js_dataset_name( $attribute_name );
+
+ if ( isset( $dataset_name ) ) {
+ $this->assertNotNull(
+ $transformed_name,
+ "Failed to recognize '{$attribute_name}' as a custom data attribute."
+ );
+
+ $this->assertSame(
+ $dataset_name,
+ $transformed_name,
+ 'Improperly transformed custom data attribute name.'
+ );
+ } else {
+ $this->assertNull(
+ $transformed_name,
+ "Should not have identified '{$attribute_name}' as a custom data attribute."
+ );
+ }
+ }
+
+ /**
+ * Ensures proper transformation from JS dataset name to HTML custom attribute name.
+ *
+ * @ticket 61501
+ *
+ * @dataProvider data_possible_custom_data_attributes_and_transformed_names
+ *
+ * @param string|null $attribute_name Raw HTML attribute name, if representable.
+ * @param string|null $dataset_name Transformed attribute name, or `null` if not a custom data attribute.
+ */
+ public function test_transforms_dataset_to_proper_html_attribute_name( ?string $attribute_name, ?string $dataset_name ) {
+ if ( ! isset( $dataset_name ) ) {
+ // Skipping leaves a warning but this test data doesn’t apply to this side of the transformer.
+ $this->assertTrue( true, 'This test only applies to the reverse transformation.' );
+ return;
+ }
+
+ $transformed_name = wp_html_custom_data_attribute_name( $dataset_name );
+
+ if ( isset( $attribute_name ) ) {
+ $this->assertNotNull(
+ $transformed_name,
+ "Failed to recognize '{$dataset_name}' as a representable dataset property."
+ );
+
+ $this->assertSame(
+ strtolower( $attribute_name ),
+ $transformed_name,
+ 'Improperly transformed dataset property name.'
+ );
+ } else {
+ $this->assertNull(
+ $transformed_name,
+ "Should not have identified '{$dataset_name}' as a representable dataset property."
+ );
+ }
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[].
+ */
+ public static function data_possible_custom_data_attributes_and_transformed_names() {
+ return array(
+ // Non-custom-data attributes.
+ 'Normal attribute' => array( 'post-id', null ),
+ 'Single word' => array( 'id', null ),
+
+ // Invalid HTML attribute names.
+ 'Contains spaces' => array( 'no spaces', null ),
+ 'Contains solidus' => array( 'one/more/name', null ),
+
+ // Unrepresentable dataset names.
+ 'Dataset contains spaces' => array( null, 'one two' ),
+ 'Dataset contains solidus' => array( null, 'no/more/names' ),
+
+ // Normative custom data attributes.
+ 'Normal custom data attribute' => array( 'data-post-id', 'postId' ),
+ 'Leading dash' => array( 'data--before', 'Before' ),
+ 'Trailing dash' => array( 'data-after-', 'after-' ),
+ 'Double-dashes' => array( 'data-wp-bind--enabled', 'wpBind-Enabled' ),
+ 'Double-dashes everywhere' => array( 'data--one--two--', 'One-Two--' ),
+ 'Triple-dashes' => array( 'data---one---two---', '-One--Two---' ),
+
+ // Unexpected but recognized custom data attributes.
+ 'Only comprising a prefix' => array( 'data-', '' ),
+ 'With upper case ASCII' => array( 'data-Post-ID', 'postId' ),
+ 'With medial upper casing' => array( 'data-uPPer-cAsE', 'upperCase' ),
+ 'With Unicode whitespace' => array( "data-\u{2003}", "\u{2003}" ),
+ 'With Emoji' => array( 'data-🐄-pasture', '🐄Pasture' ),
+ 'Brackets and colon' => array( 'data-[wish:granted]', '[wish:granted]' ),
+
+ // Pens and Pencils: a collection of interesting combinations of dash and underscore.
+ 'data-pens-and-pencils' => array( 'data-pens-and-pencils', 'pensAndPencils' ),
+ 'data-pens--and--pencils' => array( 'data-pens--and--pencils', 'pens-And-Pencils' ),
+ 'data--pens--and--pencils' => array( 'data--pens--and--pencils', 'Pens-And-Pencils' ),
+ 'data---pens---and---pencils' => array( 'data---pens---and---pencils', '-Pens--And--Pencils' ),
+ 'data-pens-and-pencils-' => array( 'data-pens-and-pencils-', 'pensAndPencils-' ),
+ 'data-pens-and-pencils--' => array( 'data-pens-and-pencils--', 'pensAndPencils--' ),
+ 'data-pens_and_pencils__' => array( 'data-pens_and_pencils__', 'pens_and_pencils__' ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/js-interop.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>