Skip to content

Commit

Permalink
Unfreeze StyleDict
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Aug 2, 2017
1 parent 033f473 commit c779977
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 34 deletions.
24 changes: 6 additions & 18 deletions weasyprint/css/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@


class StyleDict(dict):
"""A frozen dict that allows attribute access to values.
"""A dict allowing attribute access to values.
Allow eg. ``style.font_size`` instead of ``style['font-size']``.
"""

# Hide modification
__setitem__ = update = None
# 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.

# TODO: We should remove that. Some attributes (eg. "clear") exist as
# dict methods and can only be accessed with getitem.
Expand All @@ -86,21 +89,6 @@ def get_color(self, key):
value = self[key]
return value if value != 'currentColor' else self['color']

def copy(self, update):
"""Copy the ``StyleDict`` with updated values."""
# Don't copy when the updated values are already set
for key, value in update.items():
if self[key] != value:
break
else:
return self

style = dict(self)
style.update(update)
style = type(self)(style)
style.anonymous = self.anonymous
return style

def inherit_from(self):
"""Return a new StyleDict with inherited properties from this one.
Expand Down
20 changes: 7 additions & 13 deletions weasyprint/formatting_structure/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,9 @@ def wrap_table(box, children):
# Non-inherited properties of the table element apply to one
# of the wrapper and the table. The other get the initial value.
# TODO: put this in a method of the table object
wrapper_style = {
name: table.style[name]
for name in properties.TABLE_WRAPPER_BOX_PROPERTIES}
table_style = {
name: properties.INITIAL_VALUES[name]
for name in properties.TABLE_WRAPPER_BOX_PROPERTIES}
wrapper.style = wrapper.style.copy(wrapper_style)
table.style = table.style.copy(table_style)
for name in properties.TABLE_WRAPPER_BOX_PROPERTIES:
wrapper.style[name] = table.style[name]
table.style[name] = properties.INITIAL_VALUES[name]
# else: non-inherited properties already have their initial values

return wrapper
Expand Down Expand Up @@ -747,10 +742,9 @@ def set_borders(box, x, y, w, h):
# the correct widths on each box. The actual border grid will be
# painted separately.
def set_transparent_border(box, side, twice_width):
box.style = box.style.copy({
'border_%s_style' % side: 'solid',
'border_%s_width' % side: twice_width / 2,
'border_%s_color' % side: transparent})
box.style['border_%s_style' % side] = 'solid',
box.style['border_%s_width' % side] = twice_width / 2
box.style['border_%s_color' % side] = transparent

def remove_borders(box):
set_transparent_border(box, 'top', 0)
Expand Down Expand Up @@ -1125,7 +1119,7 @@ def set_viewport_overflow(root_box):
break

root_box.viewport_overflow = chosen_box.style.overflow
chosen_box.style = chosen_box.style.copy({'overflow': 'visible'})
chosen_box.style['overflow'] = 'visible'
return root_box


Expand Down
7 changes: 4 additions & 3 deletions weasyprint/layout/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,14 @@ def first_letter_to_box(box, skip_stack, first_letter_style):
# "This type of initial letter is similar to an
# inline-level element if its 'float' property is 'none',
# otherwise it is similar to a floated element."
letter_box_inherit = letter_box.style.inherit_from()
if first_letter_style['float'] == 'none':
letter_box = boxes.InlineBox(
'%s::first-letter' % box.element_tag,
first_letter_style, [])
text_box = boxes.TextBox(
'%s::first-letter' % box.element_tag,
letter_box.style.inherit_from(), first_letter)
letter_box_inherit, first_letter)
letter_box.children = (text_box,)
box.children = (letter_box,) + tuple(box.children)
else:
Expand All @@ -298,11 +299,11 @@ def first_letter_to_box(box, skip_stack, first_letter_style):
letter_box.first_letter_style = None
line_box = boxes.LineBox(
'%s::first-letter' % box.element_tag,
letter_box.style.inherit_from(), [])
letter_box_inherit, [])
letter_box.children = (line_box,)
text_box = boxes.TextBox(
'%s::first-letter' % box.element_tag,
letter_box.style.inherit_from(), first_letter)
letter_box_inherit, first_letter)
line_box.children = (text_box,)
box.children = (letter_box,) + tuple(box.children)
if skip_stack and child_skip_stack:
Expand Down

0 comments on commit c779977

Please sign in to comment.