Skip to content
Merged
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
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ For attributes `class` and `for` which conflict with Python's [reserved keywords
|cls | fr |
|className|htmlFor|
|class_name|html_for|
|klass|phor|


```python
Expand All @@ -139,27 +140,54 @@ print(test)
<label class="classname anothername" for="someinput"></label>
```

Use `data_*` for [custom HTML5 data attributes](http://www.w3.org/html/wg/drafts/html/master/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes "HTML5 Data Attributes").
You can also modify the attributes of tags through a dictionary-like interface:

```python
test = div(data_employee='101011')
print(test)
header = div()
header['id'] = 'header'
print(header)
```
```html
<div id="header"></div>
```

### Dashed Attributes

Dashed attributes for `data_` and `aria_` are supported by default:

```python
from dominate.tags import div

print(
div(data_employee='101011'),
div(aria_role='button'),
)
```
```html
<div data-employee="101011"></div>
<div aria-role="button"></div>
```

You can also modify the attributes of tags through a dictionary-like interface:
If you using a 3rd party library like HTMX, Unpoly, or AlpineJS that uses dashed attrs, you will
need to configure dominate accordingly:

```python
header = div()
header['id'] = 'header'
print(header)
import dominate
from dominate.tags import div

dominate.dashed_attrs_add('hx_', 'x_')
print(
div(hx_target='/foo'),
div(x_show='open'),
)
```

```html
<div id="header"></div>
<div hx-target="/foo"></div>
<div x-show="open"></div>
```


Complex Structures
------------------

Expand Down
1 change: 1 addition & 0 deletions dominate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .version import __version__, version
from .document import document
from .dom_tag import DASHED_ATTRS, dashed_attrs_add, dashed_attrs_reset
12 changes: 0 additions & 12 deletions dominate/community/htmx.py

This file was deleted.

14 changes: 13 additions & 1 deletion dominate/dom_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
except ImportError:
greenlet = None

# These prefixes will be converted to dashed html attributes.
DASHED_ATTRS = ['data_', 'aria_']
_DASHED_ATTRS_ORIG = DASHED_ATTRS[:]

def dashed_attrs_add(*args: tuple[str]):
DASHED_ATTRS.extend(args)


def dashed_attrs_reset():
DASHED_ATTRS.clear()
DASHED_ATTRS.extend(_DASHED_ATTRS_ORIG)

# We want dominate to work in async contexts - however, the problem is
# when we bind a tag using "with", we set what is essentially a global variable.
# If we are processing multiple documents at the same time, one context
Expand Down Expand Up @@ -447,7 +459,7 @@ def clean_attribute(attribute):
attribute = attribute[1:]

# Workaround for dash
special_prefix = any([attribute.startswith(x) for x in ('data_', 'aria_')])
special_prefix = any([attribute.startswith(x) for x in DASHED_ATTRS])
if attribute in set(['http_equiv']) or special_prefix:
attribute = attribute.replace('_', '-').lower()

Expand Down
7 changes: 0 additions & 7 deletions tests/community/test_htmx.py

This file was deleted.

14 changes: 13 additions & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dominate.tags import *
from dominate.util import raw

import dominate

def test_arguments():
assert html(body(h1('Hello, pyy!'))).render() == \
Expand Down Expand Up @@ -366,3 +366,15 @@ class Card(div):
assert Card().render() == '<div></div>'

assert Card(tagname='div').render() == '<div></div>'


def test_custom_dashed_attrs():
assert div(hx_post='/clicked').render() == '<div hx_post="/clicked"></div>'

dominate.dashed_attrs_add('hx_', 'un_')
assert div(hx_post='/clicked').render() == '<div hx-post="/clicked"></div>'
assert div(un_post='/clicked').render() == '<div un-post="/clicked"></div>'

# Ensure the reset works
dominate.dashed_attrs_reset()
assert div(hx_post='/clicked').render() == '<div hx_post="/clicked"></div>'