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

Use the testing UA stylesheet for PDF tests.

This commit is contained in:
Simon Sapin 2012-05-29 19:40:38 +02:00
parent 17b55f309d
commit cb76c822f2
3 changed files with 59 additions and 29 deletions

View File

@ -20,3 +20,13 @@ col { display: table-column }
colgroup { display: table-column-group }
td, th { display: table-cell }
caption { display: table-caption }
a[href] { -weasy-link: attr(href); }
a[name] { -weasy-anchor: attr(name); }
*[id] { -weasy-anchor: attr(id); }
h1 { -weasy-bookmark-level: 1; -weasy-bookmark-label: contents; }
h2 { -weasy-bookmark-level: 2; -weasy-bookmark-label: contents; }
h3 { -weasy-bookmark-level: 3; -weasy-bookmark-label: contents; }
h4 { -weasy-bookmark-level: 4; -weasy-bookmark-label: contents; }
h5 { -weasy-bookmark-level: 5; -weasy-bookmark-label: contents; }
h6 { -weasy-bookmark-level: 6; -weasy-bookmark-label: contents; }

View File

@ -16,10 +16,9 @@ import io
import cairo
from .. import HTML
from ..backends import MetadataPDFBackend
from .. import HTML, CSS
from .. import pdf
from .testing_utils import assert_no_logs, resource_filename
from .testing_utils import assert_no_logs, resource_filename, TestPDFDocument
@assert_no_logs
@ -40,10 +39,10 @@ def test_pdf_parser():
assert sizes == [b'0 0 100 100', b'0 0 200 10', b'0 0 3.14 987654321']
def get_metadata(html):
document = HTML(
string=html, base_url=resource_filename('<inline HTML>')
)._get_document(MetadataPDFBackend, [])
def get_metadata(html, base_url=resource_filename('<inline HTML>')):
document = TestPDFDocument(html, base_url=base_url,
user_stylesheets=[CSS(
string='@page { -weasy-size: 500pt 1000pt; margin: 50pt }')])
return pdf.gather_metadata(document)
@ -61,8 +60,8 @@ def get_bookmarks(html, structure_only=False):
return root, bookmarks
def get_links(html):
_bookmarks, links, anchors = get_metadata(html)
def get_links(html, **kwargs):
_bookmarks, links, anchors = get_metadata(html, **kwargs)
links = [
[(uri, tuple(round(v, 6) for v in rect)) for uri, rect in page_links]
for page_links in links]
@ -84,12 +83,11 @@ def test_bookmarks():
root, bookmarks = get_bookmarks('''
<style>
@page { -weasy-size: 1000pt; margin: 0 }
* { height: 90pt; margin: 0 0 10pt 0; page-break-inside: auto }
</style>
<h1>Title 1</h1>
<h1>Title 2</h1>
<div style="margin-left: 50pt"><h2>Title 3</h2></div>
<div style="margin-left: 20pt"><h2>Title 3</h2></div>
<h2>Title 4</h2>
<h3>
Title 5
@ -105,27 +103,27 @@ def test_bookmarks():
assert root == dict(Count=11, First=1, Last=10)
assert bookmarks == [
dict(Count=0, First=None, Last=None, Next=2, Parent=0, Prev=None,
label='Title 1', destination=(0, 0, 1000)),
label='Title 1', destination=(0, 50, 950)),
dict(Count=4, First=3, Last=6, Next=7, Parent=0, Prev=1,
label='Title 2', destination=(0, 0, 900)),
label='Title 2', destination=(0, 50, 850)),
dict(Count=0, First=None, Last=None, Next=4, Parent=2, Prev=None,
label='Title 3', destination=(0, 50, 800)),
label='Title 3', destination=(0, 70, 750)),
dict(Count=1, First=5, Last=5, Next=6, Parent=2, Prev=3,
label='Title 4', destination=(0, 0, 700)),
label='Title 4', destination=(0, 50, 650)),
dict(Count=0, First=None, Last=None, Next=None, Parent=4, Prev=None,
label='Title 5', destination=(0, 0, 600)),
label='Title 5', destination=(0, 50, 550)),
dict(Count=0, First=None, Last=None, Next=None, Parent=2, Prev=4,
label='Title 6', destination=(1, 0, 900)),
label='Title 6', destination=(1, 50, 850)),
dict(Count=2, First=8, Last=8, Next=10, Parent=0, Prev=2,
label='Title 7', destination=(1, 0, 800)),
label='Title 7', destination=(1, 50, 750)),
dict(Count=1, First=9, Last=9, Next=None, Parent=7, Prev=None,
label='Title 8', destination=(1, 0, 700)),
label='Title 8', destination=(1, 50, 650)),
dict(Count=0, First=None, Last=None, Next=None, Parent=8, Prev=None,
label='Title 9', destination=(1, 0, 600)),
label='Title 9', destination=(1, 50, 550)),
dict(Count=1, First=11, Last=11, Next=None, Parent=0, Prev=7,
label='Title 10', destination=(1, 0, 500)),
label='Title 10', destination=(1, 50, 450)),
dict(Count=0, First=None, Last=None, Next=None, Parent=10, Prev=None,
label='Title 11', destination=(1, 0, 400))]
label='Title 11', destination=(1, 50, 350))]
root, bookmarks = get_bookmarks('''
<h2>1</h2> level 1
@ -174,7 +172,6 @@ def test_links():
links, anchors = get_links('''
<style>
@page { -weasy-size: 1000pt; margin: 50pt }
body { margin: 0; font-size: 10pt; line-height: 2 }
p { display: block; height: 90pt; margin: 0 0 10pt 0 }
</style>
@ -184,7 +181,7 @@ def test_links():
src=pattern.png></a></p>
<p id=hello>Hello, World</p>
<p id=lipsum><a style="display: block; page-break-before: always"
href="#hello">Lorem ipsum ...</a></p>
href="#hel%6Co">Lorem ipsum ...</a></p>
''')
assert links == [
[
@ -193,8 +190,20 @@ def test_links():
# 5pt wide (image + 2 * 1pt of border), 20pt high
(('internal', 'lipsum'), (60, 850, 65, 830)),
], [
# 900pt wide (block), 20pt high
(('internal', 'hello'), (50, 950, 950, 930)),
# 400pt wide (block), 20pt high
(('internal', 'hello'), (50, 950, 450, 930)),
]
]
assert anchors == {'hello': (0, 50, 750), 'lipsum': (1, 50, 950)}
links, anchors = get_links(
'<div style="-weasy-link: url(../lipsum)">',
base_url='http://weasyprint.org/foo/bar/')
assert links == [[(('external', 'http://weasyprint.org/foo/lipsum'),
(50, 950, 450, 950))]]
# Relative URI reference without a base URI.
# links, anchors = get_links(
# '<div style="-weasy-link: url(../lipsum)">',
# base_url=None)
# assert links == [[]]

View File

@ -20,7 +20,7 @@ import functools
from .. import HTML, CSS
from ..document import Document
from ..backends import PNGBackend
from ..backends import PNGBackend, MetadataPDFBackend
from ..logger import LOGGER
@ -33,20 +33,31 @@ TEST_UA_STYLESHEET = CSS(os.path.join(
class TestPNGDocument(Document):
"""Like PNGDocument, but with a different user-agent stylesheet.
"""A Document with a PNG backend and a different user-agent stylesheet.
This stylesheet is shorter, which makes tests faster.
"""
backend_class = PNGBackend
def __init__(self, html_source, base_url=None, user_stylesheets=(),
user_agent_stylesheets=(TEST_UA_STYLESHEET,)):
super(TestPNGDocument, self).__init__(
PNGBackend,
self.backend_class,
HTML(string=html_source, base_url=base_url).root_element,
user_stylesheets=user_stylesheets,
user_agent_stylesheets=user_agent_stylesheets)
class TestPDFDocument(TestPNGDocument):
"""A Document with a PDF backend and a different user-agent stylesheet.
This stylesheet is shorter, which makes tests faster.
"""
backend_class = MetadataPDFBackend
def resource_filename(basename):
"""Return the absolute path of the resource called ``basename``."""
return os.path.join(os.path.dirname(__file__), 'resources', basename)