mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 16:07:57 +03:00
Make user and user-agent stylesheets attributes of Document objects.
This commit is contained in:
parent
69156d98af
commit
9fa954b3bd
@ -381,9 +381,8 @@ def computed_from_cascaded(element, cascaded, parent_style, pseudo_type=None):
|
||||
return style
|
||||
|
||||
|
||||
def annotate_document(document, user_stylesheets=None,
|
||||
ua_stylesheets=(HTML4_DEFAULT_STYLESHEET,),
|
||||
medium='print'):
|
||||
def annotate_document(document, medium,
|
||||
user_stylesheets=None, ua_stylesheets=None):
|
||||
"""
|
||||
Do everything from finding author stylesheets in the given HTML document
|
||||
to parsing and applying them, to end up with a `style` attribute on
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
import lxml.html
|
||||
|
||||
from .css import annotate_document
|
||||
from .css import annotate_document, HTML4_DEFAULT_STYLESHEET
|
||||
from .formatting_structure.build import build_formatting_structure
|
||||
from .layout import layout
|
||||
|
||||
@ -28,6 +28,10 @@ class Document(object):
|
||||
def __init__(self, dom):
|
||||
assert getattr(dom, 'tag') == 'html', (
|
||||
'HTML document expected, got %r.' % (dom,))
|
||||
|
||||
self.user_stylesheets = []
|
||||
self.user_agent_stylesheets = [HTML4_DEFAULT_STYLESHEET]
|
||||
|
||||
#: lxml HtmlElement object
|
||||
self.dom = dom
|
||||
|
||||
@ -62,9 +66,12 @@ class Document(object):
|
||||
"""
|
||||
return self.computed_styles[(element, pseudo_type)]
|
||||
|
||||
def do_css(self, **kwargs):
|
||||
def do_css(self):
|
||||
if self.computed_styles is None:
|
||||
self.computed_styles = annotate_document(self, **kwargs)
|
||||
self.computed_styles = annotate_document(self,
|
||||
user_stylesheets=self.user_stylesheets,
|
||||
ua_stylesheets=self.user_agent_stylesheets,
|
||||
medium='print')
|
||||
|
||||
def do_boxes(self):
|
||||
self.do_css()
|
||||
|
@ -103,8 +103,9 @@ def test_annotate_document():
|
||||
ua_stylesheet = cssutils.parseFile(resource_filename('mini_ua.css'))
|
||||
document = parse_html('doc1.html')
|
||||
|
||||
document.do_css(user_stylesheets=[user_stylesheet],
|
||||
ua_stylesheets=[ua_stylesheet])
|
||||
document.user_agent_stylesheets = [ua_stylesheet]
|
||||
document.user_stylesheets = [user_stylesheet]
|
||||
document.do_css()
|
||||
|
||||
# Element objects behave a lists of their children
|
||||
_head, body = document.dom
|
||||
@ -178,7 +179,8 @@ def test_page():
|
||||
margin-bottom: 12pt;
|
||||
}
|
||||
""")
|
||||
document.do_css(user_stylesheets=[user_sheet])
|
||||
document.user_stylesheets.append(user_sheet)
|
||||
document.do_css()
|
||||
|
||||
style = document.style_for('@page', 'first_left')
|
||||
assert style.margin_top == 5
|
||||
|
Loading…
Reference in New Issue
Block a user