stevebauman / purify

A Laravel wrapper for HTMLPurifier by ezyang
MIT License
447 stars 35 forks source link

New definition of Trix editor #77

Closed mydansun closed 1 year ago

mydansun commented 1 year ago

Hi,

I tried https://github.com/stevebauman/purify/issues/7 and it did'n work well. The following definition should help anyone who whats to work with Trix editor :)

use HTMLPurifier_HTMLDefinition;
use Stevebauman\Purify\Definitions\Definition;

class TrixPurifierDefinition implements Definition
{
    /**
     * Apply rules to the HTML Purifier definition.
     *
     *
     * @return void
     */
    public static function apply(HTMLPurifier_HTMLDefinition $definition)
    {
        $definition->addElement('figure', 'Inline', 'Inline', 'Common');
        $definition->addAttribute('figure', 'class', 'Class');
        $definition->addAttribute('figure', 'data-trix-attachment', 'Text');
        $definition->addAttribute('figure', 'data-trix-attributes', 'Text');

        $definition->addElement('figcaption', 'Inline', 'Inline', 'Common');
        $definition->addAttribute('figcaption', 'class', 'Class');
        $definition->addAttribute('figcaption', 'data-trix-placeholder', 'Text');

        $definition->addAttribute('a', 'rel', 'Text');
        $definition->addAttribute('a', 'tabindex', 'Text');
        $definition->addAttribute('a', 'contenteditable', 'Enum#true,false');
        $definition->addAttribute('a', 'data-trix-attachment', 'Text');
        $definition->addAttribute('a', 'data-trix-content-type', 'Text');
        $definition->addAttribute('a', 'data-trix-id', 'Number');

        $definition->addElement('span', 'Block', 'Flow', 'Common');
        $definition->addAttribute('span', 'data-trix-cursor-target', 'Enum#right,left');
        $definition->addAttribute('span', 'data-trix-serialize', 'Enum#true,false');

        $definition->addAttribute('img', 'data-trix-mutable', 'Enum#true,false');
        $definition->addAttribute('img', 'data-trix-store-key', 'Text');
    }
}
stevebauman commented 1 year ago

Apologies for the late reply on this @mydansun, thanks for posting your solution!

I'll update the readme with your example here 👍

stevebauman commented 1 year ago

Added! 👍 I've also added you as a credit source with this issue linked.