• File: Figure.php
  • Full Path: /var/www/crm_set/application/vendor/xemlock/htmlpurifier-html5/library/HTMLPurifier/ChildDef/HTML5/Figure.php
  • Date Modified: 09/21/2026 4:20 AM
  • File size: 1.95 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

class HTMLPurifier_ChildDef_HTML5_Figure extends HTMLPurifier_ChildDef
{
    public $type = 'figure';

    public $elements = array(
        'figcaption' => true,
    );

    protected $allowedElements;

    /**
     * @param HTMLPurifier_Config $config
     * @return array
     */
    public function getAllowedElements($config)
    {
        if (null === $this->allowedElements) {
            // Add Flow content to allowed elements to prevent MakeWellFormed
            // strategy moving them outside 'figure' element
            $def = $config->getHTMLDefinition();

            $this->allowedElements = array_merge(
                $def->info_content_sets['Flow'],
                $this->elements
            );
        }
        return $this->allowedElements;
    }

    /**
     * @param array $children
     * @param HTMLPurifier_Config $config
     * @param HTMLPurifier_Context $context
     * @return array|bool
     */
    public function validateChildren($children, $config, $context)
    {
        $hasFigcaption = false;
        $figcaptionPos = -1;

        $result = array();

        // Content model:
        // Either: one figcaption element followed by flow content.
        // Or: flow content followed by one figcaption element.
        // Or: flow content.

        // Scan through children, accept at most one figcaption.
        foreach ($children as $node) {
            if ($node->name === 'figcaption') {
                if (!$hasFigcaption) {
                    $hasFigcaption = true;
                    $figcaptionPos = count($result);
                    $result[] = $node;
                }
                continue;
            }

            // Figcaption must be a first or last child of a figure element.
            // If it's not first, then we ignore all siblings that come after.
            if ($hasFigcaption && $figcaptionPos > 0) {
                break;
            }

            $result[] = $node;
        }

        return $result;
    }
}