mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 07:57:52 +03:00
Add support for URLs (images) in content
.
This commit is contained in:
parent
daa5901fde
commit
0595fa9287
@ -312,7 +312,7 @@ def content(computer, name, values):
|
||||
def compute_content_value(computer, value):
|
||||
"""Compute a content ``value``."""
|
||||
type_, content = value
|
||||
if type_ in ('STRING', 'QUOTE'):
|
||||
if type_ in ('STRING', 'QUOTE', 'URI'):
|
||||
return value
|
||||
else:
|
||||
assert type_ == 'ATTR'
|
||||
|
@ -277,6 +277,8 @@ def validate_content_value(value):
|
||||
type_ = value.type
|
||||
if type_ == 'STRING':
|
||||
return ('STRING', value.value)
|
||||
if type_ == 'URI':
|
||||
return ('URI', value.absoluteUri)
|
||||
elif type_ == 'FUNCTION':
|
||||
seq = list(value.seq)
|
||||
if len(seq) != 3 or seq[0].value != 'attr(' or seq[2].value != ')':
|
||||
|
@ -133,7 +133,20 @@ def dom_to_box(document, element, quote_depth=None, pseudo_type=None):
|
||||
assert pseudo_type in ('before', 'after')
|
||||
texts = []
|
||||
for type_, value in content:
|
||||
if type_ == 'QUOTE':
|
||||
if type_ == 'STRING':
|
||||
texts.append(value)
|
||||
elif type_ == 'URI':
|
||||
surface = document.get_image_surface_from_uri(value)
|
||||
if surface is not None:
|
||||
texts = u''.join(texts)
|
||||
if texts:
|
||||
children.extend(create_text_box(texts))
|
||||
texts = []
|
||||
children.append(boxes.InlineLevelReplacedBox(
|
||||
element.tag, element.sourceline, style.inherit_from(),
|
||||
html.ImageReplacement(surface)))
|
||||
else:
|
||||
assert type_ == 'QUOTE'
|
||||
is_open, insert = value
|
||||
if is_open:
|
||||
this_quote_depth = quote_depth[0]
|
||||
@ -146,9 +159,6 @@ def dom_to_box(document, element, quote_depth=None, pseudo_type=None):
|
||||
open_quotes, close_quotes = style.quotes
|
||||
quotes = open_quotes if is_open else close_quotes
|
||||
texts.append(quotes[min(this_quote_depth, len(quotes) - 1)])
|
||||
else:
|
||||
assert type_ == 'STRING'
|
||||
texts.append(value)
|
||||
texts = u''.join(texts)
|
||||
if texts:
|
||||
children.extend(create_text_box(texts))
|
||||
|
@ -730,7 +730,6 @@ def test_before_after():
|
||||
|
||||
assert_tree(parse_all(u'''
|
||||
<style>
|
||||
/* \A0 is the no-break space. */
|
||||
body { quotes: '«' '»' '“' '”' }
|
||||
q:before { content: open-quote ' '}
|
||||
q:after { content: ' ' close-quote }
|
||||
@ -752,3 +751,17 @@ def test_before_after():
|
||||
('q', 'Text', ' sit amet'),
|
||||
('q', 'Inline', [ # :after
|
||||
('q', 'Text', u' »')])])])])])
|
||||
|
||||
assert_tree(parse_all('''
|
||||
<style>
|
||||
p:before { content: 'a' url(pattern.png) 'b'}
|
||||
</style>
|
||||
<p>c</p>
|
||||
'''), [
|
||||
('p', 'Block', [
|
||||
('p', 'Line', [
|
||||
('p', 'Inline', [ # :before
|
||||
('p', 'Text', 'a'),
|
||||
('p', 'InlineLevelReplaced', '<replaced>'),
|
||||
('p', 'Text', 'b')]),
|
||||
('p', 'Text', 'c')])])])
|
||||
|
@ -936,3 +936,21 @@ def test_before_after():
|
||||
<p>« Lorem ipsum “ dolor ” sit amet »</p>
|
||||
''')
|
||||
)
|
||||
|
||||
test_same_rendering(100, 30,
|
||||
('pseudo_url', u'''
|
||||
<style>
|
||||
@page { size: 100px 30px }
|
||||
body { margin: 0; background: #fff; }
|
||||
p:before { content: 'a' url(pattern.png) 'b'}
|
||||
</style>
|
||||
<p>c</p>
|
||||
'''),
|
||||
('pseudo_url_reference', u'''
|
||||
<style>
|
||||
@page { size: 100px 30px }
|
||||
body { margin: 0; background: #fff }
|
||||
</style>
|
||||
<p>a<img src="pattern.png" alt="Missing image">bc</p>
|
||||
''')
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user