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

Move parsing of the size property from computed_values to validation.

This commit is contained in:
Simon Sapin 2011-12-16 15:06:59 +01:00
parent 29388af63f
commit 7c2a055ce7
3 changed files with 36 additions and 62 deletions

View File

@ -126,6 +126,10 @@ PAGE_SIZES = dict(
17 * LENGTHS_TO_PIXELS['in'],
),
)
for w, h in PAGE_SIZES.values():
assert w < h
INITIAL_VALUES['size'] = PAGE_SIZES['A4']
class Computer(object):
@ -212,6 +216,7 @@ def other_color(computer, name, value):
@Computer.register('background-position')
@Computer.register('border-spacing')
@Computer.register('size')
def length_list(computer, name, values):
"""Compute the properties with a list of lengths."""
return [length(computer, name, value) for value in values]
@ -403,49 +408,6 @@ def line_height(computer, name, value):
return factor * font_size_value
@Computer.register('size')
def size(computer, name, values):
"""Compute the ``size`` property.
See CSS3 Paged Media.
"""
if computer.element != '@page':
return 'none'
keywords = map(get_keyword, values)
values = length_list(computer, name, values)
if keywords == ['auto']:
keywords = ['A4'] # Chosen by the UA. (Thats me!)
if isinstance(values[0], (int, float)):
if len(values) == 2:
assert isinstance(values[1], (int, float))
return values
else:
# square page
value, = values
return value, value
else:
orientation = None
size_value = None
for i, keyword in enumerate(keywords):
if keyword in ('portrait', 'landscape'):
orientation = keyword
elif keyword in PAGE_SIZES:
size_value = keyword
else:
raise ValueError("Illegal value for 'size': %r" % values[i])
if size_value is None:
size_value = 'A4'
width, height = PAGE_SIZES[size_value]
if (orientation == 'portrait' and width > height) or \
(orientation == 'landscape' and height > width):
width, height = height, width
return width, height
@Computer.register('text-align')
def text_align(computer, name, value):
"""Compute the ``text-align`` property."""

View File

@ -114,7 +114,7 @@ INITIAL_VALUES = {
'z_index': 'auto',
# CSS3 Paged Media: http://www.w3.org/TR/css3-page/#page-size
'size': _parse('auto'),
'size': None, # XXX set to A4 in computed_values
# CSS3 User Interface: http://www.w3.org/TR/css3-ui/#box-sizing
'box_sizing': 'content-box',

View File

@ -599,26 +599,38 @@ def size(values):
See http://www.w3.org/TR/css3-page/#page-size-prop
"""
if len(values) == 1:
if is_dimension(values[0]):
return values
keyword = get_single_keyword(values)
if (keyword in ('auto', 'portrait', 'landscape') or
keyword in computed_values.PAGE_SIZES):
return values
if len(values) == 2:
if all(is_dimension(value) for value in values):
return values
keywords = map(get_keyword, values)
if (
keywords[0] in ('portrait', 'landscape') and
keywords[1] in computed_values.PAGE_SIZES
) or (
keywords[0] in computed_values.PAGE_SIZES and
keywords[1] in ('portrait', 'landscape')
):
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
# Expanders