trsteel88 / TrsteelCkeditorBundle

Symfony2 bundle for easy integration of the CKEditor WYSIWYG
97 stars 59 forks source link

Allow $def->addAttribute() via config #98

Closed scottshipman closed 1 year ago

scottshipman commented 8 years ago

I had to write my own Data transformer in order to allow the use of iframe's with attribute: allowfullscreen because its not part of the out of the box HTMLPurifier attribute set.

My data transformer models the HTMLPurifierTransformer, with the method getPurifier as below:

protected function getPurifier()
  {
    if (null === $this->purifier) {

      // tweak the config
      $config = \HTMLPurifier_Config::createDefault();
      $config->set('HTML.DefinitionID', 'my custom html purifier');
      $config->set('HTML.DefinitionRev', 1);
      foreach($this->config as $key => $value){
        $config->set($key, $value);
      }
      if ($def = $config->maybeGetRawHTMLDefinition()) {
        // our custom add-ons will go here
        $def->addAttribute('iframe', 'allowfullscreen', 'Bool');

      }

      $this->purifier = new \HTMLPurifier($config);

    }
    return $this->purifier;
  }

...where $config is passed in from the service container by getting the HTML_purifier config parameter from config.yml (or whereever its stored). In my case it was based on my own app bundle's extension.

Maybe your bundle can be extended to include a process where there is a config tree that looks like:

trsteel_ckeditor:
       html_purifier:
        config:
            HTML.AllowedAttributes: 'tags,listed,here'
        addAttr:
             iframe: ['allowfullscreen', 'Bool']

or something similar so the HTMLpurifier can be extended to take advantage of all possible customizations?

erichard commented 8 years ago

I had quite the same use case this morning. I had to play with the config for allowing a specific data- attribute.

    /**
     * @return \HTMLPurifier
     */
    protected function getPurifier()
    {
        if (null === $this->purifier) {
            $this->purifier = new \HTMLPurifier($this->config);
            $def = $this->purifier->config->getHTMLDefinition(true);
            $def->info_global_attr['data-entity-file'] = new \HTMLPurifier_AttrDef_Text;
        }

        return $this->purifier;
    }