Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Parse node. Block identifiers are stored as
strings with spaces replaced with ?
args:
scope (Scope): Current scope
raises:
SyntaxError
returns:
self
"""
names = []
name = []
self._subp = ('@media', '@keyframes', '@-moz-keyframes',
'@-webkit-keyframes', '@-ms-keyframes')
if self.tokens and hasattr(self.tokens, 'parse'):
self.tokens = list(
utility.flatten([
id.split() + [',']
for id in self.tokens.parse(scope).split(',')
]))
self.tokens.pop()
if self.tokens and any(hasattr(t, 'parse') for t in self.tokens):
tmp_tokens = []
for t in self.tokens:
if hasattr(t, 'parse'):
tmp_tokens.append(t.parse(scope))
else:
tmp_tokens.append(t)
self.tokens = list(utility.flatten(tmp_tokens))
if self.tokens and self.tokens[0] in self._subp:
name = list(utility.flatten(self.tokens))
self.subparse = True
else:
'@-webkit-keyframes', '@-ms-keyframes')
if self.tokens and hasattr(self.tokens, 'parse'):
self.tokens = list(
utility.flatten([
id.split() + [',']
for id in self.tokens.parse(scope).split(',')
]))
self.tokens.pop()
if self.tokens and any(hasattr(t, 'parse') for t in self.tokens):
tmp_tokens = []
for t in self.tokens:
if hasattr(t, 'parse'):
tmp_tokens.append(t.parse(scope))
else:
tmp_tokens.append(t)
self.tokens = list(utility.flatten(tmp_tokens))
if self.tokens and self.tokens[0] in self._subp:
name = list(utility.flatten(self.tokens))
self.subparse = True
else:
self.subparse = False
for n in utility.flatten(self.tokens):
if n == '*':
name.append('* ')
elif n in '>+~':
if name and name[-1] == ' ':
name.pop()
name.append('?%s?' % n)
elif n == ',':
names.append(name)
name = []
else:
def parse(self, scope):
"""Parse node
args:
scope (Scope): current scope
raises:
SyntaxError
returns:
self
"""
self.name, args, self.guards = self.tokens[0]
self.args = [a for a in utility.flatten(args) if a]
self.body = Block([None, self.tokens[1]], 0)
self.vars = list(
utility.flatten([
list(v.values()) for v in [s['__variables__'] for s in scope]
]))
return self
def swap(self, var):
""" Replace variable
args:
var (str): variable
raises:
SyntaxError
returns:
str
"""
var = self.scope.swap('@' + var)
var = ''.join(utility.flatten(var))
return var.strip("\"'")
def parse(self, scope):
"""Parse node
args:
scope (Scope): current scope
raises:
SyntaxError
returns:
self
"""
self.parsed = list(utility.flatten(self.tokens))
if self.parsed[0] == '@import':
if len(self.parsed) > 4:
# Media @import
self.parsed.insert(3, ' ')
return self
def parse(self, scope):
"""Parse node
args:
scope (Scope): current scope
raises:
SyntaxError
returns:
self
"""
self.name, args, self.guards = self.tokens[0]
self.args = [a for a in utility.flatten(args) if a]
self.body = Block([None, self.tokens[1]], 0)
self.vars = list(
utility.flatten([
list(v.values()) for v in [s['__variables__'] for s in scope]
]))
return self
]))
self.tokens.pop()
if self.tokens and any(hasattr(t, 'parse') for t in self.tokens):
tmp_tokens = []
for t in self.tokens:
if hasattr(t, 'parse'):
tmp_tokens.append(t.parse(scope))
else:
tmp_tokens.append(t)
self.tokens = list(utility.flatten(tmp_tokens))
if self.tokens and self.tokens[0] in self._subp:
name = list(utility.flatten(self.tokens))
self.subparse = True
else:
self.subparse = False
for n in utility.flatten(self.tokens):
if n == '*':
name.append('* ')
elif n in '>+~':
if name and name[-1] == ' ':
name.pop()
name.append('?%s?' % n)
elif n == ',':
names.append(name)
name = []
else:
name.append(n)
names.append(name)
parsed = self.root(scope, names) if scope else names
# Interpolated selectors need another step, we have to replace variables. Avoid reserved words though
#
def post_parse(self):
""" Post parse cycle. nodejs version allows calls to mixins
not yet defined or known to the parser. We defer all calls
to mixins until after first cycle when all names are known.
"""
if self.result:
out = []
for pu in self.result:
try:
out.append(pu.parse(self.scope))
except SyntaxError as e:
self.handle_error(e, 0)
self.result = list(utility.flatten(out))
def expression(self):
"""Return str representation of expression
returns:
str
"""
return utility.flatten(self.tokens)