Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,15 @@ def __init__(self, xhtml: bool = False) -> None:

def xpath_checked_pseudo(self, xpath: XPathExpr) -> XPathExpr:
# FIXME: is this really all the elements?
type_attr = (
"translate(@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', "
"'abcdefghijklmnopqrstuvwxyz')"
)
return xpath.add_condition(
"(@selected and name(.) = 'option') or "
"(@checked "
"and (name(.) = 'input' or name(.) = 'command')"
"and (@type = 'checkbox' or @type = 'radio'))"
f"and ({type_attr} = 'checkbox' or {type_attr} = 'radio'))"
)

def xpath_lang_function(self, xpath: XPathExpr, function: Function) -> XPathExpr:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,38 @@ def langid(selector: str) -> list[str]:
assert HTMLTranslator(xhtml=True).css_to_xpath("A") == ("descendant-or-self::A")
assert HTMLTranslator().css_to_xpath("A") == ("descendant-or-self::a")

def test_html_checked_type_case(self) -> None:
document = etree.fromstring(
'<div><input id="checkbox" type="checkbox" checked=""/>'
'<input id="uppercase-checkbox" type="CHECKBOX" checked=""/>'
'<input id="mixed-checkbox" type="ChEcKbOx" checked=""/>'
'<input id="radio" type="radio" checked=""/>'
'<input id="uppercase-radio" type="RADIO" checked=""/>'
'<input id="mixed-radio" type="RaDiO" checked=""/>'
'<input id="unchecked" type="CHECKBOX"/>'
'<input id="unchecked-radio" type="RADIO"/>'
'<input id="text" type="TEXT" checked=""/>'
'<input id="missing-type" checked=""/>'
'<input id="non-ascii-type" type="RADİO" checked=""/>'
'<option id="selected" selected=""/>'
'<option id="unselected"/></div>'
)
for translator in (HTMLTranslator(), HTMLTranslator(xhtml=True)):
items = typing.cast(
"list[etree._Element]",
document.xpath(translator.css_to_xpath(":checked")),
)
assert [element.get("id") for element in items] == [
"checkbox",
"uppercase-checkbox",
"mixed-checkbox",
"radio",
"uppercase-radio",
"mixed-radio",
"selected",
]
assert document.xpath(GenericTranslator().css_to_xpath(":checked")) == []

def test_argument_types(self) -> None:
class CustomTranslator(GenericTranslator):
def __init__(self) -> None:
Expand Down