Skip to content

Commit

Permalink
Kill Document in css/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jul 12, 2012
1 parent 163b85b commit d038708
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
31 changes: 16 additions & 15 deletions weasyprint/css/__init__.py
Expand Up @@ -155,13 +155,13 @@ def inherit_from(self):
anonymous = False


def find_stylesheets(document, medium):
"""Yield the stylesheets of ``document``.
def find_stylesheets(element_tree, medium):
"""Yield the stylesheets in ``element_tree``.
The output order is the same as the order of the dom.
The output order is the same as the source order.
"""
for element in document.dom.iter():
for element in element_tree.iter():
if element.tag not in ('style', 'link'):
continue
mime_type = element.get('type', 'text/css').split(';', 1)[0].strip()
Expand Down Expand Up @@ -191,13 +191,13 @@ def find_stylesheets(document, medium):
yield CSS(url=href, _check_mime_type=True)


def find_style_attributes(document):
def find_style_attributes(element_tree):
"""
Yield ``element, declaration, base_url`` for elements with
a "style" attribute.
"""
parser = PARSER
for element in document.dom.iter():
for element in element_tree.iter():
style_attribute = element.get('style')
if style_attribute:
declarations, errors = parser.parse_style_attr(style_attribute)
Expand Down Expand Up @@ -383,7 +383,7 @@ def preprocess_stylesheet(medium, base_url, rules):
declarations = list(preprocess_declarations(
base_url, rule.declarations))

# The double lambda to have a closure that holds page_types
# Use a double lambda to have a closure that holds page_types
match = (lambda page_types: lambda _document: page_types)(
PAGE_PSEUDOCLASS_TARGETS[pseudo_class])
specificity = rule.specificity
Expand All @@ -401,17 +401,18 @@ def preprocess_stylesheet(medium, base_url, rules):
yield margin_rule, selector_list, declarations


def get_all_computed_styles(document, medium,
def get_all_computed_styles(element_tree, medium,
user_stylesheets=None, ua_stylesheets=None):
"""Compute all the computed styles of ``document`` for ``medium``.
"""Compute all the computed styles of all elements in ``element_tree``
for the media type ``medium``.
Do everything from finding author stylesheets in the given HTML document
to parsing and applying them.
Return a dict of (DOM element, pseudo element type) -> StyleDict instance.
Return a dict of (element, pseudo element type) -> StyleDict instance.
"""
author_stylesheets = list(find_stylesheets(document, medium))
author_stylesheets = list(find_stylesheets(element_tree, medium))

# keys: (element, pseudo_element_type)
# element: a lxml element object or the '@page' string for @page styles
Expand All @@ -437,7 +438,7 @@ def get_all_computed_styles(document, medium,
for selector in selector_list:
specificity = selector.specificity
pseudo_type = selector.pseudo_element
for element in selector.match(document.dom):
for element in selector.match(element_tree):
for name, values, importance in declarations:
precedence = declaration_precedence(
origin, importance)
Expand All @@ -447,7 +448,7 @@ def get_all_computed_styles(document, medium,
element, pseudo_type)

specificity = (1, 0, 0, 0)
for element, declarations, base_url in find_style_attributes(document):
for element, declarations, base_url in find_style_attributes(element_tree):
for name, values, importance in preprocess_declarations(
base_url, declarations):
precedence = declaration_precedence('author', importance)
Expand All @@ -465,7 +466,7 @@ def get_all_computed_styles(document, medium,
# their children, for inheritance.

# Iterate on all elements, even if there is no cascaded style for them.
for element in document.dom.iter():
for element in element_tree.iter():
set_computed_styles(cascaded_styles, computed_styles, element,
parent=element.getparent())

Expand All @@ -478,7 +479,7 @@ def get_all_computed_styles(document, medium,
set_computed_styles(cascaded_styles, computed_styles, page_type,
# @page inherits from the root element:
# http://lists.w3.org/Archives/Public/www-style/2012Jan/1164.html
parent=document.dom)
parent=element_tree)

# Then computed styles for pseudo elements, in any order.
# Pseudo-elements inherit from their associated element so they come
Expand Down
6 changes: 3 additions & 3 deletions weasyprint/document.py
Expand Up @@ -28,10 +28,10 @@

class Document(object):
"""Abstract output document."""
def __init__(self, dom, enable_hinting, user_stylesheets,
def __init__(self, element_tree, enable_hinting, user_stylesheets,
user_agent_stylesheets):
self.enable_hinting = enable_hinting
self.dom = dom #: lxml HtmlElement object
self.element_tree = element_tree #: lxml HtmlElement object
self.user_stylesheets = user_stylesheets
self.user_agent_stylesheets = user_agent_stylesheets
self._image_cache = {}
Expand All @@ -58,7 +58,7 @@ def computed_styles(self):
"""
if self._computed_styles is None:
self._computed_styles = get_all_computed_styles(
self,
self.element_tree,
user_stylesheets=self.user_stylesheets,
ua_stylesheets=self.user_agent_stylesheets,
medium='print')
Expand Down
2 changes: 1 addition & 1 deletion weasyprint/formatting_structure/build.py
Expand Up @@ -50,7 +50,7 @@
def build_formatting_structure(document, computed_styles):
"""Build a formatting structure (box tree) from a ``document``."""
# TODO: use computed_styles intsead of document.style_for()
box, = dom_to_box(document, document.dom)
box, = dom_to_box(document, document.element_tree)
box.is_for_root_element = True
# If this is changed, maybe update weasy.layout.pages.make_margin_boxes()
process_whitespace(box)
Expand Down
2 changes: 1 addition & 1 deletion weasyprint/tests/test_boxes.py
Expand Up @@ -81,7 +81,7 @@ def parse(html_content):
document = TestPNGDocument(html_content,
# Dummy filename, but in the right directory.
base_url=resource_filename('<test>'))
box, = build.dom_to_box(document, document.dom)
box, = build.dom_to_box(document, document.element_tree)
return box


Expand Down
6 changes: 3 additions & 3 deletions weasyprint/tests/test_css.py
Expand Up @@ -81,7 +81,7 @@ def test_find_stylesheets():
"""Test if the stylesheets are found in a HTML document."""
document = parse_html('doc1.html')

sheets = list(css.find_stylesheets(document, 'print'))
sheets = list(css.find_stylesheets(document.element_tree, 'print'))
assert len(sheets) == 2
# Also test that stylesheets are in tree order
assert [s.base_url.rsplit('/', 1)[-1].rsplit(',', 1)[-1] for s in sheets] \
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_annotate_document():
)

# Element objects behave a lists of their children
_head, body = document.dom
_head, body = document.element_tree
h1, p, ul = body
li_0, _li_1 = ul
a, = li_0
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_annotate_document():
def test_default_stylesheet():
"""Test if the user-agent stylesheet is used and applied."""
document = parse_html('doc1.html')
head_style = document.style_for(document.dom.head)
head_style = document.style_for(document.element_tree.head)
assert head_style.display == 'none', \
'The HTML4 user-agent stylesheet was not applied'

Expand Down

0 comments on commit d038708

Please sign in to comment.