|
1 | 1 | import base64 |
| 2 | +import html |
2 | 3 | import logging |
3 | 4 | import pathlib |
4 | 5 | import uuid |
| 6 | +from html.parser import HTMLParser |
5 | 7 |
|
6 | 8 | from django.conf import settings |
7 | 9 | from django.templatetags.static import static |
|
16 | 18 | logger = logging.getLogger("s3file") |
17 | 19 |
|
18 | 20 |
|
| 21 | +class InputToS3FileRewriter(HTMLParser): |
| 22 | + """ |
| 23 | + HTML parser that rewrites <input type="file"> tags to <s3-file> custom elements. |
| 24 | +
|
| 25 | + This provides a robust way to transform Django's rendered file input widgets |
| 26 | + into custom elements, handling various attribute orderings and formats. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + self.output = [] |
| 32 | + |
| 33 | + def handle_starttag(self, tag, attrs): |
| 34 | + if tag == "input": |
| 35 | + attrs_dict = dict(attrs) |
| 36 | + if attrs_dict.get("type") == "file": |
| 37 | + # Replace with s3-file custom element |
| 38 | + self._write_s3_file_tag(attrs) |
| 39 | + return |
| 40 | + |
| 41 | + # For all other tags, preserve as-is |
| 42 | + self.output.append(self.get_starttag_text()) |
| 43 | + |
| 44 | + def handle_endtag(self, tag): |
| 45 | + self.output.append(f"</{tag}>") |
| 46 | + |
| 47 | + def handle_data(self, data): |
| 48 | + self.output.append(data) |
| 49 | + |
| 50 | + def handle_startendtag(self, tag, attrs): |
| 51 | + # For self-closing tags |
| 52 | + if tag == "input": |
| 53 | + attrs_dict = dict(attrs) |
| 54 | + if attrs_dict.get("type") == "file": |
| 55 | + # Replace with s3-file custom element |
| 56 | + self._write_s3_file_tag(attrs) |
| 57 | + return |
| 58 | + |
| 59 | + self.output.append(self.get_starttag_text()) |
| 60 | + |
| 61 | + def _write_s3_file_tag(self, attrs): |
| 62 | + """Write the s3-file opening tag with all attributes except type.""" |
| 63 | + self.output.append("<s3-file") |
| 64 | + for name, value in attrs: |
| 65 | + if name != "type": # Skip type attribute |
| 66 | + if value is None: |
| 67 | + self.output.append(f" {name}") |
| 68 | + else: |
| 69 | + escaped_value = html.escape(value, quote=True) |
| 70 | + self.output.append(f' {name}="{escaped_value}"') |
| 71 | + self.output.append(">") |
| 72 | + |
| 73 | + def get_html(self): |
| 74 | + """Return the transformed HTML.""" |
| 75 | + return "".join(self.output) |
| 76 | + |
| 77 | + |
19 | 78 | @html_safe |
20 | 79 | class Asset: |
21 | 80 | """A generic asset that can be included in a template.""" |
@@ -99,11 +158,10 @@ def build_attrs(self, *args, **kwargs): |
99 | 158 |
|
100 | 159 | def render(self, name, value, attrs=None, renderer=None): |
101 | 160 | """Render the widget as a custom element for Safari compatibility.""" |
102 | | - return mark_safe( # noqa: S308 |
103 | | - str(super().render(name, value, attrs=attrs, renderer=renderer)).replace( |
104 | | - f'<input type="{self.input_type}"', "<s3-file" |
105 | | - ) |
106 | | - ) |
| 161 | + html_output = str(super().render(name, value, attrs=attrs, renderer=renderer)) |
| 162 | + parser = InputToS3FileRewriter() |
| 163 | + parser.feed(html_output) |
| 164 | + return mark_safe(parser.get_html()) # noqa: S308 |
107 | 165 |
|
108 | 166 | def get_conditions(self, accept): |
109 | 167 | conditions = [ |
|
0 commit comments