mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-05 00:21:15 +03:00
Simplify the handling of 'auto' lengths.
This commit is contained in:
parent
73b0bd6281
commit
34612cc4fb
@ -256,6 +256,8 @@ def length_tuple(computer, name, values):
|
||||
@register_computer('text-indent')
|
||||
def length(computer, name, value, font_size=None, pixels_only=False):
|
||||
"""Compute a length ``value``."""
|
||||
if value == 'auto':
|
||||
return value
|
||||
if value.value == 0:
|
||||
return 0 if pixels_only else ZERO_PIXELS
|
||||
|
||||
|
@ -19,12 +19,6 @@ from tinycss.color3 import COLOR_KEYWORDS
|
||||
Dimension = collections.namedtuple('Dimension', ['value', 'unit'])
|
||||
|
||||
|
||||
class _Auto(str):
|
||||
unit = None
|
||||
value = 'auto'
|
||||
AUTO = _Auto('auto') # for values that can be 'auto' as well as a dimension
|
||||
|
||||
|
||||
# See http://www.w3.org/TR/CSS21/propidx.html
|
||||
INITIAL_VALUES = {
|
||||
'background_attachment': 'scroll',
|
||||
@ -34,7 +28,7 @@ INITIAL_VALUES = {
|
||||
'background_repeat': 'repeat',
|
||||
'background_clip': 'border-box', # CSS3
|
||||
'background_origin': 'padding-box', # CSS3
|
||||
'background_size': (AUTO, AUTO), # CSS3
|
||||
'background_size': ('auto', 'auto'), # CSS3
|
||||
'border_collapse': 'separate',
|
||||
# http://www.w3.org/TR/css3-color/#currentcolor
|
||||
'border_top_color': 'currentColor',
|
||||
@ -50,7 +44,7 @@ INITIAL_VALUES = {
|
||||
'border_right_width': 3,
|
||||
'border_bottom_width': 3,
|
||||
'border_left_width': 3,
|
||||
'bottom': AUTO,
|
||||
'bottom': 'auto',
|
||||
'caption_side': 'top',
|
||||
'clear': 'none',
|
||||
'clip': (), # empty collection, computed value for 'auto'
|
||||
@ -71,8 +65,8 @@ INITIAL_VALUES = {
|
||||
'font_style': 'normal',
|
||||
'font_variant': 'normal',
|
||||
'font_weight': 400,
|
||||
'height': AUTO,
|
||||
'left': AUTO,
|
||||
'height': 'auto',
|
||||
'left': 'auto',
|
||||
'letter_spacing': 'normal',
|
||||
'line_height': 'normal',
|
||||
'list_style_image': 'none',
|
||||
@ -100,20 +94,20 @@ INITIAL_VALUES = {
|
||||
'page_break_inside': 'auto',
|
||||
'quotes': list('“”‘’'), # depends on user agent
|
||||
'position': 'static',
|
||||
'right': AUTO,
|
||||
'right': 'auto',
|
||||
'table_layout': 'auto',
|
||||
'text_align': '-weasy-start', # Taken from CSS3 Text.
|
||||
# The only other supported value form CSS3 is -weasy-end.
|
||||
'text_decoration': 'none',
|
||||
'text_indent': Dimension(0, 'px'),
|
||||
'text_transform': 'none',
|
||||
'top': AUTO,
|
||||
'top': 'auto',
|
||||
'unicode_bidi': 'normal',
|
||||
'vertical_align': 'baseline',
|
||||
'visibility': 'visible',
|
||||
'white_space': 'normal',
|
||||
'widows': 2,
|
||||
'width': AUTO,
|
||||
'width': 'auto',
|
||||
'word_spacing': 0, # computed value for 'normal'
|
||||
'z_index': 'auto',
|
||||
|
||||
|
@ -22,7 +22,7 @@ from ..logger import LOGGER
|
||||
from ..formatting_structure import counters
|
||||
from ..compat import urljoin
|
||||
from .properties import (INITIAL_VALUES, KNOWN_PROPERTIES, NOT_PRINT_MEDIA,
|
||||
AUTO, Dimension)
|
||||
Dimension)
|
||||
from . import computed_values
|
||||
|
||||
# TODO: unit-test these validators
|
||||
@ -253,15 +253,15 @@ def background_size(tokens):
|
||||
if keyword in ('contain', 'cover'):
|
||||
return keyword
|
||||
if keyword == 'auto':
|
||||
return (AUTO, AUTO)
|
||||
return ('auto', 'auto')
|
||||
length = get_length(token, negative=False)
|
||||
if length:
|
||||
return (length, AUTO)
|
||||
return (length, 'auto')
|
||||
elif len(tokens) == 2:
|
||||
values = []
|
||||
for token in tokens:
|
||||
if get_keyword(token) == 'auto':
|
||||
values.append(AUTO)
|
||||
values.append('auto')
|
||||
else:
|
||||
length = get_length(token, negative=False)
|
||||
if length:
|
||||
@ -341,7 +341,7 @@ def clip(token):
|
||||
values = []
|
||||
for arg in args:
|
||||
if get_keyword(arg) == 'auto':
|
||||
values.append(AUTO)
|
||||
values.append('auto')
|
||||
else:
|
||||
length = get_length(arg)
|
||||
if length:
|
||||
@ -465,7 +465,7 @@ def lenght_precentage_or_auto(token, negative=True):
|
||||
if length:
|
||||
return length
|
||||
if get_keyword(token) == 'auto':
|
||||
return AUTO
|
||||
return 'auto'
|
||||
|
||||
|
||||
@validator('height')
|
||||
|
@ -15,8 +15,7 @@ from __future__ import division, unicode_literals
|
||||
from ..formatting_structure import boxes
|
||||
|
||||
|
||||
def resolve_one_percentage(box, property_name, refer_to,
|
||||
allowed_keywords=None):
|
||||
def resolve_one_percentage(box, property_name, refer_to):
|
||||
"""Set a used length value from a computed length value.
|
||||
|
||||
``refer_to`` is the length for 100%. If ``refer_to`` is not a number, it
|
||||
@ -25,21 +24,13 @@ def resolve_one_percentage(box, property_name, refer_to,
|
||||
"""
|
||||
# box.style has computed values
|
||||
value = box.style[property_name]
|
||||
if value.unit == 'px':
|
||||
if value == 'auto':
|
||||
result = value
|
||||
elif value.unit == 'px':
|
||||
result = value.value
|
||||
else:
|
||||
if value.unit == '%':
|
||||
if isinstance(refer_to, (int, float)):
|
||||
# A percentage
|
||||
result = value.value * refer_to / 100.
|
||||
else:
|
||||
# Replace percentages when we have no refer_to that
|
||||
# makes sense.
|
||||
result = refer_to
|
||||
else:
|
||||
# Some other values such as 'auto' may be allowed
|
||||
result = value
|
||||
assert allowed_keywords and result in allowed_keywords
|
||||
assert value.unit == '%'
|
||||
result = value.value * refer_to / 100.
|
||||
# box attributes are used values
|
||||
setattr(box, property_name, result)
|
||||
|
||||
@ -56,15 +47,15 @@ def resolve_percentages(box, containing_block):
|
||||
maybe_height = cb_height
|
||||
else:
|
||||
maybe_height = cb_width
|
||||
resolve_one_percentage(box, 'margin_left', cb_width, ['auto'])
|
||||
resolve_one_percentage(box, 'margin_right', cb_width, ['auto'])
|
||||
resolve_one_percentage(box, 'margin_top', maybe_height, ['auto'])
|
||||
resolve_one_percentage(box, 'margin_bottom', maybe_height, ['auto'])
|
||||
resolve_one_percentage(box, 'margin_left', cb_width)
|
||||
resolve_one_percentage(box, 'margin_right', cb_width)
|
||||
resolve_one_percentage(box, 'margin_top', maybe_height)
|
||||
resolve_one_percentage(box, 'margin_bottom', maybe_height)
|
||||
resolve_one_percentage(box, 'padding_left', cb_width)
|
||||
resolve_one_percentage(box, 'padding_right', cb_width)
|
||||
resolve_one_percentage(box, 'padding_top', maybe_height)
|
||||
resolve_one_percentage(box, 'padding_bottom', maybe_height)
|
||||
resolve_one_percentage(box, 'width', cb_width, ['auto'])
|
||||
resolve_one_percentage(box, 'width', cb_width)
|
||||
# Not supported yet:
|
||||
# resolve_one_percentage(box, 'min_width', cb_width)
|
||||
# resolve_one_percentage(box, 'max_width', cb_width, ['none'])
|
||||
@ -74,12 +65,17 @@ def resolve_percentages(box, containing_block):
|
||||
if cb_height == 'auto':
|
||||
# Special handling when the height of the containing block
|
||||
# depends on its content.
|
||||
resolve_one_percentage(box, 'height', 'auto', ['auto'])
|
||||
height = box.style.height
|
||||
if height == 'auto' or height.unit == '%':
|
||||
box.height = 'auto'
|
||||
else:
|
||||
assert height.unit == 'px'
|
||||
box.height = height.value
|
||||
# Not supported yet, but min_height is used for margin collapsing.
|
||||
resolve_one_percentage(box, 'min_height', 0)
|
||||
# resolve_one_percentage(box, 'max_height', None, ['none'])
|
||||
else:
|
||||
resolve_one_percentage(box, 'height', cb_height, ['auto'])
|
||||
resolve_one_percentage(box, 'height', cb_height)
|
||||
# Not supported yet, but min_height is used for margin collapsing.
|
||||
resolve_one_percentage(box, 'min_height', cb_height)
|
||||
# resolve_one_percentage(box, 'max_height', cb_height, ['none'])
|
||||
|
@ -276,7 +276,7 @@ def fixed_table_layout(table):
|
||||
|
||||
# `width` on column boxes
|
||||
for i, column in enumerate(all_columns):
|
||||
resolve_one_percentage(column, 'width', table.width, ['auto'])
|
||||
resolve_one_percentage(column, 'width', table.width)
|
||||
if column.width != 'auto':
|
||||
column_widths[i] = column.width
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user