Merge pull request #135 from Tigge/master
Add support for adding align tags on images from css float
This commit is contained in:
commit
cc435cad0f
|
@ -114,7 +114,8 @@ class Premailer(object):
|
|||
cache_css_parsing=True,
|
||||
cssutils_logging_handler=None,
|
||||
cssutils_logging_level=None,
|
||||
disable_leftover_css=False):
|
||||
disable_leftover_css=False,
|
||||
align_floating_images=True):
|
||||
self.html = html
|
||||
self.base_url = base_url
|
||||
self.preserve_internal_links = preserve_internal_links
|
||||
|
@ -141,6 +142,7 @@ class Premailer(object):
|
|||
self.disable_validation = disable_validation
|
||||
self.cache_css_parsing = cache_css_parsing
|
||||
self.disable_leftover_css = disable_leftover_css
|
||||
self.align_floating_images = align_floating_images
|
||||
|
||||
if cssutils_logging_handler:
|
||||
cssutils.log.addHandler(cssutils_logging_handler)
|
||||
|
@ -409,6 +411,17 @@ class Premailer(object):
|
|||
parent = item.getparent()
|
||||
del parent.attrib['class']
|
||||
|
||||
# Add align attributes to images if they have a CSS float value of
|
||||
# right or left. Outlook (both on desktop and on the web) are bad at
|
||||
# understanding floats, but they do understand the HTML align attrib.
|
||||
if self.align_floating_images:
|
||||
for item in page.xpath('//img[@style]'):
|
||||
image_css = cssutils.parseStyle(item.attrib['style'])
|
||||
if image_css.float == 'right':
|
||||
item.attrib['align'] = 'right'
|
||||
elif image_css.float == 'left':
|
||||
item.attrib['align'] = 'left'
|
||||
|
||||
#
|
||||
# URLs
|
||||
#
|
||||
|
|
|
@ -2419,3 +2419,37 @@ sheet" type="text/css">
|
|||
# Because you can't set a base_url without a full protocol
|
||||
p = Premailer(html, base_url='www.peterbe.com')
|
||||
assert_raises(ValueError, p.transform)
|
||||
|
||||
def test_align_float_images(self):
|
||||
|
||||
html = """<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
<style>
|
||||
.floatright {
|
||||
float: right;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p><img src="/images/left.jpg" style="float: left"> text
|
||||
<img src="/images/right.png" class="floatright"> text
|
||||
<img src="/images/nofloat.gif"> text
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
expect_html = """<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<p><img src="/images/left.jpg" style="float: left" align="left"> text
|
||||
<img src="/images/right.png" style="float:right" align="right"> text
|
||||
<img src="/images/nofloat.gif"> text
|
||||
</p>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
p = Premailer(html, align_floating_images=True)
|
||||
result_html = p.transform()
|
||||
compare_html(expect_html, result_html)
|
||||
|
|
Loading…
Reference in New Issue