Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tokenizer(self):
tokens = [
_unicode(item) for item in tokenize(
u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))]
assert tokens == [
u(""),
"<s>",
"' at 5>",
"<s>",
# the no-break space is not whitespace in CSS
u(""), # f\xa0
"",
"",
"",
"",
"",
"",
"",</s></s>
def test_tokenizer(self):
tokens = [
_unicode(item) for item in tokenize(
u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))]
assert tokens == [
u(""),
"<s>",
"' at 5>",
"<s>",
# the no-break space is not whitespace in CSS
u(""), # f\xa0
"",
"",
"",
"",
"",
"",
"",</s></s>
def test_unicode(self):
if sys.version_info[0] < 3:
css = '.a\xc1b'.decode('ISO-8859-1')
else:
css = '.a\xc1b'
xpath = GenericTranslator().css_to_xpath(css)
assert css[1:] in xpath
xpath = xpath.encode('ascii', 'xmlcharrefreplace').decode('ASCII')
assert xpath == (
"descendant-or-self::*[@class and contains("
"concat(' ', normalize-space(@class), ' '), ' aÁb ')]")
def test_unicode_escapes(self):
# \22 == '"' \20 == ' '
css_to_xpath = GenericTranslator().css_to_xpath
assert css_to_xpath(r'*[aval="\'\22\'"]') == (
'''descendant-or-self::*[@aval = concat("'",'"',"'")]''')
assert css_to_xpath(r'*[aval="\'\22 2\'"]') == (
'''descendant-or-self::*[@aval = concat("'",'"2',"'")]''')
assert css_to_xpath(r'*[aval="\'\20 \'"]') == (
'''descendant-or-self::*[@aval = "' '"]''')
assert css_to_xpath('*[aval="\'\\20\r\n \'"]') == (
'''descendant-or-self::*[@aval = "' '"]''')
def xpath(css):
return _unicode(GenericTranslator().css_to_xpath(css, prefix=''))
def test_quoting(self):
css_to_xpath = GenericTranslator().css_to_xpath
assert css_to_xpath('*[aval="\'"]') == (
'''descendant-or-self::*[@aval = "'"]''')
assert css_to_xpath('*[aval="\'\'\'"]') == (
"""descendant-or-self::*[@aval = "'''"]""")
assert css_to_xpath('*[aval=\'"\']') == (
'''descendant-or-self::*[@aval = '"']''')
assert css_to_xpath('*[aval=\'"""\']') == (
'''descendant-or-self::*[@aval = '"""']''')
def series(css):
selector, = parse(':nth-child(%s)' % css)
args = selector.parsed_tree.arguments
try:
return parse_series(args)
except ValueError:
return None
def specificity(css):
selectors = parse(css)
assert len(selectors) == 1
return selectors[0].specificity()
def repr_parse(css):
selectors = parse(css)
for selector in selectors:
assert selector.pseudo_element is None
return [repr(selector.parsed_tree).replace("(u'", "('")
for selector in selectors]
def get_error(css):
try:
parse(css)
except SelectorSyntaxError:
# Py2, Py3, ...
return str(sys.exc_info()[1]).replace("(u'", "('")