Skip to content

Commit d215e06

Browse files
Copilotcodingjoe
andcommitted
Fix render method to keep data attributes only on wrapper element
Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
1 parent 8878e3e commit d215e06

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

s3file/forms.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,30 @@ def build_attrs(self, *args, **kwargs):
9898

9999
def render(self, name, value, attrs=None, renderer=None):
100100
"""Render the widget wrapped in a custom element for Safari compatibility."""
101-
# Build attributes for the render
101+
# Build attributes for the render - this includes data-* attributes
102102
if attrs is None:
103103
attrs = {}
104104

105-
# Get all the attributes including data-* attributes
105+
# Get all the attributes including data-* attributes from build_attrs
106106
final_attrs = self.build_attrs(self.attrs, attrs)
107107

108108
# Separate data-* attributes for the wrapper from other attributes for the input
109109
wrapper_attrs = {k: v for k, v in final_attrs.items() if k.startswith("data-")}
110110
input_attrs = {k: v for k, v in final_attrs.items() if not k.startswith("data-")}
111111

112-
# Call parent's render with only non-data attributes
113-
# We need to temporarily set attrs to avoid double-adding data attributes
114-
original_attrs = self.attrs
115-
self.attrs = {}
116-
input_html = super().render(name, value, input_attrs, renderer)
117-
self.attrs = original_attrs
112+
# Temporarily override build_attrs to return only non-data attributes for the input
113+
original_build_attrs = self.build_attrs
114+
def build_attrs_without_data(*args, **kwargs):
115+
attrs_dict = original_build_attrs(*args, **kwargs)
116+
return {k: v for k, v in attrs_dict.items() if not k.startswith("data-")}
117+
118+
self.build_attrs = build_attrs_without_data
119+
try:
120+
# Call parent's render which will use our modified build_attrs
121+
input_html = super().render(name, value, input_attrs, renderer)
122+
finally:
123+
# Restore original build_attrs
124+
self.build_attrs = original_build_attrs
118125

119126
# Build wrapper attributes string
120127
from django.utils.html import format_html_join

0 commit comments

Comments
 (0)