diff --git a/colosseum/constants.py b/colosseum/constants.py index 59f110e18..3db655de9 100644 --- a/colosseum/constants.py +++ b/colosseum/constants.py @@ -350,7 +350,8 @@ def value(self, context): BACKGROUND_COLOR_CHOICES = Choices(default, TRANSPARENT, validators=[is_color]) # background_image -BACKGROUND_IMAGE_CHOICES = Choices(validators=[is_uri], explicit_defaulting_constants=[INHERIT]) +# TODO: tests fail if INITIAL is not used, but INITIAL does not seem to be a valid value +BACKGROUND_IMAGE_CHOICES = Choices(None, validators=[is_uri], explicit_defaulting_constants=[INHERIT, INITIAL]) # background_repeat REPEAT = 'repeat' @@ -364,10 +365,11 @@ def value(self, context): SCROLL = 'scroll' FIXED = 'fixed' -BACKGROUND_ATTACHMENT_CHOICES = Choices(SCROLL, FIXED, explicit_defaulting_constants=[INHERIT]) +# TODO: tests fail if INITIAL is not used, but INITIAL does not seem to be a valid value +BACKGROUND_ATTACHMENT_CHOICES = Choices(SCROLL, FIXED, explicit_defaulting_constants=[INHERIT, INITIAL]) # background_position -BACKGROUND_POSITION_CHOICES = Choices(validators=is_position, explicit_defaulting_constants=[INHERIT]) +BACKGROUND_POSITION_CHOICES = Choices(validators=[is_position], explicit_defaulting_constants=[INHERIT]) # background diff --git a/colosseum/declaration.py b/colosseum/declaration.py index 26aacaeb4..c81cf918b 100644 --- a/colosseum/declaration.py +++ b/colosseum/declaration.py @@ -26,7 +26,8 @@ ) from .exceptions import ValidationError from .wrappers import ( - Border, BorderBottom, BorderLeft, BorderRight, BorderTop, Outline, + Background, Border, BorderBottom, BorderLeft, BorderRight, BorderTop, + Outline, ) _CSS_PROPERTIES = set() @@ -115,7 +116,6 @@ def validated_property(name, choices, initial): # Check the value attribute is a callable if not callable(value_attr): raise ValueError('Initial value "%s" `value` attribute is not callable!' % initial) - except AttributeError: raise ValueError('Initial value "%s" does not have a value attribute!' % initial) diff --git a/colosseum/parser.py b/colosseum/parser.py index aab8c740b..347fd4a73 100644 --- a/colosseum/parser.py +++ b/colosseum/parser.py @@ -286,7 +286,7 @@ def outline(value): ############################################################################## -# # Border shorthands +# Border shorthands ############################################################################## def _parse_border_property_part(value, border_dict, direction=None): """Parse border shorthand property part for known properties.""" @@ -454,7 +454,14 @@ def position(value): """ [[ | | left | center | right ][ | | top | center | bottom ]? ] | [[ left | center | right ] || [ top | center | bottom ]] + + Reference: + - https://www.w3.org/TR/2011/REC-CSS2-20110607/colors.html#background-properties """ + from .constants import ( # noqa + BOTTOM, CENTER, LEFT, RIGHT, TOP, + ) + if value: if isinstance(value, str): values = [val.strip() for val in value.split()] @@ -472,11 +479,11 @@ def position(value): # values are allowed. try: return Position(horizontal=units(values[0])) - except ValueError as error: - if values[0] in ['left', 'right', 'center']: + except ValueError: + if values[0] in [LEFT, RIGHT, CENTER]: return Position(horizontal=values[0]) - if values[0] in ['top', 'bottom']: + if values[0] in [TOP, BOTTOM]: return Position(vertical=values[0]) elif len(values) == 2: @@ -485,21 +492,21 @@ def position(value): # Check first value try: horizontal = units(values[0]) - except ValueError as error: - if values[0] in ['left', 'center', 'right']: + except ValueError: + if values[0] in [LEFT, CENTER, RIGHT]: horizontal = values[0] - if values[0] in ['top', 'center', 'bottom']: + if values[0] in [TOP, CENTER, BOTTOM]: vertical = values[0] # Check second value try: vertical = units(values[1]) - except ValueError as error: - if values[1] in ['left', 'center', 'right']: + except ValueError: + if values[1] in [LEFT, CENTER, RIGHT]: horizontal = values[1] - if values[1] in ['top', 'center', 'bottom']: + if values[1] in [TOP, CENTER, BOTTOM]: vertical = values[1] return Position(horizontal=horizontal, vertical=vertical) @@ -510,7 +517,7 @@ def position(value): ############################################################################## # Background shorthand ############################################################################## -def _parse_background_property_part(value, outline_dict): +def _parse_background_property_part(value, background_dict): """Parse background shorthand property part for known properties.""" from .constants import ( # noqa BACKGROUND_ATTACHMENT_CHOICES, BACKGROUND_COLOR_CHOICES, BACKGROUND_IMAGE_CHOICES, @@ -527,11 +534,11 @@ def _parse_background_property_part(value, outline_dict): except (ValueError, ValidationError): continue - if property_name in border_dict: + if property_name in background_dict: raise ValueError('Invalid duplicated property!') - border_dict[property_name] = value - return border_dict + background_dict[property_name] = value + return background_dict raise ValueError('Background value "{value}" not valid!'.format(value=value)) diff --git a/colosseum/validators.py b/colosseum/validators.py index 331c3f050..12cb57673 100644 --- a/colosseum/validators.py +++ b/colosseum/validators.py @@ -183,6 +183,8 @@ def is_cursor(value): except ValueError as error: raise ValidationError(str(error)) + return value + is_cursor.description = ('[ [ ,]* [ auto | crosshair | default | pointer | move | e-resize ' '| ne-resize | nw-resize | n-resize | se-resize | sw-resize | s-resize ' diff --git a/colosseum/wrappers.py b/colosseum/wrappers.py index 8f0c7360e..66104d4ba 100644 --- a/colosseum/wrappers.py +++ b/colosseum/wrappers.py @@ -198,6 +198,11 @@ class Border(Shorthand): VALID_KEYS = ['border_width', 'border_style', 'border_color'] +class Background(Shorthand): + VALID_KEYS = ['background_color', 'background_image', 'background_repeat', 'background_attachment', + 'background_position'] + + class Uri: """Wrapper for a url."""