2011-04-26 20:07:19 +04:00
|
|
|
# coding: utf8
|
|
|
|
|
2011-04-28 21:15:30 +04:00
|
|
|
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
|
|
|
|
# Copyright (C) 2011 Simon Sapin
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-05-06 19:46:46 +04:00
|
|
|
import os.path
|
2011-04-26 20:07:19 +04:00
|
|
|
from attest import Tests, assert_hook
|
|
|
|
from lxml import html
|
|
|
|
#from lxml.html import html5parser as html # API is the same as lxml.html
|
2011-04-27 20:24:59 +04:00
|
|
|
import cssutils
|
2011-04-26 20:07:19 +04:00
|
|
|
from cssutils.helper import path2url
|
|
|
|
|
2011-04-27 19:50:12 +04:00
|
|
|
from .. import css
|
2011-05-10 12:50:04 +04:00
|
|
|
from ..css.computed_values import get_value
|
2011-04-26 20:07:19 +04:00
|
|
|
|
|
|
|
from . import resource_filename
|
|
|
|
|
|
|
|
|
|
|
|
suite = Tests()
|
|
|
|
|
|
|
|
def parse_html(filename):
|
|
|
|
"""Parse an HTML file from the test resources and resolve relative URL."""
|
2011-05-04 13:47:04 +04:00
|
|
|
document = html.parse(path2url(resource_filename(filename))).getroot()
|
2011-04-26 20:07:19 +04:00
|
|
|
return document
|
|
|
|
|
|
|
|
|
|
|
|
@suite.test
|
|
|
|
def test_find_stylesheets():
|
|
|
|
document = parse_html('doc1.html')
|
|
|
|
|
2011-04-28 12:44:50 +04:00
|
|
|
sheets = list(css.find_stylesheets(document))
|
2011-04-26 20:07:19 +04:00
|
|
|
assert len(sheets) == 2
|
2011-05-10 13:41:23 +04:00
|
|
|
# Also test that stylesheets are in tree order
|
|
|
|
assert [s.href.rsplit('/', 1)[-1] for s in sheets] \
|
|
|
|
== ['sheet1.css', 'doc1.html']
|
2011-04-26 20:07:19 +04:00
|
|
|
|
2011-04-28 12:44:50 +04:00
|
|
|
rules = list(rule for sheet in sheets
|
|
|
|
for rule in css.resolve_import_media(sheet, 'print'))
|
2011-05-12 18:06:47 +04:00
|
|
|
assert len(rules) == 8
|
|
|
|
# Also test appearance order
|
|
|
|
assert [rule.selectorText for rule in rules] \
|
|
|
|
== ['li', 'p', 'ul', 'a', 'a:after', ':first', 'ul',
|
|
|
|
'body > h1:first-child']
|
2011-04-26 20:07:19 +04:00
|
|
|
|
2011-04-27 20:24:59 +04:00
|
|
|
|
|
|
|
@suite.test
|
|
|
|
def test_expand_shorthands():
|
|
|
|
sheet = cssutils.parseFile(resource_filename('sheet2.css'))
|
|
|
|
assert sheet.cssRules[0].selectorText == 'li'
|
|
|
|
style = sheet.cssRules[0].style
|
2011-04-28 12:44:50 +04:00
|
|
|
assert style['margin'] == '2em 0'
|
2011-04-28 13:33:44 +04:00
|
|
|
assert style['margin-bottom'] == '3em'
|
|
|
|
assert style['margin-left'] == '4em'
|
2011-04-28 12:44:50 +04:00
|
|
|
assert not style['margin-top']
|
2011-04-27 20:24:59 +04:00
|
|
|
css.expand_shorthands(sheet)
|
2011-04-28 13:33:44 +04:00
|
|
|
# expand_shorthands() builds new style object
|
|
|
|
style = sheet.cssRules[0].style
|
2011-04-28 12:44:50 +04:00
|
|
|
assert not style['margin']
|
|
|
|
assert style['margin-top'] == '2em'
|
|
|
|
assert style['margin-right'] == '0'
|
2011-04-28 13:33:44 +04:00
|
|
|
assert style['margin-bottom'] == '2em', \
|
|
|
|
"3em was before the shorthand, should be masked"
|
|
|
|
assert style['margin-left'] == '4em', \
|
|
|
|
"4em was after the shorthand, should not be masked"
|
2011-05-04 13:47:04 +04:00
|
|
|
|
|
|
|
|
|
|
|
@suite.test
|
|
|
|
def test_annotate_document():
|
|
|
|
user_stylesheet = cssutils.parseFile(resource_filename('user.css'))
|
|
|
|
ua_stylesheet = cssutils.parseFile(resource_filename('mini_ua.css'))
|
|
|
|
document = parse_html('doc1.html')
|
|
|
|
|
|
|
|
css.annotate_document(document, [user_stylesheet], [ua_stylesheet])
|
2011-04-27 20:24:59 +04:00
|
|
|
|
2011-05-04 13:47:04 +04:00
|
|
|
# Element objects behave a lists of their children
|
|
|
|
head, body = document
|
2011-05-06 19:46:46 +04:00
|
|
|
h1, p, ul = body
|
2011-05-04 13:47:04 +04:00
|
|
|
li = list(ul)
|
|
|
|
a, = li[0]
|
2011-05-12 18:06:47 +04:00
|
|
|
after = a.pseudo_elements['after']
|
2011-05-04 13:47:04 +04:00
|
|
|
|
2011-05-13 19:25:49 +04:00
|
|
|
assert h1.style['background-image'][0].absolute_uri == 'file://' \
|
2011-05-06 19:46:46 +04:00
|
|
|
+ os.path.abspath(resource_filename('logo_small.png'))
|
|
|
|
|
2011-05-06 20:05:04 +04:00
|
|
|
assert h1.style['font-weight'][0].value == 700
|
|
|
|
|
2011-05-04 13:47:04 +04:00
|
|
|
sides = ('-top', '-right', '-bottom', '-left')
|
2011-05-04 19:45:36 +04:00
|
|
|
# 32px = 1em * font-size: 2em * initial 16px
|
|
|
|
for side, expected_value in zip(sides, ('32px', '0', '32px', '0')):
|
2011-05-10 12:50:04 +04:00
|
|
|
assert get_value(p.style, 'margin' + side) == expected_value
|
2011-05-04 13:47:04 +04:00
|
|
|
|
2011-05-04 19:45:36 +04:00
|
|
|
# 32px = 2em * initial 16px
|
|
|
|
for side, expected_value in zip(sides, ('32px', '32px', '32px', '32px')):
|
2011-05-10 12:50:04 +04:00
|
|
|
assert get_value(ul.style, 'margin' + side) == expected_value
|
2011-05-06 18:23:29 +04:00
|
|
|
|
|
|
|
# thick = 5px, 0.25 inches = 96*.25 = 24px
|
|
|
|
for side, expected_value in zip(sides, ('0', '5px', '0', '24px')):
|
2011-05-10 12:50:04 +04:00
|
|
|
assert get_value(ul.style, 'border' + side + '-width') == expected_value
|
2011-05-04 13:47:04 +04:00
|
|
|
|
2011-05-04 19:45:36 +04:00
|
|
|
# 32px = 2em * initial 16px
|
|
|
|
# 64px = 4em * initial 16px
|
|
|
|
for side, expected_value in zip(sides, ('32px', '0', '32px', '64px')):
|
2011-05-10 12:50:04 +04:00
|
|
|
assert get_value(li[0].style, 'margin' + side) == expected_value
|
2011-05-04 13:47:04 +04:00
|
|
|
|
2011-05-10 12:50:04 +04:00
|
|
|
assert get_value(a.style, 'text-decoration') == 'underline'
|
2011-05-04 20:29:41 +04:00
|
|
|
|
|
|
|
color = a.style['color'][0]
|
|
|
|
assert (color.red, color.green, color.blue, color.alpha) == (255, 0, 0, 1)
|
|
|
|
|
2011-05-13 19:25:49 +04:00
|
|
|
# The href attr should be as in the source, not made absolute.
|
|
|
|
assert ''.join(v.value for v in after.style['content']) == ' [home.html]'
|
2011-05-12 18:06:47 +04:00
|
|
|
|
2011-05-04 13:47:04 +04:00
|
|
|
# TODO much more tests here: test that origin and selector precedence
|
|
|
|
# and inheritance are correct, ...
|
|
|
|
|