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: 6 additions & 0 deletions cssselect/xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ def xpath_class(self, class_selector):
def xpath_hash(self, id_selector):
"""Translate an ID selector."""
xpath = self.xpath(id_selector.selector)
is_valid_identifier = not (re.match('--', id_selector.id)
or re.match('[0-9]', id_selector.id)
or re.match('-[0-9]', id_selector.id)
)
if not is_valid_identifier:
raise ExpressionError("invalid identifier")
return self.xpath_attrib_equals(xpath, '@id', id_selector.id)

def xpath_element(self, selector):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_cssselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ def xpath(css):
self.assertRaises(ExpressionError, xpath, ':lorem-ipsum')
self.assertRaises(ExpressionError, xpath, ':lorem(ipsum)')
self.assertRaises(ExpressionError, xpath, '::lorem-ipsum')
self.assertRaises(ExpressionError, xpath, '#00FF55')
self.assertRaises(ExpressionError, xpath, '#--abc')
self.assertRaises(ExpressionError, xpath, '#-1abc')
self.assertRaises(TypeError, GenericTranslator().css_to_xpath, 4)
self.assertRaises(TypeError, GenericTranslator().selector_to_xpath,
'foo')
Expand Down