Value property to PyDom.Element and ElementCollection (#1828)

* add base test for input value field

* add value property to Element

* add test for non supported element

* prevent users to set value attribute on elements that do not support it

* add test for setting value on collections

* add value property to collection and add more tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Fabio Pliger
2023-11-01 10:33:38 -05:00
committed by GitHub
parent 54df7171a2
commit e81830a2ea
4 changed files with 79 additions and 7 deletions

View File

@@ -131,6 +131,22 @@ class Element(BaseElement):
def id(self, value):
self._js.id = value
@property
def value(self):
return self._js.value
@value.setter
def value(self, value):
# in order to avoid confusion to the user, we don't allow setting the
# value of elements that don't have a value attribute
if not hasattr(self._js, "value"):
raise AttributeError(
f"Element {self._js.tagName} has no value attribute. If you want to "
"force a value attribute, set it directly using the `_js.value = <value>` "
"javascript API attribute instead."
)
self._js.value = value
def clone(self, new_id=None):
clone = Element(self._js.cloneNode(True))
clone.id = new_id
@@ -264,6 +280,14 @@ class ElementCollection:
def html(self, value):
self._set_attribute("html", value)
@property
def value(self):
return self._get_attribute("value")
@value.setter
def value(self, value):
self._set_attribute("value", value)
@property
def children(self):
return self._elements