Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['.a', ',', '.b'], 0).parse(sc)
for i in [
(['&', '.scope', ' ', 'a'], '.a.scope a,\n.b.scope a'),
(['.scope', '&', ' ', 'a'], '.scope.a a,\n.scope.b a'),
(['.scope', ' ', 'a', '&'], '.scope a.a,\n.scope a.b'),
(['>', '&', '.scope', ' ', 'a'], ' > .a.scope a,\n > .b.scope a'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
sc.current = Identifier(['.next'], 0).parse(sc)
sc.push()
sc.current = Identifier(['.c', ',', '.d'], 0).parse(sc)
id = Identifier(['.deep'], 0)
self.assertEqual(
id.parse(sc).fmt(fl), '.a .next .c .deep,\n'
'.b .next .c .deep,\n'
'.a .next .d .deep,\n'
'.b .next .d .deep')
self.assertEqual(
id.raw(), '.a% %.next% %.c% %.deep%'
'.b% %.next% %.c% %.deep%'
def test_media(self):
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['@media', ' ', 'screen', ',', 'projection'],
0).parse(sc)
self.assertEqual(sc.current.fmt(fl), '@media screen,projection')
for i in [
(['html'], 'html'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
def test_basic(self):
fl = {'ws': ' ', 'nl': '\n'}
for i in [
([], ''),
(['.scope', ' ', 'a'], '.scope a'),
(['a', ' ', '.scope'], 'a .scope'),
(['a', '.scope'], 'a.scope'),
(['a', '>', 'p', '>', 'h2'], 'a > p > h2'),
(['a', '~', 'p', '+', 'h2'], 'a ~ p + h2'),
(['*', 'html'], '* html'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(None).fmt(fl), r, i)
sc = Scope()
sc.push()
sc.current = Identifier(['.a', ',', '.b'], 0).parse(sc)
for i in [
(['&', '.scope', ' ', 'a'], '.a.scope a,\n.b.scope a'),
(['.scope', '&', ' ', 'a'], '.scope.a a,\n.scope.b a'),
(['.scope', ' ', 'a', '&'], '.scope a.a,\n.scope a.b'),
(['>', '&', '.scope', ' ', 'a'], ' > .a.scope a,\n > .b.scope a'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
sc.current = Identifier(['.next'], 0).parse(sc)
sc.push()
sc.current = Identifier(['.c', ',', '.d'], 0).parse(sc)
id = Identifier(['.deep'], 0)
self.assertEqual(
id.parse(sc).fmt(fl), '.a .next .c .deep,\n'
'.b .next .c .deep,\n'
'.a .next .d .deep,\n'
'.b .next .d .deep')
self.assertEqual(
id.raw(), '.a% %.next% %.c% %.deep%'
'.b% %.next% %.c% %.deep%'
def test_scope(self):
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['.current'], 0).parse(sc)
for i in [
(['.scope', ' ', 'a'], '.current .scope a'),
(['a', ' ', '.scope'], '.current a .scope'),
(['a', '.scope'], '.current a.scope'),
(['a', '>', 'p', '>', 'h2'], '.current a > p > h2'),
(['a', '~', 'p', '+', 'h2'], '.current a ~ p + h2'),
(['>', 'p', '+', 'h2'], '.current > p + h2'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
def test_scope(self):
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['.current'], 0).parse(sc)
for i in [
(['.scope', ' ', 'a'], '.current .scope a'),
(['a', ' ', '.scope'], '.current a .scope'),
(['a', '.scope'], '.current a.scope'),
(['a', '>', 'p', '>', 'h2'], '.current a > p > h2'),
(['a', '~', 'p', '+', 'h2'], '.current a ~ p + h2'),
(['>', 'p', '+', 'h2'], '.current > p + h2'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
def test_combinators(self):
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['.current'], 0).parse(sc)
for i in [
(['&', '.scope', ' ', 'a'], '.current.scope a'),
(['.scope', '&', ' ', 'a'], '.scope.current a'),
(['.scope', ' ', 'a', '&'], '.scope a.current'),
(['&', '>', '.scope', ' ', 'a'], '.current > .scope a'),
(['.span', '&', '.scope', ' ', 'a', '&'],
'.span.current.scope a.current'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
sc.push()
sc.current = Identifier(['&', '.next'], 0).parse(sc)
id = Identifier(['&', '.top'], 0)
self.assertEqual(id.parse(sc).fmt(fl), '.current.next.top')
def test_groups(self):
fl = {'ws': ' ', 'nl': '\n'}
sc = Scope()
sc.push()
sc.current = Identifier(['.a', ',', '.b'], 0).parse(sc)
for i in [
(['&', '.scope', ' ', 'a'], '.a.scope a,\n.b.scope a'),
(['.scope', '&', ' ', 'a'], '.scope.a a,\n.scope.b a'),
(['.scope', ' ', 'a', '&'], '.scope a.a,\n.scope a.b'),
(['>', '&', '.scope', ' ', 'a'], ' > .a.scope a,\n > .b.scope a'),
]:
t, r = i
id = Identifier(t, 0)
self.assertEqual(id.parse(sc).fmt(fl), r, i)
sc.current = Identifier(['.next'], 0).parse(sc)
sc.push()
sc.current = Identifier(['.c', ',', '.d'], 0).parse(sc)
id = Identifier(['.deep'], 0)
self.assertEqual(
id.parse(sc).fmt(fl), '.a .next .c .deep,\n'
'.b .next .c .deep,\n'
'.a .next .d .deep,\n'
'.b .next .d .deep')
self.assertEqual(
id.raw(), '.a% %.next% %.c% %.deep%'
'.b% %.next% %.c% %.deep%'
# the parent with 'merged' media query (see above example).
p.tokens[1].remove(child)
if p_is_mediaquery: # Media query inside a & block
# Double-nested media query found. We remove it from 'p' and add
# it to this block with a new 'name'.
reparse_p = True
part_a = p.name.tokens[2:][0][0][0]
part_b = child.name.tokens[2:][0][0]
new_ident_tokens = [
'@media', ' ', [
part_a, (' ', 'and', ' '),
part_b
]
]
# Parse child again with new @media $BLA {} part
child.tokens[0] = Identifier(
new_ident_tokens)
child.parsed = None
child = child.parse(scope)
else:
child.block_name = p.name
append_list.append(child)
if reparse_p:
p.parsed = None
p = p.parse(scope)
if not p_is_mediaquery and not append_list:
self.inner.append(p)
else:
append_list.insert(
0, p
) # This media query should occur before it's children
for media_query in append_list:
def copy(self):
""" Return copy of self
Returns:
Identifier object
"""
tokens = ([t for t in self.tokens]
if isinstance(self.tokens, list) else self.tokens)
return Identifier(tokens, 0)