-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Introduce wp_js_dataset_name() for custom data attributes. #9953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
a94d150 to
a2d5a53
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
233a214 to
f85c1a8
Compare
5142726 to
e467ef3
Compare
25ecd94 to
86333ab
Compare
1a1a75d to
d9f2e6a
Compare
|
@peterwilsoncc @westonruter do either of you have thoughts about the structure of this proposed new module? It serves the work opening up custom data attributes by providing the necessary functions to parse them. But it introduces another module in the top-level of |
|
@dmsnell I love this. In regards to where the functions should be located, to me it seems they may make sense being added to |
|
@westonruter I like this idea, but once I started implementing it I started liking it less.
my idea with I added your changes in bb63d3c, so I would like to know your thoughts after the change and if you think it’s worth the tradeoffs to group it in that existing file. |
Co-authored-by: Weston Ruter <westonruter@git.wordpress.org>
790741c to
4fa1e2c
Compare
|
@westonruter I’m going to merge as-is, but I would really love some additional thought on this: if we leave it in I expect the if we identify problems during the Beta period we can move these back into their own new module. |
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 #9953 Discussed in https://core.trac.wordpress.org/ticket/61501 Props dmsnell, westonruter. See #61501. git-svn-id: https://develop.svn.wordpress.org/trunk@61007 602fd350-edb4-49c9-b593-d223f7449a82
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 WordPress/wordpress-develop#9953 Discussed in https://core.trac.wordpress.org/ticket/61501 Props dmsnell, westonruter. See #61501. Built from https://develop.svn.wordpress.org/trunk@61007 git-svn-id: http://core.svn.wordpress.org/trunk@60343 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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 WordPress/wordpress-develop#9953 Discussed in https://core.trac.wordpress.org/ticket/61501 Props dmsnell, westonruter. See #61501. Built from https://develop.svn.wordpress.org/trunk@61007 git-svn-id: https://core.svn.wordpress.org/trunk@60343 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Trac ticket: Core-61501
See: #6429
As part of opening up custom data attributes, it’s important for Core to know what is and what isn’t a custom data attribute. Further, it’s important to know what the translated name of a custom data attribute is when read via the JavaScript
.datasetproperty.This patch introduces two new functions to transform between these two representations. While this started as a one-way transform from custom attribute name to dataset property name, it became apparent that the reverse transform could be just as confusing in some cases as the forward transform. Therefore, a second function has been added to lower the risk of producing a mistaken custom data attribute.
With this patch, developers can call
wp_html_custom_data_attribute_name( 'postId' )to get the name of the proper HTML attribute name such that it will match the JS code, building correlations between the JS and PHP sources. In this case, it would returndata-post-id.More complicated cases make the relevance of having well-defined functions more obvious:
data-test<>testdata-1<>1data-😢-8<>😢-8data-one:more<>one:moredata-CAPS<>caps(<>data-caps)DATA--UPPER-alreadY<>UpperAlready(<>data--upper-already)data---one---two---<>-One--Two---data-<> ``