Search code examples
phphtmlfontswysiwyghtmlpurifier

How can i filter specific font faces with HTMLPurifier?


-

in my current project, I have got a HTML-WYSIWYG Editor. Users can use different Webfonts to design their texts.

The Editor I am using is NiceEdit. It generates code like this:

        <font face="comic sans ms">Comic Sans</font><br>
        <font face="georgia">Georgia</font><br>
        <font face="helvetica">Helvetica</font><br>

I want to filter the fonts and only want to allow specific fonts viaHTMLPurifier.

so my Idea was to customize the config like this:

$config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
$config->set('HTML.DefinitionRev', 1);
$config->set('Cache.DefinitionImpl', null); //TODO: remove this later!
$def = $config->getHTMLDefinition(true);
$def = $config->getHTMLDefinition(
$def->addAttribute('font', 'face', 'Text#impact, georgia');

$purifier = new HTMLPurifier($config);

So in this case I only want to allow the fonts "impact" and "georgia".

But it doesn't work this way, all fonts still go through and are displayed.

Would be great, if someone could help me out here!

Thank you!
Stefan


Solution

  • You'll have to override the logic for filtering font-family and font. I think this will be tricky to do correctly externally, so you might just edit AttrDef/CSS/FontFamily.php manually to apply your restrictions.