Repository: peterbe/premailer

Issue:
AttributeError: 'CSSUnknownRule' object has no attribute 'style' in _css_rules_to_string
Exception on complicated style sheet:

	>> from premailer import Premailer; import requests; url='https://www.smashingmagazine.com'; Premailer(html=requests.get(url).content.decode('utf-8'), base_url=url).transform()

	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	  File "/Users/lavr/dev/.env/lib/python3.6/site-packages/premailer/premailer.py", line 358, in transform
	    style.text = self._css_rules_to_string(these_leftover)
	  File "/Users/lavr/dev/.env/lib/python3.6/site-packages/premailer/premailer.py", line 613, in _css_rules_to_string
	    for key in rule.style.keys():
	AttributeError: 'CSSUnknownRule' object has no attribute 'style'

I know it's not usual source for email, but seems it covers some cssutils internals and might be easy to fix.

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/20290445/badge)](https://coveralls.io/builds/20290445)

Coverage remained the same at 100.0% when pulling **6b4a1b4b6216ffeb0e647d96d39551605ffe4344 on 201-handle-cssunknownrule** into **63f350d4477da71b8b9721e22b78f81afc83e1fb on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/20290445/badge)](https://coveralls.io/builds/20290445)

Coverage remained the same at 100.0% when pulling **6b4a1b4b6216ffeb0e647d96d39551605ffe4344 on 201-handle-cssunknownrule** into **63f350d4477da71b8b9721e22b78f81afc83e1fb on master**.


Patch:
diff --git a/README.rst b/README.rst
index 5fc563c..f8445af 100644
--- a/README.rst
+++ b/README.rst
@@ -10,7 +10,7 @@ Looking for sponsors
 
 This project is actively looking for corporate sponsorship. If you want
 to help making this an active project consider `pinging
-Peter <http://www.peterbe.com/contact>`__ and we can talk about putting
+Peter <https://www.peterbe.com/contact>`__ and we can talk about putting
 up logos and links to your company.
 
 Python versions
diff --git a/premailer/premailer.py b/premailer/premailer.py
index 04d6eb5..ad91102 100644
--- a/premailer/premailer.py
+++ b/premailer/premailer.py
@@ -624,7 +624,10 @@ class Premailer(object):
             # media rule
             else:
                 for rule in item.cssRules:
-                    if isinstance(rule, cssutils.css.csscomment.CSSComment):
+                    if isinstance(rule, (
+                        cssutils.css.csscomment.CSSComment,
+                        cssutils.css.cssunknownrule.CSSUnknownRule
+                    )):
                         continue
                     for key in rule.style.keys():
                         rule.style[key] = (


Test patch:
diff --git a/premailer/tests/test_premailer.py b/premailer/tests/test_premailer.py
index 4155370..196d419 100644
--- a/premailer/tests/test_premailer.py
+++ b/premailer/tests/test_premailer.py
@@ -2215,6 +2215,29 @@ ent:"" !important;display:block !important}
         result_html = p.transform()
         ok_('/* comment */' in result_html)
 
+    def test_unknown_in_media_queries(self):
+        """CSS unknown rule inside a media query block should not be a problem
+        """
+        html = """<!doctype html>
+        <html lang="en">
+        <head>
+            <meta charset="UTF-8">
+            <title>Document</title>
+            <style>
+                @media screen {
+                    @unknownrule {
+                        /* unknown rule */
+                    }
+                }
+            </style>
+        </head>
+        <body></body>
+        </html>"""
+
+        p = Premailer(html, disable_validation=True)
+        result_html = p.transform()
+        ok_('/* unknown rule */' in result_html)
+
     def test_fontface_selectors_with_no_selectortext(self):
         """
         @font-face selectors are weird.