Skip to content

Commit fe485d3

Browse files
committed
refine camel to dash conversion
1 parent 8a00c65 commit fe485d3

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/idom/utils.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,6 @@ def _hypen_to_camel_case(string: str) -> str:
289289
return first.lower() + remainder.title().replace("-", "")
290290

291291

292-
# Pattern for delimitting camelCase names (e.g. camelCase to camel-case)
293-
_CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
294-
295-
# see list of HTML attributes with dashes in them:
296-
# https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#attribute_list
297-
_CAMEL_TO_DASH_CASE_HTML_ATTRS = {
298-
"acceptCharset": "accept-charset",
299-
"httpEquiv": "http-equiv",
300-
}
301-
302-
303292
def _vdom_attr_to_html_str(key: str, value: Any) -> tuple[str, str]:
304293
if key == "style":
305294
if isinstance(value, dict):
@@ -309,11 +298,15 @@ def _vdom_attr_to_html_str(key: str, value: Any) -> tuple[str, str]:
309298
f"{_CAMEL_CASE_SUB_PATTERN.sub('-', k).lower()}:{v}"
310299
for k, v in value.items()
311300
)
312-
elif key.startswith("data") or key.startswith("aria"):
313-
# Handle data-* attribute names
301+
elif (
302+
# camel to data-* attributes
303+
key.startswith("data")
304+
# camel to aria-* attributes
305+
or key.startswith("aria")
306+
# handle special cases
307+
or key in _DASHED_HTML_ATTRS
308+
):
314309
key = _CAMEL_CASE_SUB_PATTERN.sub("-", key)
315-
else:
316-
key = _CAMEL_TO_DASH_CASE_HTML_ATTRS.get(key, key)
317310

318311
assert not callable(
319312
value
@@ -322,3 +315,11 @@ def _vdom_attr_to_html_str(key: str, value: Any) -> tuple[str, str]:
322315
# Again, we lower the attribute name only to normalize - HTML is case-insensitive:
323316
# http://w3c.github.io/html-reference/documents.html#case-insensitivity
324317
return key.lower(), str(value)
318+
319+
320+
# Pattern for delimitting camelCase names (e.g. camelCase to camel-case)
321+
_CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
322+
323+
# see list of HTML attributes with dashes in them:
324+
# https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#attribute_list
325+
_DASHED_HTML_ATTRS = {"acceptCharset", "httpEquiv"}

tests/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import idom
66
from idom import html
7-
from idom.utils import HTMLParseError, html_to_vdom, vdom_to_html
7+
from idom.utils import _DASHED_HTML_ATTRS, HTMLParseError, html_to_vdom, vdom_to_html
88

99

1010
def test_basic_ref_behavior():

0 commit comments

Comments
 (0)