phphtmlpurifier

How to exempt a tag from HTMLpurifier?


I'm currently running some code with HTMLPurifier. However, there's one tag type we want left as-is.

I've looked through questions and docs, and haven't been able to find a clear answer... how do I exempt a specific tag from HTMLPurifier?

I'm looking for something along the lines of...

$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Allowed', $HTML_Allowed);
$config->set('ExemptHTML', "img,form,pre");

Solution

  • It's a bit hacky, but it turns out you can write a class that extends the particular schema with a _tagname on the parent and register it, and create a new validator tailored to your passthrough (ended up switching to targeting an attribute for more atomic control, but it should work for either, hypothetically).

    if (!class_exists("HTMLPurifier_[something]scheme_[mytag]")){
        class HTMLPurifier_[something]Scheme_[mytag] extends HTMLPurifier_[something]Scheme{
            function __construct()
            {
                $this->[required_variable_for_passing_its_category] = true;
            }
            public function doValidate(&$uri, $config, $context) {
                return true;
                // Or, alternatively, you could actually do some validation here.
            }
        }
        HTMLPurifier_[something]SchemeRegistry::instance()->register(new HTMLPurifier_[something]Scheme_[mytag](), $config);
    }