Spaces are removed from CSS definitions

mindgame - April 29, 2009 - 16:50
Project:Mime Mail
Version:6.x-1.0-alpha1
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:reviewed & tested by the community
Description

I found that all style definitions from my mail.css containing spaces (e.g. "border: 0 none;") are not "compressed" correctly into my HTML mails.

The problem, however, is not in the Mime Mail CSS Compressor module, but in the last line of function template_preprocess_mimemail_message() in mimemail.module:

$variables['css'] = str_replace(' ','', str_replace("\n", '', $css));

It converts "border: 0 none;" to "border:0none;" which of course cannot be interpreted by the mail client.

As a quick fix is changed the line to:

$variables['css'] = str_replace("\n", '', $css);

#1

gregarios - June 8, 2009 - 20:33

My issue is a duplicate of this one. Sorry. Subscribing. Here was my writeup:
http://drupal.org/node/475830

#2

gregarios - June 8, 2009 - 20:35

I might also add, that it looks like the code your fix is applied to is meant to strip out multiple white spaces, but actually strips out all whitespaces. Maybe the code should be more like:
$variables['css'] = str_replace('  ',' ', str_replace("\n", '', $css));?

#3

bright8 - June 21, 2009 - 11:05

I was having the same issue. I have made the modification suggested by gregarios and now the css in my newsletters looks correct and is interpreted correctly by the mail clients I've tried so far.
Thanks for pinpointing the fix mindgame and gregarios.

#4

gregarios - June 21, 2009 - 19:50
Priority:normal» critical

I'm bumping this to critical, then... since this is a show-stopper bug and a fix is in this post.

#5

pillarsdotnet - August 14, 2009 - 11:02
Status:active» needs review

Changing to "needs review" since a patch is attached.

Also see #443964: media=<print|screen|all> selector stripped from mime_mail embedded css

AttachmentSize
mimemail-mail_alter-fixes.patch 2.99 KB

#6

cameronp - August 19, 2009 - 05:20
Status:needs review» needs work

me fail sorry about that

#7

omo - September 2, 2009 - 18:03
Status:needs work» needs review

Thanks for the hint.

I didn't check the later patch, but the very first solution seams just fine for mime mail's core module. (Because spaces are part of the CSS selector syntax, removing any of them without semantical parsing seems fatal in any case.)

AttachmentSize
mimemail.module.patch 362 bytes

#8

gregarios - September 2, 2009 - 19:14

Which solution is the "very first solution" you are referring to? "Multiple spaces" are not part of CSS in any way.

#9

omo - September 7, 2009 - 17:24

I'm referring to the one in mindgame's original post: Multiple spaces are not, but single spaces are.

#10

miro_dietiker - September 21, 2009 - 14:30

I opened a different case, namely: #581142: truncated mail due to css

My original issue was not css space removement, but much more mail truncation.

Please please read my post and understand that removing any newline is NO solution for SMTP due to RFC limitation of 998 bytes line length.

So finally i see no gain in removing a few bytes such as newlines and spaces, except introducing an incredible amount of potential problems. Instead mimemail should implement line length checks and add them manually or do base64 encoding...

Please simply switch this to the full unmodified css content.

AttachmentSize
mimemail_css.patch 745 bytes

#11

scottrigby - October 4, 2009 - 20:54

+1 for #10

#12

gregarios - October 4, 2009 - 23:24

Please simply switch this to the full unmodified css content.

I'm ALL for that! Please, Allie Micka, or one of the other maintainers...

#13

omo - October 6, 2009 - 11:33

Good point, Miro.

#14

omo - October 6, 2009 - 11:34

Here's the patch for the latest dev release (2009-Jun-08):

AttachmentSize
mimemail.module.patch 242 bytes

#15

gregarios - October 6, 2009 - 15:14

Here's the patch for the latest dev release (2009-Jun-08):

I think having it remove multiple spaces (leaving all as single spaces) is still a good thing. Leave line-endings though. can you update the patch to reflect what I wrote in the space-removal part of #2?

#16

omo - October 8, 2009 - 18:10

