diff options
| author | Stefan Behnel <stefan_ml@behnel.de> | 2020-08-12 07:35:04 +0200 |
|---|---|---|
| committer | Stefan Behnel <stefan_ml@behnel.de> | 2020-08-12 07:35:04 +0200 |
| commit | fcf0efcbb256d48b75cc6c4d0766d1643c6086ea (patch) | |
| tree | c5a12600874040389664b48d48b094a64ffcf898 /src | |
| parent | fa734e0980972548258261a02e756b889a17ce96 (diff) | |
| download | python-lxml-fcf0efcbb256d48b75cc6c4d0766d1643c6086ea.tar.gz | |
html: Avoid XPath in InputGetter where fast and simple iteration is enough.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lxml/html/__init__.py | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/src/lxml/html/__init__.py b/src/lxml/html/__init__.py index c909f050..6649268b 100644 --- a/src/lxml/html/__init__.py +++ b/src/lxml/html/__init__.py @@ -1183,8 +1183,6 @@ class InputGetter(object): checkboxes and radio elements are returned individually. """ - _name_xpath = etree.XPath(".//*[@name = $name and (local-name(.) = 'select' or local-name(.) = 'input' or local-name(.) = 'textarea')]") - def __init__(self, form): self.form = form @@ -1197,27 +1195,28 @@ class InputGetter(object): ## a dictionary-like object or list-like object def __getitem__(self, name): - results = self._name_xpath(self.form, name=name) - if results: - type = results[0].get('type') - if type == 'radio' and len(results) > 1: - group = RadioGroup(results) - group.name = name - return group - elif type == 'checkbox' and len(results) > 1: - group = CheckboxGroup(results) - group.name = name - return group - else: - # I don't like throwing away elements like this - return results[0] + fields = [field for field in self if field.get('name') == name] + if not fields: + raise KeyError("No input element with the name %r" % name) + + input_type = fields[0].get('type') + if input_type == 'radio' and len(fields) > 1: + group = RadioGroup(fields) + group.name = name + return group + elif input_type == 'checkbox' and len(fields) > 1: + group = CheckboxGroup(fields) + group.name = name + return group else: - raise KeyError( - "No input element with the name %r" % name) + # I don't like throwing away elements like this + return fields[0] def __contains__(self, name): - results = self._name_xpath(self.form, name=name) - return bool(results) + for field in self: + if field.get('name') == name: + return True + return False def keys(self): names = set() |
