mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 07:57:52 +03:00
Add support for data: URL scheme stylesheets.
This commit is contained in:
parent
fc1bb2dd29
commit
f6940496f1
@ -83,6 +83,7 @@ def find_stylesheets(document):
|
||||
Find and parse stylesheets in a Document object. Return an iterable of
|
||||
stylesheets, in tree order.
|
||||
"""
|
||||
parser = document.css_parser
|
||||
for element in document.dom.iter():
|
||||
if element.tag not in ('style', 'link'):
|
||||
continue
|
||||
@ -102,15 +103,17 @@ def find_stylesheets(document):
|
||||
content = ''.join(content)
|
||||
# lxml should give us either unicode or ASCII-only bytestrings, so
|
||||
# we don't need `encoding` here.
|
||||
yield parseString(content, href=element.base_url,
|
||||
media=media_attr, title=element.get('title'))
|
||||
yield parser.parseString(content,
|
||||
href=element.base_url,
|
||||
media=media_attr, title=element.get('title'))
|
||||
elif element.tag == 'link' and element.get('href') \
|
||||
and ' stylesheet ' in ' %s ' % element.get('rel', ''):
|
||||
# URLs should NOT have been made absolute earlier
|
||||
# TODO: support the <base> HTML element, but do not use
|
||||
# lxml.html.HtmlElement.make_links_absolute() that changes the tree
|
||||
href = get_url_attribute(element, 'href')
|
||||
yield parseUrl(href, media=media_attr, title=element.get('title'))
|
||||
yield parser.parseUrl(href,
|
||||
media=media_attr, title=element.get('title'))
|
||||
|
||||
|
||||
def find_style_attributes(document):
|
||||
|
@ -21,7 +21,7 @@ import os.path
|
||||
import math
|
||||
import logging
|
||||
|
||||
from cssutils import parseFile
|
||||
from cssutils import parseFile, CSSParser
|
||||
import lxml.html
|
||||
import cairo
|
||||
|
||||
@ -54,6 +54,9 @@ class Document(object):
|
||||
# Go through the property setter which calls ensure_url()
|
||||
self.base_url = self.base_url
|
||||
|
||||
self.css_parser = CSSParser()
|
||||
self.css_parser.setFetcher(utils.urllib_fetcher)
|
||||
|
||||
self.user_stylesheets = user_stylesheets or []
|
||||
self.user_agent_stylesheets = user_agent_stylesheets or []
|
||||
|
||||
|
@ -2,10 +2,11 @@
|
||||
<head>
|
||||
<meta http-equiv=Content-Type content=text/html;charset=utf8>
|
||||
<link rel=stylesheet href="sheet1.css" media=screen>
|
||||
<!-- currentColor means 'inherit' on color itself. -->
|
||||
<link rel=stylesheet href="data:text/css,a%7Bcolor%3AcurrentColor%7D">
|
||||
<style media=print>
|
||||
@import url(sheet1.css);
|
||||
li { color: red; }
|
||||
a { color: currentColor; /* means 'inherit' on color itself. */ }
|
||||
@import url(data:text/css,li%7Bcolor%3Ared%7D);
|
||||
a:after {
|
||||
content: " [" attr(href) "]"
|
||||
}
|
||||
|
@ -56,17 +56,18 @@ def test_find_stylesheets():
|
||||
document = parse_html('doc1.html')
|
||||
|
||||
sheets = list(css.find_stylesheets(document))
|
||||
assert len(sheets) == 2
|
||||
assert len(sheets) == 3
|
||||
# Also test that stylesheets are in tree order
|
||||
assert [s.href.rsplit('/', 1)[-1] for s in sheets] \
|
||||
== ['sheet1.css', 'doc1.html']
|
||||
assert [s.href.rsplit('/', 1)[-1].rsplit(',', 1)[-1] for s in sheets] \
|
||||
== ['sheet1.css', 'a%7Bcolor%3AcurrentColor%7D',
|
||||
'doc1.html']
|
||||
|
||||
rules = list(rule for sheet in sheets
|
||||
for rule in css.effective_rules(sheet, 'print'))
|
||||
assert len(rules) == 9
|
||||
# Also test appearance order
|
||||
assert [rule.selectorText for rule in rules] \
|
||||
== ['li', 'p', 'ul', 'li', 'a', 'a:after', ':first', 'ul',
|
||||
== ['a', 'li', 'p', 'ul', 'li', 'a:after', ':first', 'ul',
|
||||
'body > h1:first-child']
|
||||
|
||||
|
||||
|
@ -31,7 +31,7 @@ def test_multifunction():
|
||||
@utils.MultiFunction
|
||||
def foo():
|
||||
"Hello, docstrings!"
|
||||
|
||||
|
||||
assert foo.__name__ == 'foo'
|
||||
assert foo.__doc__ == 'Hello, docstrings!'
|
||||
assert foo.__module__ == __name__
|
||||
|
@ -16,6 +16,8 @@
|
||||
# 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/>.
|
||||
|
||||
|
||||
import urllib
|
||||
try:
|
||||
from urlparse import urljoin, urlparse
|
||||
except ImportError:
|
||||
@ -72,3 +74,17 @@ def ensure_url(filename_or_url):
|
||||
return filename_or_url
|
||||
else:
|
||||
return path2url(filename_or_url)
|
||||
|
||||
|
||||
def urllib_fetcher(url):
|
||||
"""
|
||||
A "fetcher" for cssutils, based on urllib instead of urllib2, since
|
||||
urllib has support for the "data" URL scheme.
|
||||
"""
|
||||
result = urllib.urlopen(url)
|
||||
info = result.info()
|
||||
if info.gettype() != 'text/css':
|
||||
# TODO: warn
|
||||
return None
|
||||
charset = info.getparam('charset')
|
||||
return charset, result.read()
|
||||
|
Loading…
Reference in New Issue
Block a user