|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Core JavaScript Interoperability functions. |
| 5 | + * |
| 6 | + * @package WordPress |
| 7 | + * @subpackage JS-Interop |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Return the corresponding JavaScript `dataset` name for an attribute |
| 12 | + * if it represents a custom data attribute, or `null` if not. |
| 13 | + * |
| 14 | + * Custom data attributes appear in an element's `dataset` property in a |
| 15 | + * browser, but there's a specific way the names are translated from HTML |
| 16 | + * into JavaScript. This function indicates how the name would appear in |
| 17 | + * JavaScript if a browser would recognize it as a custom data attribute. |
| 18 | + * |
| 19 | + * Example: |
| 20 | + * |
| 21 | + * // Dash-letter pairs turn into capital letters. |
| 22 | + * 'postId' === wp_js_dataset_name( 'data-post-id' ); |
| 23 | + * 'Before' === wp_js_dataset_name( 'data--before' ); |
| 24 | + * '-One--Two---' === wp_js_dataset_name( 'data---one---two---' ); |
| 25 | + * |
| 26 | + * // Not every attribute name will be interpreted as a custom data attribute. |
| 27 | + * null === wp_js_dataset_name( 'post-id' ); |
| 28 | + * null === wp_js_dataset_name( 'data' ); |
| 29 | + * |
| 30 | + * // Some very surprising names will; for example, a property whose name is the empty string. |
| 31 | + * '' === wp_js_dataset_name( 'data-' ); |
| 32 | + * 0 === strlen( wp_js_dataset_name( 'data-' ) ); |
| 33 | + * |
| 34 | + * @since 6.9.0 |
| 35 | + * |
| 36 | + * @see https://html.spec.whatwg.org/#concept-domstringmap-pairs |
| 37 | + * @see wp_html_custom_data_attribute_name() |
| 38 | + * |
| 39 | + * @param string $html_attribute_name Raw attribute name as found in the source HTML. |
| 40 | + * @return string|null Transformed `dataset` name, if interpretable as a custom data attribute, else `null`. |
| 41 | + */ |
| 42 | +function wp_js_dataset_name( string $html_attribute_name ): ?string { |
| 43 | + if ( 0 !== substr_compare( $html_attribute_name, 'data-', 0, 5, true ) ) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + $end = strlen( $html_attribute_name ); |
| 48 | + |
| 49 | + /* |
| 50 | + * If it contains characters which would end the attribute name parsing then |
| 51 | + * something else is wrong and this contains more than just an attribute name. |
| 52 | + */ |
| 53 | + if ( ( $end - 5 ) !== strcspn( $html_attribute_name, "=/> \t\f\r\n", 5 ) ) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * > For each name in list, for each U+002D HYPHEN-MINUS character (-) |
| 59 | + * > in the name that is followed by an ASCII lower alpha, remove the |
| 60 | + * > U+002D HYPHEN-MINUS character (-) and replace the character that |
| 61 | + * > followed it by the same character converted to ASCII uppercase. |
| 62 | + * |
| 63 | + * @link https://html.spec.whatwg.org/#concept-domstringmap-pairs |
| 64 | + */ |
| 65 | + $custom_name = ''; |
| 66 | + $at = 5; |
| 67 | + $was_at = $at; |
| 68 | + |
| 69 | + while ( $at < $end ) { |
| 70 | + $next_dash_at = strpos( $html_attribute_name, '-', $at ); |
| 71 | + if ( false === $next_dash_at || $next_dash_at === $end - 1 ) { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + // Transform `-a` to `A`, for example. |
| 76 | + $c = $html_attribute_name[ $next_dash_at + 1 ]; |
| 77 | + if ( ( $c >= 'A' && $c <= 'Z' ) || ( $c >= 'a' && $c <= 'z' ) ) { |
| 78 | + $prefix = substr( $html_attribute_name, $was_at, $next_dash_at - $was_at ); |
| 79 | + $custom_name .= strtolower( $prefix ); |
| 80 | + $custom_name .= strtoupper( $c ); |
| 81 | + $at = $next_dash_at + 2; |
| 82 | + $was_at = $at; |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $at = $next_dash_at + 1; |
| 87 | + } |
| 88 | + |
| 89 | + // If nothing has been added it means there are no dash-letter pairs; return the name as-is. |
| 90 | + return '' === $custom_name |
| 91 | + ? strtolower( substr( $html_attribute_name, 5 ) ) |
| 92 | + : ( $custom_name . strtolower( substr( $html_attribute_name, $was_at ) ) ); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Returns a corresponding HTML attribute name for the given name, |
| 97 | + * if that name were found in a JS element’s `dataset` property. |
| 98 | + * |
| 99 | + * Example: |
| 100 | + * |
| 101 | + * 'data-post-id' === wp_html_custom_data_attribute_name( 'postId' ); |
| 102 | + * 'data--before' === wp_html_custom_data_attribute_name( 'Before' ); |
| 103 | + * 'data---one---two---' === wp_html_custom_data_attribute_name( '-One--Two---' ); |
| 104 | + * |
| 105 | + * // Not every attribute name will be interpreted as a custom data attribute. |
| 106 | + * null === wp_html_custom_data_attribute_name( '/not-an-attribute/' ); |
| 107 | + * null === wp_html_custom_data_attribute_name( 'no spaces' ); |
| 108 | + * |
| 109 | + * // Some very surprising names will; for example, a property whose name is the empty string. |
| 110 | + * 'data-' === wp_html_custom_data_attribute_name( '' ); |
| 111 | + * |
| 112 | + * @since 6.9.0 |
| 113 | + * |
| 114 | + * @see https://html.spec.whatwg.org/#concept-domstringmap-pairs |
| 115 | + * @see wp_js_dataset_name() |
| 116 | + * |
| 117 | + * @param string $js_dataset_name Name of JS `dataset` property to transform. |
| 118 | + * @return string|null Corresponding name of an HTML custom data attribute for the given dataset name, |
| 119 | + * if possible to represent in HTML, otherwise `null`. |
| 120 | + */ |
| 121 | +function wp_html_custom_data_attribute_name( string $js_dataset_name ): ?string { |
| 122 | + $end = strlen( $js_dataset_name ); |
| 123 | + if ( 0 === $end ) { |
| 124 | + return 'data-'; |
| 125 | + } |
| 126 | + |
| 127 | + /* |
| 128 | + * If it contains characters which would end the attribute name parsing then |
| 129 | + * something it’s not possible to represent this in HTML. |
| 130 | + */ |
| 131 | + if ( strcspn( $js_dataset_name, "=/> \t\f\r\n" ) !== $end ) { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + $html_name = 'data-'; |
| 136 | + $at = 0; |
| 137 | + $was_at = $at; |
| 138 | + |
| 139 | + while ( $at < $end ) { |
| 140 | + $next_upper_after = strcspn( $js_dataset_name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', $at ); |
| 141 | + $next_upper_at = $at + $next_upper_after; |
| 142 | + if ( $next_upper_at >= $end ) { |
| 143 | + break; |
| 144 | + } |
| 145 | + |
| 146 | + $prefix = substr( $js_dataset_name, $was_at, $next_upper_at - $was_at ); |
| 147 | + $html_name .= strtolower( $prefix ); |
| 148 | + $html_name .= '-' . strtolower( $js_dataset_name[ $next_upper_at ] ); |
| 149 | + $at = $next_upper_at + 1; |
| 150 | + $was_at = $at; |
| 151 | + } |
| 152 | + |
| 153 | + if ( $was_at < $end ) { |
| 154 | + $html_name .= strtolower( substr( $js_dataset_name, $was_at ) ); |
| 155 | + } |
| 156 | + |
| 157 | + return $html_name; |
| 158 | +} |
0 commit comments