Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

148 lines (123 sloc) 5.87 KB
# coding: utf8
:copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
from __future__ import division, unicode_literals
import operator
from .formatting_structure import boxes
from .layout.absolute import AbsolutePlaceholder
_Z_INDEX_GETTER = operator.attrgetter('z_index')
class StackingContext(object):
"""Stacking contexts define the paint order of all pieces of a document.
def __init__(self, box, child_contexts, blocks, floats, blocks_and_cells,
page): = box = page
self.block_level_boxes = blocks # 4: In flow, non positioned
self.float_contexts = floats # 5: Non positioned
self.negative_z_contexts = [] # 3: Child contexts, z-index < 0
self.zero_z_contexts = [] # 8: Child contexts, z-index = 0
self.positive_z_contexts = [] # 9: Child contexts, z-index > 0
self.blocks_and_cells = blocks_and_cells # 7: Non positioned
for context in child_contexts:
if context.z_index < 0:
elif context.z_index == 0:
else: # context.z_index > 0
# sort() is stable, so the lists are now storted
# by z-index, then tree order.
self.z_index =
if self.z_index == 'auto':
self.z_index = 0
def from_page(cls, page):
# Page children (the box for the root element and margin boxes)
# as well as the page box itself are unconditionally stacking contexts.
child_contexts = [cls.from_box(child, page) for child in page.children]
# Children are sub-contexts, remove them from the "normal" tree.
page = page.copy_with_children([])
return cls(page, child_contexts, [], [], [], page)
def from_box(cls, box, page, child_contexts=None):
children = [] # What will be passed to this box
if child_contexts is None:
child_contexts = children
# child_contexts: where to put sub-contexts that we find here.
# May not be the same as children for:
# "treat the element as if it created a new stacking context,
# but any positioned descendants and descendants which actually
# create a new stacking context should be considered part of the
# parent stacking context, not this new one."
blocks = []
floats = []
blocks_and_cells = []
def dispatch(box):
if isinstance(box, AbsolutePlaceholder):
box = box._box
if (( != 'static' and != 'auto')
or < 1
# 'transform: none' gives a "falsy" empty list here
or != 'visible'
# This box defines a new stacking context, remove it
# from the "normal" children list.
StackingContext.from_box(box, page))
if != 'static':
assert == 'auto'
# "Fake" context: sub-contexts will go in this
# `child_contexts` list.
# Insert at the position before creating the sub-context.
index = len(child_contexts)
StackingContext.from_box(box, page, child_contexts))
elif box.is_floated():
box, page, child_contexts))
elif isinstance(box, boxes.InlineBlockBox):
# Have this fake stacking context be part of the "normal"
# box tree, because we need its position in the middle
# of a tree of inline boxes.
return StackingContext.from_box(box, page, child_contexts)
if isinstance(box, boxes.BlockLevelBox):
blocks_index = len(blocks)
blocks_and_cells_index = len(blocks_and_cells)
elif isinstance(box, boxes.TableCellBox):
blocks_index = None
blocks_and_cells_index = len(blocks_and_cells)
blocks_index = None
blocks_and_cells_index = None
box = dispatch_children(box)
# Insert at the positions before dispatch the children.
if blocks_index is not None:
blocks.insert(blocks_index, box)
if blocks_and_cells_index is not None:
blocks_and_cells.insert(blocks_and_cells_index, box)
return box
def dispatch_children(box):
if not isinstance(box, boxes.ParentBox):
return box
new_children = []
for child in box.children:
result = dispatch(child)
if result is not None:
return box.copy_with_children(new_children)
box = dispatch_children(box)
return cls(box, children, blocks, floats, blocks_and_cells, page)
Jump to Line
Something went wrong with that request. Please try again.