Skip to content

Commit f9d0df7

Browse files
committed
Add autocomplete attribute
1 parent 0905ff4 commit f9d0df7

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# News in version 2.1.0
2+
3+
## API Additions
4+
5+
* Add `autocomplete` attribute to `Input`, `TextArea`, `Select`, and `Form`.
6+
17
# News in version 2.0.0
28

39
## API Additions

htmlgen/form.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class Form(Element):
3434
"""
3535

3636
def __init__(self, method="GET", url=""):
37-
super(Form, self).__init__("form")
37+
super().__init__("form")
3838
self.method = method
3939
self.url = url
4040

4141
method = html_attribute("method", default="GET")
4242
url = html_attribute("action", default="")
4343
target = html_attribute("target", "_self")
4444
encryption_type = html_attribute("enctype", _ENC_TYPE_URL_ENCODED)
45+
autocomplete = html_attribute("autocomplete")
4546

4647
def set_blank_target(self):
4748
self.target = "_blank"
@@ -80,7 +81,7 @@ def __init__(self, type_="text", name=""):
8081
submitting a form.
8182
8283
"""
83-
super(Input, self).__init__("input")
84+
super().__init__("input")
8485
self.type = type_
8586
self.name = name
8687

@@ -89,6 +90,7 @@ def __init__(self, type_="text", name=""):
8990
readonly = boolean_html_attribute("readonly")
9091
disabled = boolean_html_attribute("disabled")
9192
type = html_attribute("type")
93+
autocomplete = html_attribute("autocomplete")
9294
placeholder = html_attribute("placeholder")
9395
size = int_html_attribute("size")
9496
focus = boolean_html_attribute("autofocus")
@@ -380,14 +382,15 @@ class TextArea(Element):
380382
"""
381383

382384
def __init__(self, name=""):
383-
super(TextArea, self).__init__("textarea")
385+
super().__init__("textarea")
384386
self.name = name
385387

386388
name = html_attribute("name", default="")
387389
readonly = boolean_html_attribute("readonly")
388390
disabled = boolean_html_attribute("disabled")
389391
columns = int_html_attribute("cols")
390392
rows = int_html_attribute("rows")
393+
autocomplete = html_attribute("autocomplete")
391394
placeholder = html_attribute("placeholder")
392395

393396

@@ -417,11 +420,12 @@ class Select(Element):
417420
"""
418421

419422
def __init__(self, name=""):
420-
super(Select, self).__init__("select")
423+
super().__init__("select")
421424
self.name = name
422425

423426
name = html_attribute("name", default="")
424427
disabled = boolean_html_attribute("disabled")
428+
autocomplete = html_attribute("autocomplete")
425429

426430
def create_group(self, label):
427431
"""Create and append an option group."""

htmlgen/form.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Form(Element):
99
url: str
1010
target: str
1111
encryption_type: str
12+
autocomplete: Optional[str]
1213
multipart: bool
1314
def __init__(self, method: str = ..., url: str = ...) -> None: ...
1415
def set_blank_target(self) -> None: ...
@@ -19,6 +20,7 @@ class Input(VoidElement):
1920
readonly: bool
2021
disabled: bool
2122
type: str
23+
autocomplete: Optional[str]
2224
placeholder: Optional[str]
2325
size: Optional[int]
2426
focus: bool
@@ -87,12 +89,14 @@ class TextArea(Element):
8789
disabled: bool
8890
columns: Optional[int]
8991
rows: Optional[int]
92+
autocomplete: Optional[str]
9093
placeholder: Optional[str]
9194
def __init__(self, name: str = ...) -> None: ...
9295

9396
class Select(Element):
9497
name: str
9598
disabled: bool
99+
autocomplete: Optional[str]
96100
selected_option: Optional[Option]
97101
selected_value: Optional[str]
98102
def __init__(self, name: str = ...) -> None: ...

test_htmlgen/form.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,23 @@ def test_implicit_arguments(self):
3939
assert_equal("PUT", form.method)
4040
assert_equal("/test", form.url)
4141
assert_equal("_self", form.target)
42+
assert_is_none(form.autocomplete)
4243
assert_equal(
4344
[b'<form action="/test" method="PUT">', b"</form>"],
4445
list(iter(form)),
4546
)
4647

48+
def test_arguments(self):
49+
form = Form("PUT", "/test")
50+
form.autocomplete = "username"
51+
assert_equal(
52+
[
53+
b'<form action="/test" autocomplete="username" method="PUT">',
54+
b"</form>",
55+
],
56+
list(iter(form)),
57+
)
58+
4759
def test_default_arguments(self):
4860
form = Form()
4961
assert_equal("GET", form.method)
@@ -90,13 +102,14 @@ def test_defaults(self):
90102

91103
def test_attributes(self):
92104
input_ = Input()
93-
input_.placeholder = "Foo"
94105
input_.size = 5
95106
input_.value = "My Value"
107+
input_.autocomplete = "username"
108+
input_.placeholder = "Foo"
96109
assert_equal(
97110
[
98-
b'<input placeholder="Foo" size="5" type="text" '
99-
b'value="My Value"/>'
111+
b'<input autocomplete="username" placeholder="Foo" size="5" '
112+
b'type="text" value="My Value"/>'
100113
],
101114
list(iter(input_)),
102115
)
@@ -433,11 +446,20 @@ def test_without_name(self):
433446
text_area = TextArea()
434447
assert_equal("<textarea></textarea>", str(text_area))
435448

449+
def test_attributes(self):
450+
text_area = TextArea()
451+
text_area.autocomplete = "username"
452+
assert_equal(
453+
'<textarea autocomplete="username"></textarea>', str(text_area),
454+
)
455+
436456

437457
class SelectTest(TestCase):
438458
def test_attributes(self):
439459
select = Select()
460+
select.autocomplete = "username"
440461
assert_false(select.disabled)
462+
assert_equal('<select autocomplete="username"></select>', str(select))
441463

442464
def test_with_name(self):
443465
select = Select("my-name")

0 commit comments

Comments
 (0)