Merge pull request #165 from ccorcos/master

Inline Important Tags
This commit is contained in:
Peter Bengtsson 2016-04-13 16:17:06 -04:00
commit 18539ced84
3 changed files with 46 additions and 2 deletions

View File

@ -8,6 +8,13 @@ except ImportError: # pragma: no cover
from ordereddict import OrderedDict
def format_value(prop):
if prop.priority == "important":
return prop.propertyValue.cssText.strip() + ' !important'
else:
return prop.propertyValue.cssText.strip()
def csstext_to_pairs(csstext):
"""
csstext_to_pairs takes css text and make it to list of
@ -18,7 +25,7 @@ def csstext_to_pairs(csstext):
with csstext_to_pairs._lock:
return sorted(
[
(prop.name.strip(), prop.propertyValue.cssText.strip())
(prop.name.strip(), format_value(prop))
for prop in cssutils.parseStyle(csstext)
],
key=itemgetter(0)

View File

@ -185,12 +185,18 @@ class Premailer(object):
The bulk of the rule should not end in a semicolon.
"""
def format_css_property(prop):
if self.strip_important or prop.priority != 'important':
return '{0}:{1}'.format(prop.name, prop.value)
else:
return '{0}:{1} !important'.format(prop.name, prop.value)
def join_css_properties(properties):
""" Accepts a list of cssutils Property objects and returns
a semicolon delimitted string like 'color: red; font-size: 12px'
"""
return ';'.join(
'{0}:{1}'.format(prop.name, prop.value)
format_css_property(prop)
for prop in properties
)

View File

@ -2540,3 +2540,34 @@ sheet" type="text/css">
p = Premailer(html, remove_unset_properties=True)
result_html = p.transform()
compare_html(expect_html, result_html)
def test_inline_important(self):
'Are !important tags preserved inline.'
html = """<html>
<head>
<title></title>
</head>
<body>
<style type="text/css">.something { display:none !important; }</style>
<div class="something">blah</div>
</body>
</html>"""
expect_html = """<html>
<head>
<title></title>
</head>
<body>
<style type="text/css">.something { display:none !important; }</style>
<div class="something" style="display:none !important">blah</div>
</body>
</html>"""
p = Premailer(
html,
remove_classes=False,
keep_style_tags=True,
strip_important=False
)
result_html = p.transform()
compare_html(expect_html, result_html)