Search code examples
phphtmlpurifier

Whitelist element with class of, using htmlpurifier


I want to only allow the span element only when it has a certain class in htmlpurifier

does anyone know how to do this, right now I have

  $config->set('HTML.Allowed','a[href],p,ol,li,ul,img[src],blockquote,em,span[class]');
  $config->set('Attr.AllowedClasses',"allowed");

but that allows all spans and only allows class allowed I like that it only allows the "allowed" class but I only want it to allow span when the value of its class is "allowed"

thanks


Solution

  • Ok so based on Ambush-comander's suggestion I was able to remove all spans that did not have a specific class the idea is that if the class it required then it the element doesn't have that class the element will be removed.

    I did some research and found htmlpurifier customize page which explains how to add an attribute following their instructions i only need an additonal four lines of code so here is what how I did it

     // more configuration stuff up here
        $config->set('HTML.DefinitionID', 'enduser-customize.html editor');
        $config->set('HTML.DefinitionRev', 1);
        $def = $config->getHTMLDefinition(true);
        $def->addAttribute('span', 'class*', new HTMLPurifier_AttrDef_Enum(
          array('allowed')
        ));
     // purify down here
    

    the * in class makes the class requried and becuse we only allow the "allowed" class everything else gets striped. now, there is one caveats to doing it this way. if someone put that class in there span then it would be allowed in my case I'm not really using "allowed" I'm using something else that will be replaced by html purifier

    hth someone else

    and thanks to ambush and pinkgothic for all their help!