Skip to content

fix data-* and aria-* attribute name conversion #911

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/source/about/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ more info, see the :ref:`Contributor Guide <Creating a Changelog Entry>`.
Unreleased
----------

No changes.
**Fixed**

- :pull:`911` - fixed ``data-*`` and ``aria-*`` attribute name conversions. Previously
an attribute declared as ``data_some_thing`` got converted to ``data-some_thing``
instead of ``data-some-thing``.


v1.0.0-a3
Expand Down
2 changes: 1 addition & 1 deletion src/client/packages/idom-client-react/src/element-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function normalizeAttribute([key, value]) {
key.startsWith("aria_") ||
DASHED_HTML_ATTRS.includes(key)
) {
normKey = key.replace("_", "-");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dang stack overflow lied to me. Will switch to that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, replaceAll was standardized fairly recently (mid-2020).

Copy link
Contributor

@Archmonger Archmonger Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given replaceAll was only fully adopted by all browsers early 2021...

For compatibility purposes, it might make sense to keep using key.split("_").join("-")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a comment to this effect.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a __ could stand for an actual _ in the attribute name. Also, the regex in snakeToCamel could be more permissive, e.g., uppercase anything that's allowed, cf. comment here. Or, normalize the attribute name (i.e., "project" to ascii) and complain if not equal.

normKey = key.split("_").join("-");
} else {
normKey = snakeToCamel(key);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,23 @@ async def handle_change(event):
await inp.type("hello", delay=DEFAULT_TYPE_DELAY)

assert (await inp.evaluate("node => node.value")) == "hello"


async def test_data_and_aria_attribute_naming(display: DisplayFixture):
@idom.component
def SomeComponent():
return idom.html.h1(
"see attributes",
id="title",
data_some_thing="some data",
aria_description="some title",
**{"data-another-thing": "more data"},
)

await display.show(SomeComponent)

title = await display.page.wait_for_selector("#title")

assert await title.get_attribute("data-some-thing") == "some data"
assert await title.get_attribute("data-another-thing") == "more data"
assert await title.get_attribute("aria-description") == "some title"