Skip to content

Commit

Permalink
Reorganize computers of CSS functions
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Nov 25, 2023
1 parent 1769611 commit 7f6b128
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions weasyprint/css/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ..logger import LOGGER, PROGRESS_LOGGER
from ..urls import URLFetchingError, get_url_attribute, url_join
from . import counters, media_queries
from .computed_values import COMPUTER_FUNCTIONS, ZERO_PIXELS, compute_variables
from .computed_values import COMPUTER_FUNCTIONS, ZERO_PIXELS, compute_var
from .properties import INHERITED, INITIAL_NOT_COMPUTED, INITIAL_VALUES
from .utils import get_url, remove_whitespace
from .validation import preprocess_declarations
Expand Down Expand Up @@ -678,7 +678,7 @@ def __missing__(self, key):

if key in self.cascaded:
# Property defined in cascaded properties.
keyword, computed = compute_variables(key, self, parent_style)
keyword, computed = compute_var(key, self, parent_style)
value = keyword
else:
# Property not defined in cascaded properties, define as inherited
Expand Down
82 changes: 41 additions & 41 deletions weasyprint/css/computed_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def decorator(function):
return decorator


def compute_variables(name, computed_style, parent_style):
def compute_var(name, computed_style, parent_style):
original_values = computed_style.cascaded[name][0]
validation_name = name.replace('_', '-')
multiple_values = validation_name in MULTIVAL_PROPERTIES
Expand Down Expand Up @@ -280,6 +280,44 @@ def compute_variables(name, computed_style, parent_style):
return values, computed


def compute_attr(style, values):
# TODO: use real token parsing instead of casting with Python types
func_name, value = values
assert func_name == 'attr()'
attr_name, type_or_unit, fallback = value
# style.element sometimes is None
# style.element sometimes is a 'PageType' object without .get()
# so wrapt the .get() into try and return None instead of crashing
try:
attr_value = style.element.get(attr_name, fallback)
if type_or_unit == 'string':
pass # Keep the string
elif type_or_unit == 'url':
if attr_value.startswith('#'):
attr_value = ('internal', unquote(attr_value[1:]))
else:
attr_value = (
'external', safe_urljoin(style.base_url, attr_value))
elif type_or_unit == 'color':
attr_value = parse_color(attr_value.strip())
elif type_or_unit == 'integer':
attr_value = int(attr_value.strip())
elif type_or_unit == 'number':
attr_value = float(attr_value.strip())
elif type_or_unit == '%':
attr_value = Dimension(float(attr_value.strip()), '%')
type_or_unit = 'length'
elif type_or_unit in LENGTH_UNITS:
attr_value = Dimension(float(attr_value.strip()), type_or_unit)
type_or_unit = 'length'
elif type_or_unit in ANGLE_TO_RADIANS:
attr_value = Dimension(float(attr_value.strip()), type_or_unit)
type_or_unit = 'angle'
except Exception:
return
return (type_or_unit, attr_value)


@register_computer('background-image')
def background_image(style, name, values):
"""Compute lenghts in gradient background-image."""
Expand Down Expand Up @@ -473,52 +511,14 @@ def column_gap(style, name, value):
return length(style, name, value, pixels_only=True)


def compute_attr_function(style, values):
# TODO: use real token parsing instead of casting with Python types
func_name, value = values
assert func_name == 'attr()'
attr_name, type_or_unit, fallback = value
# style.element sometimes is None
# style.element sometimes is a 'PageType' object without .get()
# so wrapt the .get() into try and return None instead of crashing
try:
attr_value = style.element.get(attr_name, fallback)
if type_or_unit == 'string':
pass # Keep the string
elif type_or_unit == 'url':
if attr_value.startswith('#'):
attr_value = ('internal', unquote(attr_value[1:]))
else:
attr_value = (
'external', safe_urljoin(style.base_url, attr_value))
elif type_or_unit == 'color':
attr_value = parse_color(attr_value.strip())
elif type_or_unit == 'integer':
attr_value = int(attr_value.strip())
elif type_or_unit == 'number':
attr_value = float(attr_value.strip())
elif type_or_unit == '%':
attr_value = Dimension(float(attr_value.strip()), '%')
type_or_unit = 'length'
elif type_or_unit in LENGTH_UNITS:
attr_value = Dimension(float(attr_value.strip()), type_or_unit)
type_or_unit = 'length'
elif type_or_unit in ANGLE_TO_RADIANS:
attr_value = Dimension(float(attr_value.strip()), type_or_unit)
type_or_unit = 'angle'
except Exception:
return
return (type_or_unit, attr_value)


def _content_list(style, values):
computed_values = []
for value in values:
if value[0] in ('string', 'content', 'url', 'quote', 'leader()'):
computed_value = value
elif value[0] == 'attr()':
assert value[1][1] == 'string'
computed_value = compute_attr_function(style, value)
computed_value = compute_attr(style, value)
elif value[0] in (
'counter()', 'counters()', 'content()', 'element()',
'string()',
Expand All @@ -531,7 +531,7 @@ def _content_list(style, values):
'target-counter()', 'target-counters()', 'target-text()'):
anchor_token = value[1][0]
if anchor_token[0] == 'attr()':
attr = compute_attr_function(style, anchor_token)
attr = compute_attr(style, anchor_token)
if attr is None:
computed_value = None
else:
Expand Down

0 comments on commit 7f6b128

Please sign in to comment.