Skip to content

Commit a90d36d

Browse files
authored
Merge pull request #2 from openculinary/issue-1/empty-attribute-search
Support XPath attribute-existence search for empty-valued HTML element attributes
2 parents ca88466 + 6047341 commit a90d36d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

htmlement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def _handle_starttag(self, tag, attrs, self_closing=False):
330330
# Add tag element to tree if we have no filter or that the filter matches
331331
if enabled or self._search(tag, attrs):
332332
# Convert attrs to dictionary
333-
attrs = dict(attrs) if attrs else {}
333+
attrs = {k: v or "" for k, v in attrs}
334334
self._flush()
335335

336336
# Create the new element

tests/test_module.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ def test_extra_tag():
129129
assert Etree.tostring(root, method="html") == b'<html><body></body></html>'
130130

131131

132+
def test_find_empty_attribute():
133+
# Check whether we can find an element with an empty-valued attribute
134+
html = "<html><body><form autofocus><input type='checkbox' checked></form></body></html>"
135+
form = quick_parse_filter(html, "form", {"autofocus": True})
136+
assert "autofocus" in form.attrib
137+
assert form.find(".//input[@checked]") is not None
138+
139+
132140
# ############################# HTML Entity ############################## #
133141

134142

0 commit comments

Comments
 (0)