1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 07:57:52 +03:00

Don't always copy boxes

This commit is contained in:
Guillaume Ayoub 2016-11-01 04:31:15 +01:00
parent 45c815870b
commit 05d5e7d726
9 changed files with 31 additions and 32 deletions

View File

@ -312,10 +312,9 @@ class ParentBox(Box):
if end:
self._reset_spacing('bottom')
def copy_with_children(self, new_children, is_start=True, is_end=True,
copy_style=False):
def copy_with_children(self, new_children, is_start=True, is_end=True):
"""Create a new equivalent box with given ``new_children``."""
new_box = self.copy(copy_style=copy_style)
new_box = self.copy()
new_box.children = tuple(new_children)
if not is_start:
new_box.outside_list_marker = None

View File

@ -173,8 +173,7 @@ def element_to_box(element, style_for, get_image_from_uri, state=None):
if not counter_values[name]:
counter_values.pop(name)
if children:
box = box.copy_with_children(children)
box.children = children
replace_content_lists(element, box, style, counter_values)
# Specific handling for the element. (eg. replaced element)
@ -209,7 +208,8 @@ def pseudo_to_box(element, pseudo_type, state, style_for, get_image_from_uri):
children.extend(content_to_boxes(
style, box, quote_depth, counter_values, get_image_from_uri))
yield box.copy_with_children(children, copy_style=True)
box.children = children
yield box
def content_to_boxes(style, parent_box, quote_depth, counter_values,
@ -517,7 +517,8 @@ def table_boxes_children(box, children):
if isinstance(box, boxes.TableBox):
return wrap_table(box, children)
else:
return box.copy_with_children(children)
box.children = list(children)
return box
def wrap_table(box, children):
@ -626,7 +627,7 @@ def wrap_table(box, children):
grid_width = max(grid_width, grid_x)
grid_height += len(group.children)
table = box.copy_with_children(row_groups, copy_style=True)
table = box.copy_with_children(row_groups)
table.column_groups = tuple(column_groups)
if table.style.border_collapse == 'collapse':
table.collapsed_border_grid = collapse_table_borders(
@ -905,7 +906,8 @@ def inline_in_block(box):
if not (isinstance(child, boxes.TextBox) and not child.text)]
if not isinstance(box, boxes.BlockContainerBox):
return box.copy_with_children(children)
box.children = children
return box
new_line_children = []
new_children = []
@ -944,7 +946,8 @@ def inline_in_block(box):
# Only inline-level children: one line box
new_children.append(line_box)
return box.copy_with_children(new_children)
box.children = new_children
return box
def block_in_inline(box):
@ -1045,9 +1048,8 @@ def block_in_inline(box):
new_children.append(new_child)
if changed:
return box.copy_with_children(new_children)
else:
return box
box.children = new_children
return box
def _inner_block_in_inline(box, skip_stack=None):
@ -1099,7 +1101,7 @@ def _inner_block_in_inline(box, skip_stack=None):
else:
if changed or skip:
box = box.copy_with_children(
new_children, is_start=is_start, is_end=True, copy_style=False)
new_children, is_start=is_start, is_end=True)
return box, block_level_box, resume_at

View File

@ -138,8 +138,8 @@ def handle_img(element, box, get_image_from_uri):
else:
# Invalid image, use the alt-text.
if alt:
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
box.children = [boxes.TextBox.anonymous_from(box, alt)]
return [box]
elif alt == '':
# The element represents nothing
return []
@ -150,8 +150,8 @@ def handle_img(element, box, get_image_from_uri):
return []
else:
if alt:
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
box.children = [boxes.TextBox.anonymous_from(box, alt)]
return [box]
else:
return []

View File

@ -57,7 +57,7 @@ def layout_document(enable_hinting, style_for, get_image_from_uri, root_box):
root_children.extend(layout_fixed_boxes(context, pages[:i]))
root_children.extend(root.children)
root_children.extend(layout_fixed_boxes(context, pages[i+1:]))
root = root.copy_with_children(root_children)
root.children = root_children
context.current_page = page_counter[0]
page.children = (root,) + tuple(
make_margin_boxes(context, page, counter_values))

View File

@ -389,7 +389,7 @@ def block_container_layout(context, box, max_position_y, skip_stack,
is_start = skip_stack is None
if not is_start:
# Remove top margin, border and padding:
box = box.copy_with_children(box.children, is_start=False)
box._remove_decoration(start=True, end=False)
if adjoining_margins is None:
adjoining_margins = []
@ -719,8 +719,7 @@ def block_container_layout(context, box, max_position_y, skip_stack,
adjoining_margins = []
new_box = box.copy_with_children(
new_children, is_start=is_start, is_end=resume_at is None,
copy_style=True)
new_children, is_start=is_start, is_end=resume_at is None)
# TODO: See corner cases in
# http://www.w3.org/TR/CSS21/visudet.html#normal-block
@ -847,8 +846,7 @@ def find_earlier_page_break(children, absolute_boxes, fixed_boxes):
child.children, absolute_boxes, fixed_boxes)
if result:
new_grand_children, resume_at = result
new_child = child.copy_with_children(
new_grand_children, copy_style=True)
new_child = child.copy_with_children(new_grand_children)
new_children = list(children[:index]) + [new_child]
# Index in the original parent
resume_at = (new_child.index, resume_at)

View File

@ -155,8 +155,7 @@ def get_next_linebox(context, linebox, position_y, skip_stack,
absolute_boxes, fixed_boxes)
float_children.append(waiting_float)
if float_children:
line = line.copy_with_children(
line.children + tuple(float_children))
line.children += tuple(float_children)
return line, resume_at
@ -627,7 +626,7 @@ def split_inline_box(context, box, position_x, max_x, skip_stack,
resume_at = None
new_box = box.copy_with_children(
children, is_start=is_start, is_end=resume_at is None, copy_style=True)
children, is_start=is_start, is_end=resume_at is None)
if isinstance(box, boxes.LineBox):
# Line boxes already have a position_x which may not be the same
# as content_box_left when text-indent is non-zero.

View File

@ -303,10 +303,9 @@ def make_margin_boxes(context, page, counter_values):
# TODO: get actual counter values at the time of the last page break
if box.is_generated:
quote_depth = [0]
children = build.content_to_boxes(
box.children = build.content_to_boxes(
box.style, box, quote_depth, counter_values,
context.get_image_from_uri, context)
box = box.copy_with_children(children)
# content_to_boxes() only produces inline-level boxes, no need to
# run other post-processors from build.build_formatting_structure()
box = build.inline_in_block(box)
@ -521,7 +520,7 @@ def make_page(context, root_box, page_type, resume_at, content_empty,
absolute_layout(context, absolute_box, page, positioned_boxes)
context.finish_block_formatting_context(root_box)
page = page.copy_with_children([root_box])
page.children = [root_box]
descendants = page.descendants()
for child in descendants:
string_sets = child.style.string_set

View File

@ -138,7 +138,8 @@ class StackingContext(object):
result = dispatch(child)
if result is not None:
new_children.append(result)
return box.copy_with_children(new_children)
box.children = new_children
return box
box = dispatch_children(box)

View File

@ -50,7 +50,8 @@ def serialize(box_list):
# All concrete boxes are either text, replaced, column or parent.
(box.text if isinstance(box, boxes.TextBox)
else '<replaced>' if isinstance(box, boxes.ReplacedBox)
else serialize(getattr(box, 'column_groups', ()) + box.children)))
else serialize(
getattr(box, 'column_groups', ()) + tuple(box.children))))
for box in box_list]