Skip to content

Commit

Permalink
With parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Dec 16, 2024
1 parent cbe1bb4 commit 3d63db5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/django_bootstrap5/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from django_bootstrap5.text import text_value
from django_bootstrap5.utils import get_url_attrs

from .core import get_bootstrap_setting


def starts_with_prefix(string, prefixes):
return any(string.startswith(prefix) for prefix in prefixes)


def render_script_tag(url):
"""Build a script tag."""
Expand All @@ -23,7 +29,7 @@ def render_tag(tag, attrs=None, content=None, close=True):
"""Render an HTML tag."""
if attrs:
for att_name, att_value in copy(attrs).items():
if "_" in att_name:
if starts_with_prefix(att_name, get_bootstrap_setting("hyphenate_attribute_prefixes")):
attrs[att_name.replace("_", "-")] = att_value
del attrs[att_name]
attrs_string = flatatt(attrs) if attrs else ""
Expand Down
41 changes: 31 additions & 10 deletions tests/test_bootstrap_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,37 @@ def test_button_type_link(self):
with self.assertRaises(ValueError):
self.render("{% bootstrap_button 'button' button_type='button' href='#' %}")

def test_button_extra_attributes(self):
link_button = (
'<button class="btn btn-outline-primary btn-lg something" hx-post="/something" data-value="some value" '
'aria-label="example" extra-attribute="something" type="reset" role="button">some button</button>'
def test_button_hyphenate_attributes(self):
# No attributes to hyphenate
self.assertHTMLEqual(
self.render("{% bootstrap_button 'button' href='#' %}"),
'<a class="btn btn-primary" href="#" role="button">button</a>',
)
# Default: data prefix
self.assertHTMLEqual(
self.render(
"{% bootstrap_button content='some button' button_type='reset' button_class='btn-outline-primary'"
" size='lg' hx_post='/something' data_value='some value' aria_label='example'"
" extra_classes='something' extra_attribute='something' role='button' %}"
),
link_button,
self.render("{% bootstrap_button 'button' href='#' data_foo='bar' %}"),
'<a class="btn btn-primary" href="#" role="button" data-foo="bar">button</a>',
)
# Default: no hx prefix
self.assertHTMLEqual(
self.render("{% bootstrap_button 'button' href='#' data_foo='bar' hx_xyz='abc' %}"),
'<a class="btn btn-primary" href="#" role="button" data-foo="bar" hx_xyz="abc">button</a>',
)
# Custom: data and hx
with self.settings(BOOTSTRAP5={"hyphenate_attribute_prefixes": ["data", "hx"]}):
self.assertHTMLEqual(
self.render("{% bootstrap_button 'button' href='#' data_foo='bar' hx_xyz='abc' %}"),
'<a class="btn btn-primary" href="#" role="button" data-foo="bar" hx-xyz="abc">button</a>',
)
# Custom: hx only
with self.settings(BOOTSTRAP5={"hyphenate_attribute_prefixes": ["hx"]}):
self.assertHTMLEqual(
self.render("{% bootstrap_button 'button' href='#' data_foo='bar' hx_xyz='abc' %}"),
'<a class="btn btn-primary" href="#" role="button" data_foo="bar" hx-xyz="abc">button</a>',
)
# Custom: empty list
with self.settings(BOOTSTRAP5={"hyphenate_attribute_prefixes": []}):
self.assertHTMLEqual(
self.render("{% bootstrap_button 'button' href='#' data_foo='bar' hx_xyz='abc' %}"),
'<a class="btn btn-primary" href="#" role="button" data_foo="bar" hx_xyz="abc">button</a>',
)

0 comments on commit 3d63db5

Please sign in to comment.