Search code examples
phpelementfigurehtmlpurifier

How to add elements figureand figcaption in htmlpurifier?


When i add image with params i have had ERROR

ErrorException: Element 'figcaption' is not supported (for information on implementing this, see the support forums)

my code

         'HTML.Allowed' => 'a[href],blockquote,br,del,em,figcaption,figure,h1,h2,h3,h4,h5,h6,img[title|alt|src],li,ol,p,pre,strong,ul',

 .//////////////////////////////////////////////////////////////

        $config = HTMLPurifier_Config::createDefault();

        $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');

        $config->set('HTML.DefinitionRev', 1);

        if ($def = $config->maybeGetRawHTMLDefinition()) {
            $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
            $def->addElement('figcaption', 'Block', 'Flow', 'Common');
        }

Solution

  • Your code looks good to me. The order you're doing things in might be causing things to fail - make sure your application isn't trying to purify() before you're adding the elements. This example worked for me:

    <?php
    
    require_once 'library/HTMLPurifier.auto.php';
    
    $dirty_html = '<figure>
        <img src="/media/examples/elephant-660-480.jpg"
             alt="Elephant at sunset">
        <figcaption>An elephant at sunset</figcaption>
    </figure>';
    
    $config = HTMLPurifier_Config::createDefault();
    $config->set('HTML.Allowed', 'a[href],blockquote,br,del,em,figcaption,figure,h1,h2,h3,h4,h5,h6,img[title|alt|src],li,ol,p,pre,strong,ul');
    $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
    $config->set('HTML.DefinitionRev', 1);
    if ($def = $config->maybeGetRawHTMLDefinition()) {
        $def->addElement('figcaption', 'Block', 'Flow', 'Common');
        $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
    }
    
    $purifier = new HTMLPurifier($config);
    
    $clean_html = $purifier->purify($dirty_html);
    
    echo $clean_html;
    

    This gave me <figure><img src="/media/examples/elephant-660-480.jpg" alt="Elephant at sunset" /><figcaption>An elephant at sunset</figcaption></figure>.

    Commenting out these lines:

    $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial');
    $config->set('HTML.DefinitionRev', 1);
    if ($def = $config->maybeGetRawHTMLDefinition()) {
        $def->addElement('figcaption', 'Block', 'Flow', 'Common');
        $def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
    }
    

    ...gave me the error that you cite in your question, suggesting that by the time you're calling purify(), the code hasn't been executed yet.

    If you tell us more about where in your application workflow you're trying to adjust the HTML definition, and where you're purifying, I might be able to help you troubleshoot the cause of your problem.