mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 16:07:57 +03:00
Make DummyPropertyValue lists instead of using get_value. (And remove get_value)
This commit is contained in:
parent
4f3b1d90f3
commit
8fd543134a
@ -25,7 +25,7 @@ import collections
|
||||
|
||||
import cssutils.helper
|
||||
from cssutils.css import PropertyValue, DimensionValue, Value
|
||||
from .initial_values import get_value, INITIAL_VALUES
|
||||
from .initial_values import INITIAL_VALUES
|
||||
|
||||
|
||||
# How many CSS pixels is one <unit> ?
|
||||
@ -93,6 +93,16 @@ FONT_WEIGHT_RELATIVE = dict(
|
||||
)
|
||||
|
||||
|
||||
class DummyPropertyValue(list):
|
||||
"""
|
||||
A list that quacks like a PropertyValue.
|
||||
"""
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return ' '.join(value.cssText for value in self)
|
||||
|
||||
|
||||
def handle_computed_font_size(element):
|
||||
"""
|
||||
Set the computed value for font-size, and return this value in pixels.
|
||||
@ -169,7 +179,8 @@ def handle_computed_lengths(element, font_size):
|
||||
"""
|
||||
element.style = dict(
|
||||
# PropertyValue objects are not mutable, build a new list
|
||||
(name, [compute_length(value, font_size) for value in values])
|
||||
(name, DummyPropertyValue(compute_length(value, font_size)
|
||||
for value in values))
|
||||
for name, values in element.style.iteritems()
|
||||
)
|
||||
|
||||
@ -199,10 +210,10 @@ def handle_computed_border_width(element):
|
||||
"""
|
||||
style = element.style
|
||||
for side in ('top', 'right', 'bottom', 'left'):
|
||||
if get_value(style, 'border-%s-style' % side) in ('none', 'hidden'):
|
||||
if style['border-%s-style' % side].value in ('none', 'hidden'):
|
||||
style['border-%s-width' % side] = PropertyValue('0')
|
||||
else:
|
||||
value = get_value(style, 'border-%s-width' % side)
|
||||
value = style['border-%s-width' % side].value
|
||||
if value in BORDER_WIDTH_KEYWORDS:
|
||||
width = BORDER_WIDTH_KEYWORDS[value]
|
||||
style['border-%s-width' % side] = PropertyValue(
|
||||
@ -215,10 +226,10 @@ def handle_computed_outline_width(element):
|
||||
"""
|
||||
# TODO: test this
|
||||
style = element.style
|
||||
if get_value(style, 'outline-style') == 'none':
|
||||
if style['outline-style'].value == 'none':
|
||||
style['outline-width'] = PropertyValue('0')
|
||||
else:
|
||||
value = get_value(style, 'outline-width')
|
||||
value = style['outline-width'].value
|
||||
if value in BORDER_WIDTH_KEYWORDS:
|
||||
width = BORDER_WIDTH_KEYWORDS[value]
|
||||
style['outline-width'] = PropertyValue(str(width) + 'px')
|
||||
@ -231,18 +242,18 @@ def handle_computed_display_float(element):
|
||||
"""
|
||||
# TODO: test this
|
||||
style = element.style
|
||||
if get_value(style, 'display') == 'none':
|
||||
if style['display'].value == 'none':
|
||||
# Case 1
|
||||
return # position and float do not apply, but leave them
|
||||
elif get_value(style, 'position') in ('absolute', 'fixed'):
|
||||
elif style['position'].value in ('absolute', 'fixed'):
|
||||
# Case 2
|
||||
style['float'] = PropertyValue('none')
|
||||
elif get_value(style, 'float') == 'none' and element.getparent() is not None:
|
||||
elif style['float'].value == 'none' and element.getparent() is not None:
|
||||
# Case 5
|
||||
return
|
||||
|
||||
# Cases 2, 3, 4
|
||||
display = get_value(style, 'display')
|
||||
display = style['display'].value
|
||||
if display == 'inline-table':
|
||||
style['display'] = PropertyValue('table')
|
||||
elif display in ('inline', 'table-row-group', 'table-column',
|
||||
@ -260,7 +271,7 @@ def handle_computed_word_spacing(element):
|
||||
# TODO: test this
|
||||
style = element.style
|
||||
# CSS 2.1 says this for word-spacing but not letter-spacing. Why?
|
||||
if get_value(style, 'word-spacing') == 'normal':
|
||||
if style['word-spacing'].value == 'normal':
|
||||
style['word-spacing'] = PropertyValue('0')
|
||||
|
||||
|
||||
@ -270,7 +281,7 @@ def handle_computed_font_weight(element):
|
||||
"""
|
||||
# TODO: test this
|
||||
style = element.style
|
||||
value = get_value(style, 'font-weight')
|
||||
value = style['font-weight'].value
|
||||
if value == 'normal':
|
||||
style['font-weight'] = PropertyValue('400')
|
||||
elif value == 'bold':
|
||||
@ -309,12 +320,13 @@ def handle_computed_content(element):
|
||||
# TODO: properly test this
|
||||
style = element.style
|
||||
if getattr(element, 'pseudo_element_type', '') in ('before', 'after'):
|
||||
if get_value(style, 'content') == 'normal':
|
||||
if style['content'].value == 'normal':
|
||||
style['content'] = PropertyValue('none')
|
||||
else:
|
||||
parent = element.getparent()
|
||||
style['content'] = [compute_content_value(parent, value)
|
||||
for value in style['content']]
|
||||
style['content'] = DummyPropertyValue(
|
||||
compute_content_value(parent, value)
|
||||
for value in style['content'])
|
||||
else:
|
||||
# CSS 2.1 says it computes to 'normal' for elements, but does not say
|
||||
# anything for pseudo-elements other than :before and :after
|
||||
|
@ -135,28 +135,15 @@ INITIAL_VALUES = dict(
|
||||
)
|
||||
|
||||
|
||||
def get_value(style, name):
|
||||
"""
|
||||
Return the value of a property as a string, defaulting to 'initial'.
|
||||
"""
|
||||
if name not in style:
|
||||
return 'initial'
|
||||
values = style[name]
|
||||
if hasattr(values, 'value'):
|
||||
# This looks like a PropertyValue object
|
||||
return values.value
|
||||
else:
|
||||
# One of the functions below may have replace a PropertyValue by a list
|
||||
# of Value objects.
|
||||
return ' '.join(value.cssText for value in values)
|
||||
|
||||
|
||||
def is_initial(style, name):
|
||||
"""
|
||||
Return whether the property `name` is missing in the given `style` dict
|
||||
or if its value is the 'initial' keyword.
|
||||
"""
|
||||
return get_value(style, name) == 'initial'
|
||||
# Explicit 'initial' values are new in CSS3
|
||||
# http://www.w3.org/TR/css3-values/#computed0
|
||||
return name not in style or style[name].value == 'initial'
|
||||
|
||||
|
||||
def handle_initial_values(element):
|
||||
@ -166,8 +153,6 @@ def handle_initial_values(element):
|
||||
"""
|
||||
style = element.style
|
||||
for name, initial in INITIAL_VALUES.iteritems():
|
||||
# Explicit 'initial' values are new in CSS3
|
||||
# http://www.w3.org/TR/css3-values/#computed0
|
||||
if is_initial(style, name):
|
||||
style[name] = initial
|
||||
|
||||
|
@ -24,7 +24,6 @@ import cssutils
|
||||
from cssutils.helper import path2url
|
||||
|
||||
from .. import css
|
||||
from ..css.computed_values import get_value
|
||||
|
||||
from . import resource_filename
|
||||
|
||||
@ -100,22 +99,22 @@ def test_annotate_document():
|
||||
sides = ('-top', '-right', '-bottom', '-left')
|
||||
# 32px = 1em * font-size: 2em * initial 16px
|
||||
for side, expected_value in zip(sides, ('32px', '0', '32px', '0')):
|
||||
assert get_value(p.style, 'margin' + side) == expected_value
|
||||
assert p.style['margin' + side].value == expected_value
|
||||
|
||||
# 32px = 2em * initial 16px
|
||||
for side, expected_value in zip(sides, ('32px', '32px', '32px', '32px')):
|
||||
assert get_value(ul.style, 'margin' + side) == expected_value
|
||||
assert ul.style['margin' + side].value == expected_value
|
||||
|
||||
# thick = 5px, 0.25 inches = 96*.25 = 24px
|
||||
for side, expected_value in zip(sides, ('0', '5px', '0', '24px')):
|
||||
assert get_value(ul.style, 'border' + side + '-width') == expected_value
|
||||
assert ul.style['border' + side + '-width'].value == expected_value
|
||||
|
||||
# 32px = 2em * initial 16px
|
||||
# 64px = 4em * initial 16px
|
||||
for side, expected_value in zip(sides, ('32px', '0', '32px', '64px')):
|
||||
assert get_value(li[0].style, 'margin' + side) == expected_value
|
||||
assert li[0].style['margin' + side].value == expected_value
|
||||
|
||||
assert get_value(a.style, 'text-decoration') == 'underline'
|
||||
assert a.style['text-decoration'].value == 'underline'
|
||||
|
||||
color = a.style['color'][0]
|
||||
assert (color.red, color.green, color.blue, color.alpha) == (255, 0, 0, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user