@gregarios. Here you go. I can't test the patch myself for now. Could you please verify that this works? Thanks, Moritz

AttachmentSize
mimemail.module.patch 270 bytes

#17

gregarios - October 11, 2009 - 06:08

The patch in #16 doesn't seem to do anything.

In testing, I tried a few mods myself to see if I could get it to work right. The string you are modifying doesn't seem to do anything at all. Even if I change it to be like the patch in #14, there is no change... all the CSS is still compressed.

I believe the fault lies in the mimemail_compress modules. I've tracked down another somewhat related issue to this, where certain CSS containing links gets corrupted as well:

background-image: url(http://domain.com/image.jpg);
turns into:
background-image:url(http;

Somehow, in the mimemail_compress.inc file, there is code that is reading all colons as CSS seperators, and using the colon in a URL incorrectly, causing it to be corrupted.

I believe all the substitutions with whitespace are also done in the mimemail_compress.inc file as well... could you take a look in it? I tried to spot the defects, but could not due to my lack of enough PHP experience.

UPDATE I believe the string list($key,$value) = explode(':',$def); found in the mimemail_compress.inc file may be to blame for all CSS URL corruption.

#18

miro_dietiker - October 11, 2009 - 10:05

gregarios:
This issue is NOT about mimemail_compress. It's about wrong behaviour of plain mimemail.module - without mimemail_compress activated.

After having this basic functionality fixed you might think about enabling mimemail_compress (and also look into depth of much other strange things it does ;-) ) .. You don't need mimemail_compress. I strongly recommend you to add an own mail.css file and get rid of other css definitions that way.

#19

gregarios - October 12, 2009 - 04:49
Status:needs review» reviewed & tested by the community

@miro_dietiker:
My mistake... the last patch (in #16) does work.

#20

omo - October 14, 2009 - 14:25

*whew* Thanks for testing @gregarios

#21

gregarios - October 14, 2009 - 20:42

*whew* Thanks for testing @gregarios

As a matter of fact I disabled the CSS Compress module at your suggestion and went with a mail.css file. WAY happier with the results. :-) (Now I wish mail.css could be named differently, but oh well.)

#22

gregarios - October 24, 2009 - 07:30
Version:6.x-1.x-dev» 6.x-1.0-alpha1

Alas, this has not been fixed in the newest version... and the patch in #16 would appear to not work on this version as well.

The patch needs to be rewritten to work on the same BUG CODE which has been moved to the mimemail.theme.inc file now. Disappointing.

#23

gregarios - October 24, 2009 - 07:31
Status:reviewed & tested by the community» active

#24

omo - October 24, 2009 - 19:56
Status:active» needs review

Here we go @gregarios. For future reference: http://drupal.org/patch/create ;)

AttachmentSize
mimemail.theme_.inc_.patch 286 bytes

#25

gregarios - October 24, 2009 - 23:06
Status:needs review» reviewed & tested by the community

Thanks omo for the patching help. Your patch works great! Now, how can we get this into alpha2? :-)

#26

Sutharsan - October 26, 2009 - 14:12

Patch works as advertised. Let's move this one in.

#27

ericbellot - November 8, 2009 - 16:13

For a CSS code cleaning, this line it's better :

<?php
$variables
['css'] = preg_replace('#\s+#s', ' ', preg_replace('#\s*([:;,\(\)])\s*#s', '$1',$css));
?>

- remove all space before and after CSS separator : ":", ";", "(", ")" and ","
- replace multiple spaces and similar (\n, \r, etc.) by one space

Patch with comment #28

#28

ericbellot - November 8, 2009 - 16:12

Patch for comment #27:

AttachmentSize
mimemail.theme_.inc_.patch 339 bytes

#29

miro_dietiker - November 16, 2009 - 11:19

I don't think we should do any complex css cleanups in here.
Just make it work as minimal as possible. Without need to check complex specific replacements to understand if it works now or if it still doesn't in some specific cases.

Specific advanced cleanups should be integrated the way as css Compressor module does.

 
 

Drupal is a registered trademark of Dries Buytaert.