From 84bdee14c344cf4e7b7645f0eb587ed2064b317f Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 2 Aug 2017 20:21:34 +0200 Subject: [PATCH] Revert "Change table and table-* elements' style before creating StyleDicts" This reverts commit ca8bc3e57831555eece88e72ac70fc24392514ce. The main change is the creation of StyleDicts removed from style_for, for memory consumption problems. --- weasyprint/css/__init__.py | 26 ++++++++++-------------- weasyprint/formatting_structure/build.py | 4 ++-- weasyprint/layout/pages.py | 4 ++-- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/weasyprint/css/__init__.py b/weasyprint/css/__init__.py index 89b57a844..5e4af5909 100644 --- a/weasyprint/css/__init__.py +++ b/weasyprint/css/__init__.py @@ -77,9 +77,10 @@ class StyleDict(dict): # TODO: this dict should be frozen, but modification is currently # authorized for some corner cases when building the structure: - # - table wrapping, - # - border modification for tables with collapsing borders, and - # - viewport overflow. + # - wrapping tables, + # - removing paddings and margins from tables, + # - modifying borders for table cells with collapsing borders, and + # - setting viewports and pages overflow. # TODO: We should remove that. Some attributes (eg. "clear") exist as # dict methods and can only be accessed with getitem. @@ -97,10 +98,10 @@ def inherit_from(self): """ if '_inherited_style' not in self.__dict__: - self._inherited_style = type(self)(computed_from_cascaded( + self._inherited_style = computed_from_cascaded( cascaded={}, parent_style=self, # Only by non-inherited properties, eg `content: attr(href)` - element=None)) + element=None) self._inherited_style.anonymous = True return self._inherited_style @@ -535,7 +536,7 @@ def computed_from_cascaded(element, cascaded, parent_style, pseudo_type=None, for side in ('top', 'bottom', 'left', 'right'): computed['border_%s_width' % side] = 0 computed['outline_width'] = 0 - return computed + return StyleDict(computed) # Handle inheritance and initial values specified = {} @@ -566,9 +567,9 @@ def computed_from_cascaded(element, cascaded, parent_style, pseudo_type=None, specified[name] = value - return computed_values.compute( + return StyleDict(computed_values.compute( element, pseudo_type, specified, computed, parent_style, root_style, - base_url) + base_url)) def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules, @@ -846,8 +847,7 @@ def get_all_computed_styles(html, user_stylesheets=None, base_url=html.base_url) # This is mostly useful to make pseudo_type optional. - def style_for(element, pseudo_type=None, update=None, - __get=computed_styles.get): + def style_for(element, pseudo_type=None, __get=computed_styles.get): """ Convenience function to get the computed styles for an element. """ @@ -865,11 +865,7 @@ def style_for(element, pseudo_type=None, update=None, # Margins do not apply for side in ['top', 'bottom', 'left', 'right']: style['margin_' + side] = computed_values.ZERO_PIXELS - if update: - style.update(update) - elif update: - style = dict(update) - return style and StyleDict(style) + return style return style_for diff --git a/weasyprint/formatting_structure/build.py b/weasyprint/formatting_structure/build.py index 057fbdb01..eb3ff0a81 100644 --- a/weasyprint/formatting_structure/build.py +++ b/weasyprint/formatting_structure/build.py @@ -53,8 +53,8 @@ def build_formatting_structure(element_tree, style_for, get_image_from_uri, box, = box_list else: # No root element - def root_style_for(element, pseudo_type=None, update=None): - style = style_for(element, pseudo_type, update) + def root_style_for(element, pseudo_type=None): + style = style_for(element, pseudo_type) if style: if element.parent is None: style.display = 'block' diff --git a/weasyprint/layout/pages.py b/weasyprint/layout/pages.py index aa732f726..ebaaf8dbd 100644 --- a/weasyprint/layout/pages.py +++ b/weasyprint/layout/pages.py @@ -482,8 +482,8 @@ def make_page(context, root_box, page_type, resume_at, content_empty, """ # Overflow value propagated from the root or . - style = context.style_for(page_type, update={ - 'overflow': root_box.viewport_overflow}) + style = context.style_for(page_type) + style['overflow'] = root_box.viewport_overflow page = boxes.PageBox(page_type, style) device_size = page.style.size