Repository: peterbe/premailer

Issue:
keep class attributes
the transform() function replaces the "class" attribute with the appropriate styling, but it also removes the class from the html elements. I need the styling added inline, but the classes to be left within their elements so that my @media (!important) styling can be inserted in after and still have classes to point to within the dom.

Im having a hard time changing your code to meet this needs, do you have any thoughts on how I could keep the classes within the html elements but still inject the inline styling?

Discussion:
graingert: I think it's such a trivial and unlikely to be used piece of code, that I'd rather just remove it and recommend people add their own:
```python
            # now we can delete all 'class' attributes
            for item in page.xpath('//@class'):
                parent = item.getparent()
                del parent.attrib['class']
```


peterbe: @graingert But I want the library to be as easy as possible. Also, as lazy as possible :)
graingert: It's your call, but imagine if this feature was not implemented, would it be worth adding?

I don't see a use-case for it.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/6493685/badge)](https://coveralls.io/builds/6493685)

Coverage decreased (-0.7%) to 99.299% when pulling **4e1762899aca476de755ada50c95ec02072e24db on keep-classes-by-default-fixes-33** into **cc18022e334d5336e48f75bd4e0a73c98cc5942a on master**.

peterbe: One usecase is that it's not hard to keep and it's nice to have it there
for people who needed it.

Actually, if you don't have media queries. If you have some really simple
CSS for your HTML emails, then having the `class` attributes still in means
the email gets unnecessarily larger.
I suggest we just keep the functionality around.

On Tue, Jun 7, 2016 at 8:45 AM, Thomas Grainger <notifications@github.com>
wrote:

> It's your call, but imagine if this feature was not implemented, would it
> be worth adding?
>
> I don't see a use-case for it.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/peterbe/premailer/pull/167#issuecomment-224269061>,
> or mute the thread
> <https://github.com/notifications/unsubscribe/AABoc_2Fg2_REhvGVNqOZLI7Z5RBUWkLks5qJWfjgaJpZM4Iv3_H>
> .
>



-- 
Peter Bengtsson
Mozilla Tools & Services

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

Coverage remained the same at 100.0% when pulling **6e9df1c5587d133851f2185c63cf79998ad679ec on keep-classes-by-default-fixes-33** into **cc18022e334d5336e48f75bd4e0a73c98cc5942a on master**.


Patch:
diff --git a/premailer/__init__.py b/premailer/__init__.py
index e831baa..570b55c 100644
--- a/premailer/__init__.py
+++ b/premailer/__init__.py
@@ -1,4 +1,4 @@
 from __future__ import absolute_import, unicode_literals
 from .premailer import Premailer, transform
 
-__version__ = '2.11.0'
+__version__ = '3.0.0'
diff --git a/premailer/premailer.py b/premailer/premailer.py
index de9839d..fc157ee 100644
--- a/premailer/premailer.py
+++ b/premailer/premailer.py
@@ -121,7 +121,7 @@ class Premailer(object):
                  exclude_pseudoclasses=True,
                  keep_style_tags=False,
                  include_star_selectors=False,
-                 remove_classes=True,
+                 remove_classes=False,
                  capitalize_float_margin=False,
                  strip_important=True,
                  external_styles=None,


Test patch:
diff --git a/premailer/tests/test_premailer.py b/premailer/tests/test_premailer.py
index 42213d8..5cdf6a7 100644
--- a/premailer/tests/test_premailer.py
+++ b/premailer/tests/test_premailer.py
@@ -170,6 +170,37 @@ class Tests(unittest.TestCase):
 
         compare_html(expect_html, result_html)
 
+    def test_remove_classes(self):
+        """test the simplest case"""
+
+        html = """<html>
+        <head>
+        <title>Title</title>
+        <style type="text/css">
+        .stuff {
+            color: red;
+        }
+        </style>
+        </head>
+        <body>
+        <p class="stuff"><strong>Yes!</strong></p>
+        </body>
+        </html>"""
+
+        expect_html = """<html>
+        <head>
+        <title>Title</title>
+        </head>
+        <body>
+        <p style="color:red"><strong>Yes!</strong></p>
+        </body>
+        </html>"""
+
+        p = Premailer(html, remove_classes=True)
+        result_html = p.transform()
+
+        compare_html(expect_html, result_html)
+
     def test_basic_html_shortcut_function(self):
         """test the plain transform function"""
         html = """<html>
@@ -1088,7 +1119,7 @@ b
         <head>
         </head>
         <body>
-        <div style="color:red"></div>
+        <div class="example" style="color:red"></div>
         </body>
         </html>"""
 
@@ -1118,7 +1149,7 @@ b
         <head>
         </head>
         <body>
-        <div style="color:green"></div>
+        <div class="example" style="color:green"></div>
         </body>
         </html>"""
 
@@ -1148,7 +1179,7 @@ b
         <head>
         </head>
         <body>
-        <div style="color:green"></div>
+        <div class="example" style="color:green"></div>
         </body>
         </html>"""
 
@@ -1178,7 +1209,7 @@ b
         <head>
         </head>
         <body>
-        <div id="identified" style="color:green"></div>
+        <div class="example" id="identified" style="color:green"></div>
         </body>
         </html>"""
 
@@ -1195,7 +1226,7 @@ b
             color: blue !important;
             font-size: 12px;
         }
-        #identified {
+        #id {
             color: green;
             font-size: 22px;
         }
@@ -1205,17 +1236,17 @@ b
         </style>
         </head>
         <body>
-        <div class="example makeblue" id="identified"></div>
+        <div class="example makeblue" id="id"></div>
         </body>
         </html>"""
 
         expect_html = """<html>
-        <head>
-        </head>
-        <body>
-        <div id="identified" style="font-size:22px; color:blue"></div>
-        </body>
-        </html>"""
+<head>
+</head>
+<body>
+<div class="example makeblue" id="id" style="font-size:22px; color:blue"></div>
+</body>
+</html>"""
 
         p = Premailer(html)
         result_html = p.transform()
@@ -1285,7 +1316,7 @@ ration:none">Yes!</strong></p>
         <title>Title</title>
         </head>
         <body>
-        <h1 style="color:green">Hi!</h1>
+        <h1 class="foo" style="color:green">Hi!</h1>
         </body>
         </html>"""
 
@@ -2395,7 +2426,7 @@ sheet" type="text/css">
         <head>
         </head>
         <body>
-        <div style="color:green; font-size:10px"></div>
+        <div class="color example" style="color:green; font-size:10px"></div>
         </body>
         </html>"""
 
@@ -2453,22 +2484,22 @@ sheet" type="text/css">
         </head>
         <body>
         <p><img src="/images/left.jpg" style="float: left"> text
-           <img src="/images/right.png" class="floatright"> text
+           <img src="/r.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>"""
+<head>
+<title>Title</title>
+</head>
+<body>
+<p><img src="/images/left.jpg" style="float: left" align="left"> text
+   <img src="/r.png" class="floatright" 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()
@@ -2498,7 +2529,8 @@ sheet" type="text/css">
         <head>
         </head>
         <body>
-        <div style="color:green"><span></span></div>
+        <div class="color" style="color:green"><span class="nocolor"></span>
+        </div>
         </body>
         </html>"""