Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def transform(tokens):
"""Validation for ``transform``."""
if get_single_keyword(tokens) == 'none':
return ()
else:
transforms = []
for token in tokens:
function = parse_function(token)
if not function:
return
name, args = function
if len(args) == 1:
angle = get_angle(args[0])
length = get_length(args[0], percentage=True)
if name in ('rotate', 'skewx', 'skewy') and angle:
transforms.append((name, angle))
elif name in ('translatex', 'translate') and length:
transforms.append((
'translate', (length, computed_values.ZERO_PIXELS)))
elif name == 'translatey' and length:
transforms.append((
'translate', (computed_values.ZERO_PIXELS, length)))
elif name == 'scalex' and args[0].type == 'number':
transforms.append(('scale', (args[0].value, 1)))
elif name == 'scaley' and args[0].type == 'number':
transforms.append(('scale', (1, args[0].value)))
elif name == 'scale' and args[0].type == 'number':
transforms.append(('scale', (args[0].value,) * 2))
else:
return
transforms.append((
'translate', (computed_values.ZERO_PIXELS, length)))
elif name == 'scalex' and args[0].type == 'number':
transforms.append(('scale', (args[0].value, 1)))
elif name == 'scaley' and args[0].type == 'number':
transforms.append(('scale', (1, args[0].value)))
elif name == 'scale' and args[0].type == 'number':
transforms.append(('scale', (args[0].value,) * 2))
else:
return
elif len(args) == 2:
if name == 'scale' and all(a.type == 'number' for a in args):
transforms.append((name, tuple(arg.value for arg in args)))
else:
lengths = tuple(
get_length(token, percentage=True) for token in args)
if name == 'translate' and all(lengths):
transforms.append((name, lengths))
else:
return
elif len(args) == 6 and name == 'matrix' and all(
a.type == 'number' for a in args):
transforms.append((name, tuple(arg.value for arg in args)))
else:
return
return tuple(transforms)
def border_corner_radius(tokens):
"""Validator for the `border-*-radius` properties."""
lengths = [
get_length(token, negative=False, percentage=True) for token in tokens]
if all(lengths):
if len(lengths) == 1:
return (lengths[0], lengths[0])
elif len(lengths) == 2:
return tuple(lengths)
def background_size(tokens):
"""Validation for ``background-size``."""
if len(tokens) == 1:
token = tokens[0]
keyword = get_keyword(token)
if keyword in ('contain', 'cover'):
return keyword
if keyword == 'auto':
return ('auto', 'auto')
length = get_length(token, negative=False, percentage=True)
if length:
return (length, 'auto')
elif len(tokens) == 2:
values = []
for token in tokens:
length = get_length(token, negative=False, percentage=True)
if length:
values.append(length)
elif get_keyword(token) == 'auto':
values.append('auto')
if len(values) == 2:
return tuple(values)
def border_spacing(tokens):
"""Validator for the `border-spacing` property."""
lengths = [get_length(token, negative=False) for token in tokens]
if all(lengths):
if len(lengths) == 1:
return (lengths[0], lengths[0])
elif len(lengths) == 2:
return tuple(lengths)
def line_height(token):
"""``line-height`` property validation."""
if get_keyword(token) == 'normal':
return 'normal'
if token.type == 'number' and token.value >= 0:
return Dimension(token.value, None)
if token.type == 'percentage' and token.value >= 0:
return Dimension(token.value, '%')
elif token.type == 'dimension' and token.value >= 0:
return get_length(token)
def width_height(token):
"""Validation for the ``width`` and ``height`` properties."""
length = get_length(token, negative=False, percentage=True)
if length:
return length
if get_keyword(token) == 'auto':
return 'auto'
def border_width(token):
"""Border, column rule and outline widths properties validation."""
length = get_length(token, negative=False)
if length:
return length
keyword = get_keyword(token)
if keyword in ('thin', 'medium', 'thick'):
return keyword
def length_or_precentage(token):
"""``padding-*`` properties validation."""
length = get_length(token, negative=False, percentage=True)
if length:
return length
def column_width(token):
"""``column-width`` property validation."""
length = get_length(token, negative=False)
if length:
return length
keyword = get_keyword(token)
if keyword == 'auto':
return keyword