Skip to content

Commit f52371a

Browse files
committed
css "^" as "." xpath symbol to use css "^ >" to get immediate children
1 parent cb7a7e2 commit f52371a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
/dist
66
/docs/_build
77
/.coverage
8-
.idea
8+
.idea
9+
/venv
10+
*.vscode

cssselect/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ def parse_simple_selector(stream, inside_negation=False):
400400
stream.skip_whitespace()
401401
selector_start = len(stream.used)
402402
peek = stream.peek()
403-
if peek.type == 'IDENT' or peek == ('DELIM', '*'):
404-
if peek.type == 'IDENT':
403+
if peek.type == 'IDENT' or peek == ('DELIM', '*') or peek == ('DELIM', '^'):
404+
if peek.type == 'IDENT' or peek == ('DELIM', '^'):
405405
namespace = stream.next().value
406406
else:
407407
stream.next()

cssselect/xpath.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ def css_to_xpath(self, css, prefix='descendant-or-self::'):
187187
The equivalent XPath 1.0 expression as an Unicode string.
188188
189189
"""
190+
# no prefix if css immediate children (example: css "^ > div" to xpath "./div")
191+
child_re = r'^[ \t\r\n\f]*\^[ \t\r\n\f]*>'
192+
if re.match(child_re, css):
193+
prefix = ''
194+
# prefix = 'child::'
195+
# css = re.sub(child_re, '', css)
196+
# print('*' * 50)
197+
# print(css)
190198
return ' | '.join(self.selector_to_xpath(selector, prefix,
191199
translate_pseudo_elements=True)
192200
for selector in parse(css))
@@ -332,6 +340,9 @@ def xpath_element(self, selector):
332340
if not element:
333341
element = '*'
334342
safe = True
343+
if element == '^':
344+
element = '.'
345+
safe = True
335346
else:
336347
safe = is_safe_name(element)
337348
if self.lower_case_element_names:

0 commit comments

Comments
 (0)