tijsverkoyen / CssToInlineStyles

CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very usefull when you're sending emails.
BSD 3-Clause "New" or "Revised" License
5.8k stars 187 forks source link

Disable inlinling of certail style tags #229

Open e1himself opened 2 years ago

e1himself commented 2 years ago

Hi!

I'm working on a problem with certain versions of Outlook email client, and came to a solution where I need to apply styles to every email client, except Microsoft Outlook.

There's a conditional comment that can do that:

<!--[if !mso]><!-->
<style>...</style>
<!--<![endif]-->

But the problem is that it's being inlined, as it's a valid non-commented <style> attribute.

I've been thinking that having a way to disable inlining of styles from a specific style attribute could solve this. For example, the regular expression in Processor::getCssFromStyleTags() could ignore style tags that have do-not-inline attribute:

https://github.com/tijsverkoyen/CssToInlineStyles/blob/master/src/Css/Processor.php#L38-L39

        $htmlNoComments = preg_replace('|<!--.*?-->|s', '', $html);
+       $htmlNoComments = preg_replace('|<style(?:.*\sdo-not-inline\s?.*)?>(.*)</style>|s', '', $htmlNoComments);
        preg_match_all('|<style(?:\s.*)?>(.*)</style>|isU', $htmlNoComments, $matches);

I can file a PR if you agree it's a good addition.

Anyway, thank you for your great work! This package is helping us a lot with rendering emails :+1:

dweidner commented 1 year ago

I was looking for the same functionality.