Question

How do I allow borderRadius with htmlpurifier?

I found this but it doesn't seem to work with current version of htmlpurifier, perhaps they changed the way you add your own css?

http://htmlpurifier.org/phorum/read.php?2,6154,6154

  $config = HTMLPurifier_Config::createDefault();

  // add some custom CSS3 properties                                                                                                                                              
  $css_definition = $config->getDefinition('CSS');

  $border_radius =
    $info['border-top-left-radius'] =
    $info['border-top-right-radius'] =
    $info['border-bottom-left-radius'] =
    $info['border-bottom-right-radius'] =
    new HTMLPurifier_AttrDef_CSS_Composite(array(
                                             new HTMLPurifier_AttrDef_CSS_Length('0'),
                                             new HTMLPurifier_AttrDef_CSS_Percentage(true)
                                             ));

  $info['border-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius);

  // wrap all new attr-defs with decorator that handles !important                                                                                                                
  $allow_important = $config->get('CSS.AllowImportant');
  foreach ($info as $k => $v) {
    $css_definition->info[$k] = new HTMLPurifier_AttrDef_CSS_ImportantDecorator($v, $allow_important);
  }

  $html_purifier = new HTMLPurifier($config);
Était-ce utile?

La solution

I forked the original repo and added functionality for border radius in the purifying function, code is found here

https://github.com/msvensson82/htmlpurifier

I basically just added this to the CSSDefinition.php file, if you wish to amend yours instead of getting my repo.

// border-radius
$border_radius =
$this->info['border-top-left-radius'] =
$this->info['border-top-right-radius'] =
$this->info['border-bottom-left-radius'] =
$this->info['border-bottom-right-radius'] = new HTMLPurifier_AttrDef_CSS_Composite(array(
 new HTMLPurifier_AttrDef_CSS_Length('0'),
 new HTMLPurifier_AttrDef_CSS_Percentage(true)
));
$this->info['border-radius'] = new HTMLPurifier_AttrDef_CSS_Multiple($border_radius);

Autres conseils

As of 2016 this is achieveable by turning on the CSS.Proprietary option:

use HTMLPurifier;
use HTMLPurifier_Config;

$input = '<div style="border: 2px solid; border-radius: 10px;">Hello world!</div>';

$config = HTMLPurifier_Config::createDefault();
// Enable "proprietary" css properties to allow use of `border-radius`
$config->set('CSS.Proprietary', true);

$purifier = new HTMLPurifier($config);

echo $hp_purifier->purify($input);
// => <div style="border:2px solid;border-radius:10px;">Hello world!</div>

This allows you to use border-radius, as well as the individual properties border-top-left-radius, border-top-right-radius, border-bottom-right-radius, and border-bottom-left-radius

Turning on proprietary css properties also enables a small handful of other properties, but besides the filter property (which apparently only supports the opacity filter at this time), I don't think I've ever seen any of them in use before.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top