1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00
WeasyPrint/weasy/css/validation.py

970 lines
30 KiB
Python
Raw Normal View History

# coding: utf8
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
# Copyright (C) 2011 Simon Sapin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Validator for all supported properties.
See http://www.w3.org/TR/CSS21/propidx.html for allowed values.
"""
# TODO: unit-test these validators
import functools
import logging
from ..formatting_structure import counters
from .values import (get_keyword, get_single_keyword, as_css,
make_percentage_value)
from .properties import INITIAL_VALUES, NOT_PRINT_MEDIA
from . import computed_values
2011-12-02 21:02:58 +04:00
# keyword -> (open, insert)
CONTENT_QUOTE_KEYWORDS = {
'open-quote': (True, True),
'close-quote': (False, True),
'no-open-quote': (True, False),
'no-close-quote': (False, False),
}
BACKGROUND_POSITION_PERCENTAGES = {
'top': make_percentage_value(0),
'left': make_percentage_value(0),
'center': make_percentage_value(50),
'bottom': make_percentage_value(100),
'right': make_percentage_value(100),
}
LOGGER = logging.getLogger('WEASYPRINT')
# yes/no validators for non-shorthand properties
# Maps property names to functions taking a property name and a value list,
# returning a value or None for invalid.
# Also transform values: keyword and URLs are returned as strings.
# For properties that take a single value, that value is returned by itself
# instead of a list.
VALIDATORS = {}
EXPANDERS = {}
PREFIXED = set()
# The same replacement was done on property names:
PREFIX = '-weasy-'.replace('-', '_')
class InvalidValues(ValueError):
2011-08-23 19:35:10 +04:00
"""Invalid or unsupported values for a known CSS property."""
2011-08-23 19:35:10 +04:00
# Validators
def validator(property_name=None, prefixed=False):
2011-08-23 19:35:10 +04:00
"""Decorator adding a function to the ``VALIDATORS``.
The name of the property covered by the decorated function is set to
``property_name`` if given, or is inferred from the function name
(replacing underscores by hyphens).
"""
def decorator(function):
2011-08-23 19:35:10 +04:00
"""Add ``function`` to the ``VALIDATORS``."""
if property_name is None:
name = function.__name__
else:
name = property_name.replace('-', '_')
assert name in INITIAL_VALUES, name
assert name not in VALIDATORS, name
VALIDATORS[name] = function
if prefixed:
PREFIXED.add(name)
return function
return decorator
def single_keyword(function):
"""Decorator for validators that only accept a single keyword."""
@functools.wraps(function)
2011-08-23 19:35:10 +04:00
def keyword_validator(values):
"""Wrap a validator to call get_single_keyword on values."""
keyword = get_single_keyword(values)
if function(keyword):
return keyword
2011-08-23 19:35:10 +04:00
return keyword_validator
def single_value(function):
"""Decorator for validators that only accept a single value."""
@functools.wraps(function)
2011-08-23 19:35:10 +04:00
def single_value_validator(values):
"""Validate a property whose value is single."""
if len(values) == 1:
return function(values[0])
single_value_validator.__func__ = function
2011-08-23 19:35:10 +04:00
return single_value_validator
def is_dimension(value, negative=True):
2011-08-23 19:35:10 +04:00
"""Get if ``value`` is a dimension.
The ``negative`` argument sets wether negative values are allowed.
"""
type_ = value.type
# Units may be ommited on zero lenghts.
return (
2011-08-23 19:35:10 +04:00
type_ == 'DIMENSION' and (negative or value.value >= 0) and (
value.dimension in computed_values.LENGTHS_TO_PIXELS or
2011-08-23 19:35:10 +04:00
value.dimension in ('em', 'ex'))
) or (type_ == 'NUMBER' and value.value == 0)
def is_dimension_or_percentage(value, negative=True):
2011-08-23 19:35:10 +04:00
"""Get if ``value`` is a dimension or a percentage.
The ``negative`` argument sets wether negative values are allowed.
"""
return is_dimension(value, negative) or (
2011-08-23 19:35:10 +04:00
value.type == 'PERCENTAGE' and (negative or value.value >= 0))
@validator()
@single_keyword
def background_attachment(keyword):
2011-08-23 19:35:10 +04:00
"""``background-attachment`` property validation."""
return keyword in ('scroll', 'fixed')
@validator('background-color')
@validator('border-top-color')
@validator('border-right-color')
@validator('border-bottom-color')
@validator('border-left-color')
@validator('color')
@single_value
def color(value):
2011-08-23 19:35:10 +04:00
"""``*-color`` and ``color`` properties validation."""
if value.type == 'COLOR_VALUE':
return value
if get_keyword(value) == 'currentColor':
return 'inherit'
@validator('background-image')
@validator('list-style-image')
@single_value
def image(value):
2011-08-23 19:35:10 +04:00
"""``*-image`` properties validation."""
if get_keyword(value) == 'none':
return 'none'
if value.type == 'URI':
return value.absoluteUri
@validator()
def background_position(values):
2011-08-23 19:35:10 +04:00
"""``background-position`` property validation.
See http://www.w3.org/TR/CSS21/colors.html#propdef-background-position
"""
kw_to_percentage = dict(top=0, left=0, center=50, bottom=100, right=100)
if len(values) == 1:
center = BACKGROUND_POSITION_PERCENTAGES['center']
value = values[0]
keyword = get_keyword(value)
if keyword in BACKGROUND_POSITION_PERCENTAGES:
return BACKGROUND_POSITION_PERCENTAGES[keyword], center
elif is_dimension_or_percentage(value):
return value, center
elif len(values) == 2:
value_1, value_2 = values
keyword_1, keyword_2 = map(get_keyword, values)
if is_dimension_or_percentage(value_1):
if keyword_2 in ('top', 'center', 'bottom'):
return value_1, BACKGROUND_POSITION_PERCENTAGES[keyword_2]
elif is_dimension_or_percentage(value_2):
return value_1, value_2
elif is_dimension_or_percentage(value_2):
if keyword_1 in ('left', 'center', 'right'):
return BACKGROUND_POSITION_PERCENTAGES[keyword_1], value_2
elif (
keyword_1 in ('left', 'center', 'right') and
keyword_2 in ('top', 'center', 'bottom')
) or (
keyword_1 in ('top', 'center', 'bottom') and
keyword_2 in ('left', 'center', 'right')
):
return (BACKGROUND_POSITION_PERCENTAGES[keyword_1],
BACKGROUND_POSITION_PERCENTAGES[keyword_2])
#else: invalid
@validator()
@single_keyword
def background_repeat(keyword):
2011-08-23 19:35:10 +04:00
"""``background-repeat`` property validation."""
return keyword in ('repeat', 'repeat-x', 'repeat-y', 'no-repeat')
@validator()
def border_spacing(values):
"""Validator for the `border-spacing` property."""
2011-12-16 16:44:25 +04:00
if all(is_dimension(value, negative=False) for value in values):
if len(values) == 1:
return (values[0], values[0])
elif len(values) == 2:
return tuple(values)
@validator('border-top-style')
@validator('border-right-style')
@validator('border-left-style')
@validator('border-bottom-style')
@single_keyword
def border_style(keyword):
2011-08-23 19:35:10 +04:00
"""``border-*-style`` properties validation."""
return keyword in ('none', 'hidden', 'dotted', 'dashed', 'double',
'inset', 'outset', 'groove', 'ridge', 'solid')
@validator('border-top-width')
@validator('border-right-width')
@validator('border-left-width')
@validator('border-bottom-width')
@single_value
def border_width(value):
2011-08-23 19:35:10 +04:00
"""``border-*-width`` properties validation."""
if is_dimension(value, negative=False):
return value
keyword = get_keyword(value)
if keyword in ('thin', 'medium', 'thick'):
return keyword
@validator()
@single_keyword
def box_sizing(keyword):
return keyword in ('padding-box', 'border-box')
@validator()
@single_keyword
def caption_side(keyword):
"""``caption-side`` properties validation."""
return keyword in ('top', 'bottom')
@validator()
def content(values):
"""``content`` property validation."""
keyword = get_single_keyword(values)
if keyword in ('normal', 'none'):
return keyword
parsed_values = map(validate_content_value, values)
2011-12-08 14:32:35 +04:00
if None not in parsed_values:
return parsed_values
def validate_content_value(value):
"""Validation for a signle value for the ``content`` property.
Return (type, content) or False for invalid values.
"""
2011-12-02 21:02:58 +04:00
quote_type = CONTENT_QUOTE_KEYWORDS.get(get_keyword(value))
if quote_type is not None:
return ('QUOTE', quote_type)
type_ = value.type
if type_ == 'STRING':
return ('STRING', value.value)
if type_ == 'URI':
return ('URI', value.absoluteUri)
elif type_ == 'FUNCTION':
2011-12-08 14:32:35 +04:00
seq = [e.value for e in value.seq]
# seq is expected to look like
# ['name(', ARG_1, ',', ARG_2, ',', ..., ARG_N, ')']
if (seq[0][-1] == '(' and seq[-1] == ')' and
all(v == ',' for v in seq[2:-1:2])):
name = seq[0][:-1]
args = seq[1:-1:2]
prototype = (name, [a.type for a in args])
args = [a.value for a in args]
if prototype == ('attr', ['IDENT']):
return (name, args[0])
elif prototype in (('counter', ['IDENT']),
('counters', ['IDENT', 'STRING'])):
args.append('decimal')
return (name, args)
elif prototype in (('counter', ['IDENT', 'IDENT']),
('counters', ['IDENT', 'STRING', 'IDENT'])):
style = args[-1]
if style in ('none', 'decimal') or style in counters.STYLES:
return (name, args)
@validator()
def counter_increment(values):
"""``counter-increment`` property validation."""
return counter(values, default_integer=1)
@validator()
def counter_reset(values):
"""``counter-reset`` property validation."""
return counter(values, default_integer=0)
def counter(values, default_integer):
"""``counter-increment`` and ``counter-reset`` properties validation."""
if get_single_keyword(values) == 'none':
return []
values = iter(values)
value = next(values, None)
if value is None:
return # expected at least one value
results = []
while value is not None:
counter_name = get_keyword(value)
if counter_name is None:
return # expected a keyword here
if counter_name in ('none', 'initial', 'inherit'):
raise InvalidValues('Invalid counter name: '+ counter_name)
value = next(values, None)
if (value is not None and value.type == 'NUMBER' and
isinstance(value.value, int)):
# Found an integer. Use it and get the next value
integer = value.value
value = next(values, None)
else:
# Not an integer. Might be the next counter name.
# Keep `value` for the next loop iteration.
integer = default_integer
results.append((counter_name, integer))
return results
#@validator('top')
#@validator('right')
#@validator('left')
#@validator('bottom')
@validator('margin-top')
@validator('margin-right')
@validator('margin-bottom')
@validator('margin-left')
@single_value
def lenght_precentage_or_auto(value, negative=True):
2011-08-23 19:35:10 +04:00
"""``margin-*`` properties validation."""
if is_dimension_or_percentage(value, negative):
return value
if get_keyword(value) == 'auto':
return 'auto'
@validator('height')
@validator('width')
@single_value
2011-08-16 19:49:33 +04:00
def positive_lenght_precentage_or_auto(value):
return lenght_precentage_or_auto.__func__(value, negative=False)
@validator()
@single_keyword
def direction(keyword):
2011-08-23 19:35:10 +04:00
"""``direction`` property validation."""
return keyword in ('ltr', 'rtl')
@validator()
@single_keyword
def display(keyword):
2011-08-23 19:35:10 +04:00
"""``display`` property validation."""
if keyword in ('inline-block',):
raise InvalidValues('value not supported yet')
return keyword in (
'inline', 'block', 'list-item', 'none',
2011-11-15 21:19:36 +04:00
'table', 'inline-table', 'table-caption',
'table-row-group', 'table-header-group', 'table-footer-group',
'table-row', 'table-column-group', 'table-column', 'table-cell')
@validator()
def font_family(values):
2011-08-23 19:35:10 +04:00
"""``font-family`` property validation."""
# TODO: we should split on commas only.
# " If a sequence of identifiers is given as a font family name, the
# computed value is the name converted to a string by joining all the
# identifiers in the sequence by single spaces. "
# http://www.w3.org/TR/CSS21/fonts.html#font-family-prop
# eg. `font-family: Foo bar, "baz
if all(value.type in ('IDENT', 'STRING') for value in values):
return [value.value for value in values]
@validator()
@single_value
def font_size(value):
2011-08-23 19:35:10 +04:00
"""``font-size`` property validation."""
if is_dimension_or_percentage(value):
return value
2011-08-23 19:35:10 +04:00
font_size_keyword = get_keyword(value)
if font_size_keyword in ('smaller', 'larger'):
raise InvalidValues('value not supported yet')
if (
2011-08-23 19:35:10 +04:00
font_size_keyword in computed_values.FONT_SIZE_KEYWORDS #or
#keyword in ('smaller', 'larger')
):
return font_size_keyword
@validator()
@single_keyword
def font_style(keyword):
2011-08-23 19:35:10 +04:00
"""``font-style`` property validation."""
return keyword in ('normal', 'italic', 'oblique')
@validator()
@single_keyword
def font_variant(keyword):
2011-08-23 19:35:10 +04:00
"""``font-variant`` property validation."""
return keyword in ('normal', 'small-caps')
@validator()
@single_value
def font_weight(value):
2011-08-23 19:35:10 +04:00
"""``font-weight`` property validation."""
keyword = get_keyword(value)
if keyword in ('normal', 'bold', 'bolder', 'lighter'):
return keyword
if value.type == 'NUMBER':
value = value.value
if value in [100, 200, 300, 400, 500, 600, 700, 800, 900]:
return value
#@validator() XXX not supported yet
@single_value
def letter_spacing(value):
2011-08-23 19:35:10 +04:00
"""``letter-spacing`` property validation."""
if get_keyword(value) == 'normal':
return 'normal'
if is_dimension(value):
return value
@validator()
@single_value
def line_height(value):
2011-08-23 19:35:10 +04:00
"""``line-height`` property validation."""
if get_keyword(value) == 'normal':
return 'normal'
if (value.type in ('NUMBER', 'DIMENSION', 'PERCENTAGE') and
value.value >= 0):
return value
@validator()
@single_keyword
def list_style_position(keyword):
2011-08-23 19:35:10 +04:00
"""``list-style-position`` property validation."""
return keyword in ('inside', 'outside')
@validator()
@single_keyword
def list_style_type(keyword):
2011-08-23 19:35:10 +04:00
"""``list-style-type`` property validation."""
return keyword in ('none', 'decimal') or keyword in counters.STYLES
@validator('padding-top')
@validator('padding-right')
@validator('padding-bottom')
@validator('padding-left')
@single_value
def length_or_precentage(value):
2011-08-23 19:35:10 +04:00
"""``padding-*`` properties validation."""
if is_dimension_or_percentage(value, negative=False):
return value
@validator()
@single_keyword
def position(keyword):
2011-08-23 19:35:10 +04:00
"""``position`` property validation."""
if keyword in ('relative', 'absolute', 'fixed'):
raise InvalidValues('value not supported yet')
return keyword in ('static',)
2011-12-02 21:02:58 +04:00
@validator()
def quotes(values):
"""``quotes`` property validation."""
if (values and len(values) % 2 == 0
and all(v.type == 'STRING' for v in values)):
strings = [v.value for v in values]
# Separate open and close quotes.
# eg. ['«', '»', '“', '”'] -> (['«', '“'], ['»', '”'])
return strings[::2], strings[1::2]
@validator()
@single_keyword
def text_align(keyword):
2011-08-23 19:35:10 +04:00
"""``text-align`` property validation."""
if keyword in ('justify',):
raise InvalidValues('value not supported yet')
return keyword in ('left', 'right', 'center')
@validator()
def text_decoration(values):
2011-08-23 19:35:10 +04:00
"""``text-decoration`` property validation."""
keywords = map(get_keyword, values)
if keywords == ['none']:
return 'none'
if all(keyword in ('underline', 'overline', 'line-through', 'blink')
for keyword in keywords):
unique = frozenset(keywords)
if len(unique) == len(keywords):
# No duplicate
return unique
@validator()
@single_value
def text_indent(value):
"""``text-indent`` property validation."""
if is_dimension_or_percentage(value, negative=True):
return value
@validator()
@single_keyword
def text_transform(keyword):
"""``text-align`` property validation."""
return keyword in ('none', 'uppercase', 'lowercase', 'capitalize')
@validator()
@single_value
def vertical_align(value):
if is_dimension_or_percentage(value, negative=True):
return value
keyword = get_keyword(value)
if keyword in ('baseline', 'middle', 'sub', 'super',
'text-top', 'text-bottom', 'top', 'bottom'):
return keyword
2011-10-21 13:15:06 +04:00
@validator()
@single_keyword
def visibility(keyword):
"""``white-space`` property validation."""
return keyword in ('visible', 'hidden', 'collapse')
@validator()
@single_keyword
def white_space(keyword):
2011-08-23 19:35:10 +04:00
"""``white-space`` property validation."""
return keyword in ('normal', 'pre', 'nowrap', 'pre-wrap', 'pre-line')
@validator(prefixed=True)
def size(values):
2011-08-23 19:35:10 +04:00
"""``size`` property validation.
See http://www.w3.org/TR/css3-page/#page-size-prop
"""
if is_dimension(values[0]):
if len(values) == 1:
return values * 2
elif len(values) == 2 and is_dimension(values[1]):
return values
keywords = map(get_keyword, values)
if len(keywords) == 1:
keyword = keywords[0]
if keyword in ('auto', 'portrait'):
return INITIAL_VALUES['size']
elif keyword == 'landscape':
height, width = INITIAL_VALUES['size']
return width, height
elif keyword in computed_values.PAGE_SIZES:
return computed_values.PAGE_SIZES[keyword]
if len(keywords) == 2:
if keywords[0] in ('portrait', 'landscape'):
orientation, size = keywords
elif keywords[1] in ('portrait', 'landscape'):
size, orientation = keywords
else:
size = None
if size in computed_values.PAGE_SIZES:
size = computed_values.PAGE_SIZES[size]
if orientation == 'portrait':
return size
else:
height, width = size
return width, height
2011-08-23 19:35:10 +04:00
# Expanders
# Let's be consistent, always use ``name`` as an argument even
# when it is useless.
2011-08-23 19:35:10 +04:00
# pylint: disable=W0613
def expander(property_name):
2011-08-23 19:35:10 +04:00
"""Decorator adding a function to the ``EXPANDERS``."""
property_name = property_name.replace('-', '_')
2011-08-23 19:35:10 +04:00
def expander_decorator(function):
"""Add ``function`` to the ``EXPANDERS``."""
assert property_name not in EXPANDERS, property_name
EXPANDERS[property_name] = function
return function
2011-08-23 19:35:10 +04:00
return expander_decorator
@expander('border-color')
@expander('border-style')
@expander('border-width')
@expander('margin')
@expander('padding')
def expand_four_sides(name, values):
2011-08-23 19:35:10 +04:00
"""Expand properties setting a value for the four sides of a box."""
# Make sure we have 4 values
if len(values) == 1:
values *= 4
elif len(values) == 2:
2011-08-23 19:35:10 +04:00
values *= 2 # (bottom, left) defaults to (top, right)
elif len(values) == 3:
2011-08-23 19:35:10 +04:00
values.append(values[1]) # left defaults to right
elif len(values) != 4:
2011-08-23 19:35:10 +04:00
raise InvalidValues(
'Expected 1 to 4 value components got %i' % len(values))
for suffix, value in zip(('_top', '_right', '_bottom', '_left'), values):
i = name.rfind('_')
if i == -1:
new_name = name + suffix
else:
# eg. border-color becomes border-*-color, not border-color-*
new_name = name[:i] + suffix + name[i:]
# validate_non_shorthand returns [(name, value)], we want
# to yield (name, value)
result, = validate_non_shorthand(new_name, [value], required=True)
yield result
def generic_expander(*expanded_names):
2011-08-23 19:35:10 +04:00
"""Decorator helping expanders to handle ``inherit`` and ``initial``.
Wrap an expander so that it does not have to handle the 'inherit' and
'initial' cases, and can just yield name suffixes. Missing suffixes
get the initial value.
2011-08-23 19:35:10 +04:00
"""
expanded_names = [name.replace('-', '_') for name in expanded_names]
2011-08-23 19:35:10 +04:00
def generic_expander_decorator(wrapped):
"""Decorate the ``wrapped`` expander."""
@functools.wraps(wrapped)
2011-08-23 19:35:10 +04:00
def generic_expander_wrapper(name, values):
"""Wrap the expander."""
keyword = get_single_keyword(values)
if keyword in ('inherit', 'initial'):
results = dict.fromkeys(expanded_names, keyword)
skip_validation = True
else:
skip_validation = False
results = {}
for new_name, new_values in wrapped(name, values):
assert new_name in expanded_names, new_name
assert new_name not in results, new_name
results[new_name] = new_values
for new_name in expanded_names:
if new_name.startswith('_'):
# new_name is a suffix
actual_new_name = name + new_name
else:
actual_new_name = new_name
if new_name in results:
values = results[new_name]
if not skip_validation:
# validate_non_shorthand returns [(name, value)]
(actual_new_name, values), = validate_non_shorthand(
actual_new_name, values, required=True)
else:
values = INITIAL_VALUES[actual_new_name]
yield actual_new_name, values
2011-08-23 19:35:10 +04:00
return generic_expander_wrapper
return generic_expander_decorator
@expander('list-style')
@generic_expander('-type', '-position', '-image')
def expand_list_style(name, values):
2011-08-23 19:35:10 +04:00
"""Expand the ``list-style`` shorthand property.
See http://www.w3.org/TR/CSS21/generate.html#propdef-list-style
"""
type_specified = image_specified = False
none_count = 0
for value in values:
if get_keyword(value) == 'none':
# Can be either -style or -image, see at the end which is not
# otherwise specified.
none_count += 1
none_value = value
continue
if list_style_type([value]) is not None:
suffix = '_type'
type_specified = True
elif list_style_position([value]) is not None:
suffix = '_position'
elif image([value]) is not None:
suffix = '_image'
image_specified = True
else:
raise InvalidValues
yield suffix, [value]
if not type_specified and none_count:
yield '_type', [none_value]
none_count -= 1
if not image_specified and none_count:
yield '_image', [none_value]
none_count -= 1
if none_count:
# Too many none values.
raise InvalidValues
@expander('border')
def expand_border(name, values):
2011-08-23 19:35:10 +04:00
"""Expand the ``border`` shorthand property.
See http://www.w3.org/TR/CSS21/box.html#propdef-border
"""
for suffix in ('_top', '_right', '_bottom', '_left'):
for new_prop in expand_border_side(name + suffix, values):
yield new_prop
@expander('border-top')
@expander('border-right')
@expander('border-bottom')
@expander('border-left')
@generic_expander('-width', '-color', '-style')
def expand_border_side(name, values):
2011-08-23 19:35:10 +04:00
"""Expand the ``border-*`` shorthand properties.
See http://www.w3.org/TR/CSS21/box.html#propdef-border-top
"""
for value in values:
if color([value]) is not None:
suffix = '_color'
elif border_width([value]) is not None:
suffix = '_width'
elif border_style([value]) is not None:
suffix = '_style'
else:
raise InvalidValues
yield suffix, [value]
def is_valid_background_positition(value):
2011-08-23 19:35:10 +04:00
"""Tell whether the value is valid for ``background-position``."""
return (
value.type in ('DIMENSION', 'PERCENTAGE') or
(value.type == 'NUMBER' and value.value == 0) or
2011-08-23 19:35:10 +04:00
get_keyword(value) in ('left', 'right', 'top', 'bottom', 'center'))
@expander('background')
@generic_expander('-color', '-image', '-repeat', '-attachment', '-position')
def expand_background(name, values):
2011-08-23 19:35:10 +04:00
"""Expand the ``background`` shorthand property.
See http://www.w3.org/TR/CSS21/colors.html#propdef-background
"""
# Make `values` a stack
values = list(reversed(values))
while values:
value = values.pop()
if color([value]) is not None:
suffix = '_color'
elif image([value]) is not None:
suffix = '_image'
elif background_repeat([value]) is not None:
suffix = '_repeat'
elif background_attachment([value]) is not None:
suffix = '_attachment'
elif background_position([value]):
if values:
next_value = values.pop()
if background_position([value, next_value]):
# Two consecutive '-position' values, yield them together
yield '_position', [value, next_value]
continue
else:
# The next value is not a '-position', put it back
# for the next loop iteration
values.append(next_value)
# A single '-position' value
suffix = '_position'
else:
raise InvalidValues
yield suffix, [value]
@expander('font')
@generic_expander('-style', '-variant', '-weight', '-size',
'line-height', '-family') # line-height is not a suffix
def expand_font(name, values):
2011-08-23 19:35:10 +04:00
"""Expand the ``font`` shorthand property.
http://www.w3.org/TR/CSS21/fonts.html#font-shorthand
"""
2011-08-23 19:35:10 +04:00
expand_font_keyword = get_single_keyword(values)
if expand_font_keyword in ('caption', 'icon', 'menu', 'message-box',
'small-caption', 'status-bar'):
LOGGER.warn(
2011-08-23 19:35:10 +04:00
'System fonts are not supported, `font: %s` ignored.',
expand_font_keyword)
return
# Make `values` a stack
values = list(reversed(values))
# Values for font-style font-variant and font-weight can come in any
# order and are all optional.
while values:
value = values.pop()
if get_keyword(value) == 'normal':
# Just ignore 'normal' keywords. Unspecified properties will get
# their initial value, which is 'normal' for all three here.
continue
if font_style([value]) is not None:
suffix = '_style'
elif font_variant([value]) is not None:
suffix = '_variant'
elif font_weight([value]) is not None:
suffix = '_weight'
else:
# Were done with these three, continue with font-size
break
yield suffix, [value]
# Then font-size is mandatory
# Latest `value` from the loop.
if font_size([value]) is None:
raise InvalidValues
yield '_size', [value]
# Then line-height is optional, but font-family is not so the list
# must not be empty yet
2011-10-11 14:09:37 +04:00
if not values:
raise InvalidValues
value = values.pop()
if line_height([value]) is not None:
yield 'line_height', [value]
else:
# We pop()ed a font-family, add it back
values.append(value)
# Reverse the stack to get normal list
values.reverse()
if font_family(values) is None:
raise InvalidValues
yield '_family', values
def validate_non_shorthand(name, values, required=False):
2011-08-23 19:35:10 +04:00
"""Default validator for non-shorthand properties."""
if not required and name not in INITIAL_VALUES:
raise InvalidValues('unknown property')
if not required and name not in VALIDATORS:
raise InvalidValues('property not supported yet')
keyword = get_single_keyword(values)
if keyword in ('initial', 'inherit'):
value = keyword
else:
value = VALIDATORS[name](values)
if value is None:
raise InvalidValues
return [(name, value)]
def validate_and_expand(name, values):
2011-08-23 19:35:10 +04:00
"""Expand and validate shorthand properties.
The invalid or unsupported declarations are ignored and logged.
Return a iterable of ``(name, values)`` tuples.
"""
if name in PREFIXED and not name.startswith(PREFIX):
level = 'warn'
reason = ('the property is experimental, use ' +
(PREFIX + name).replace('_', '-'))
elif name in NOT_PRINT_MEDIA:
level = 'info'
reason = 'the property does not apply for the print media'
else:
if name.startswith(PREFIX):
unprefixed_name = name[len(PREFIX):]
if unprefixed_name in PREFIXED:
name = unprefixed_name
2011-08-23 19:35:10 +04:00
expander_ = EXPANDERS.get(name, validate_non_shorthand)
try:
2011-08-23 19:35:10 +04:00
results = expander_(name, values)
# Use list() to consume any generator now,
# so that InvalidValues is caught.
return list(results)
except InvalidValues, exc:
level = 'warn'
if exc.args and exc.args[0]:
reason = exc.args[0]
else:
reason = 'invalid value'
2011-11-25 20:22:59 +04:00
getattr(LOGGER, level)('Ignored declaration: `%s: %s`, %s.',
name.replace('_', '-'), as_css(values), reason)
return []