Skip to content

Commit a35eb3e

Browse files
Copilotcodingjoe
andcommitted
Refactor HTML parser to reduce code duplication and improve documentation
Co-authored-by: codingjoe <1772890+codingjoe@users.noreply.github.com>
1 parent 5a3a1f1 commit a35eb3e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

s3file/forms.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ def __init__(self):
3030
super().__init__()
3131
self.output = []
3232

33+
def _is_file_input(self, attrs):
34+
"""Check if attributes indicate a file input element."""
35+
attrs_dict = dict(attrs)
36+
return attrs_dict.get("type") == "file"
37+
3338
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
39+
if tag == "input" and self._is_file_input(attrs):
40+
# Replace with s3-file custom element
41+
self._write_s3_file_tag(attrs)
42+
return
4043

4144
# For all other tags, preserve as-is
4245
self.output.append(self.get_starttag_text())
@@ -49,17 +52,19 @@ def handle_data(self, data):
4952

5053
def handle_startendtag(self, tag, attrs):
5154
# 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
55+
if tag == "input" and self._is_file_input(attrs):
56+
# Replace with s3-file custom element
57+
self._write_s3_file_tag(attrs)
58+
return
5859

5960
self.output.append(self.get_starttag_text())
6061

6162
def _write_s3_file_tag(self, attrs):
62-
"""Write the s3-file opening tag with all attributes except type."""
63+
"""
64+
Write the s3-file opening tag with all attributes except type.
65+
66+
Note: This creates an opening tag that requires a corresponding closing tag.
67+
"""
6368
self.output.append("<s3-file")
6469
for name, value in attrs:
6570
if name != "type": # Skip type attribute

0 commit comments

Comments
 (0)