htmlpurifier icon indicating copy to clipboard operation
htmlpurifier copied to clipboard

Conditional comments around CSS style blocks are gone

Open PHPGangsta opened this issue 8 years ago • 2 comments

When using Filter.ExtractStyleBlocks to extract CSS style blocks, HTML conditional comments are removed, which leads to wrong looking HTML. I have this example code:

<head>
    <style>
        div {display: none;}
    </style>
    <!--[if IE 6]>
    <style>    
        div {display: block;}
    </style>
    <![endif]-->
</head>
<body>
<div>hi</div>
</body>
</html>```

In this case, the link should only be shown in IE 6.

The output of HTML is the following, which is correct:
<div>hi</div>

The corresponding CSS style blocks look like this:
```  array(2) {
    [0] =>
    string(18) "div {
display:none
}"
    [1] =>
    string(19) "div {
display:block
}"
  }

The information about the IE6 is gone, if I output both style blocks on the website, the div is always shown, because the conditional comment is gone.

At the moment I don't have a good idea how to fix this, I hope someone of you has an idea. The information is important and should not be lost.

PHPGangsta avatar Aug 03 '17 17:08 PHPGangsta

Hmm, the problem is likely because we regex out style blocks, so comment parsing is not in effect:

$html = preg_replace_callback('#<style(?:\s.*)?>(.*)<\/style>#isU', array($this, 'styleCallback'), $html);

in library/HTMLPurifier/Filter/ExtractStyleBlocks.php. If you swapped this out for something more clever that would probably solve the problem.

ezyang avatar Aug 05 '17 14:08 ezyang

Facing the same problem. Is there something that can be specified in HTML.AllowedComments to not filter this out?

Whip avatar Jul 20 '21 09:07 Whip