Merge pull request #155 from peterbe/tidying-up-branch-for-hex-features-fixes-143-114

Tidying up branch for hex features fixes 143 114
This commit is contained in:
Peter Bengtsson 2016-01-13 13:26:27 -05:00
commit e5f73b074a
2 changed files with 57 additions and 2 deletions

View File

@ -88,6 +88,9 @@ def _cache_parse_css_string(css_body, validate=True):
_element_selector_regex = re.compile(r'(^|\s)\w')
_cdata_regex = re.compile(r'\<\!\[CDATA\[(.*?)\]\]\>', re.DOTALL)
_importants = re.compile('\s*!important')
#: The short (3-digit) color codes that cause issues for IBM Notes
_short_color_codes = re.compile(r'^#([0-9a-f])([0-9a-f])([0-9a-f])$', re.I)
# These selectors don't apply to all elements. Rather, they specify
# which elements to apply to.
FILTER_PSEUDOSELECTORS = [':last-child', ':first-child', 'nth-child']
@ -510,6 +513,17 @@ class Premailer(object):
return css_body
@staticmethod
def six_color(color_value):
"""Fix background colors for Lotus Notes
Notes which fails to handle three character ``bgcolor`` codes well.
see <https://github.com/peterbe/premailer/issues/114>"""
# Turn the color code from three to six digits
retval = _short_color_codes.sub(r'#\1\1\2\2\3\3', color_value)
return retval
def _style_to_basic_html_attributes(self, element, style_content,
force=False):
"""given an element and styles like
@ -534,8 +548,15 @@ class Premailer(object):
attributes['align'] = value.strip()
elif key == 'vertical-align':
attributes['valign'] = value.strip()
elif key == 'background-color':
attributes['bgcolor'] = value.strip()
elif (
key == 'background-color' and
'transparent' not in value.lower()
):
# Only add the 'bgcolor' attribute if the value does not
# contain the word "transparent"; before we add it possibly
# correct the 3-digit color code to its 6-digit equivalent
# ("abc" to "aabbcc") so IBM Notes copes.
attributes['bgcolor'] = self.six_color(value.strip())
elif key == 'width' or key == 'height':
value = value.strip()
if value.endswith('px'):

View File

@ -2501,3 +2501,37 @@ sheet" type="text/css">
self.assertTrue(p.remove_unset_properties)
result_html = p.transform()
compare_html(expect_html, result_html)
def test_six_color(self):
r = Premailer.six_color('#cde')
e = '#ccddee'
self.assertEqual(e, r)
def test_3_digit_color_expand(self):
'Are 3-digit color values expanded into 6-digits for IBM Notes'
html = """<html>
<style>
body {background-color: #fe5;}
p {background-color: #123456;}
h1 {color: #f0df0d;}
</style>
<body>
<h1>color test</h1>
<p>
This is a test of color handling.
</p>
</body>
</html>"""
expect_html = """<html>
<head>
</head>
<body style="background-color:#fe5" bgcolor="#ffee55">
<h1 style="color:#f0df0d">color test</h1>
<p style="background-color:#123456" bgcolor="#123456">
This is a test of color handling.
</p>
</body>
</html>"""
p = Premailer(html, remove_unset_properties=True)
result_html = p.transform()
compare_html(expect_html, result_html)