Custom Configuration Rules are not applied
Closed this issue · 2 comments
I am using Purify in a laravel project and need to allow
elements (for tinymyce wysiwyg editor).Have followed the readme Custom Configuration Rules and installed a PurifySetupProvider with the new element. Nevertheless, Purify strips out the figure elements.
Here is the ServideProvider:
<?php
namespace App\Providers;
use HTMLPurifier_HTMLDefinition;
use Stevebauman\Purify\Facades\Purify;
use Illuminate\Support\ServiceProvider;
class PurifySetupProvider extends ServiceProvider
{
const DEFINITION_ID = 'tinymce-editor';
const DEFINITION_REV = 1;
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
/** @var \HTMLPurifier $purifier */
$purifier = Purify::getPurifier();
/** @var \HTMLPurifier_Config $config */
$config = $purifier->config;
$config->set('HTML.DefinitionID', static::DEFINITION_ID);
$config->set('HTML.DefinitionRev', static::DEFINITION_REV);
$config->set('URI.AllowedSchemes', ['data' => true]); // allow data URIs
if ($def = $config->maybeGetRawHTMLDefinition()) {
$this->setupDefinitions($def);
}
$purifier->config = $config;
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Adds elements and attributes to the HTML purifier
* definition required by the tinyMCE editor.
*
* @param HTMLPurifier_HTMLDefinition $def
*/
protected function setupDefinitions(HTMLPurifier_HTMLDefinition $def)
{
$def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
$def->addAttribute('figure', 'class', 'Text');
$def->addElement('figcaption', 'Inline', 'Flow', 'Common');
$def->addAttribute('figcaption', 'class', 'Text');
}
}
and my test:
<?php
namespace Tests\Unit;
use Tests\TestCase;
class PurifyTest extends TestCase
{
/** @test */
function it_allows_figures()
{
$input = '<figure><figcaption>Hello fig</figcaption></figure>';
$cleaned = \Purify::clean($input);
$this->assertEquals( '<figure><figcaption>Hello fig</figcaption></figure>', $cleaned);
}
}
The workaround to make it work was to update the package service provider register method to bind a singleton, instead of an usual bind:
/**
* Register the service provider.
*/
public function register()
{
$this->publishes([
__DIR__.'/Config/config.php' => config_path('purify.php'),
], 'config');
$this->app->singleton('purify', function ($app) {
return new Purify();
});
}
What am I missing in setting up the Custom Configuration rules? What are the implications of using the singleton on the caching?
thanks
Hi @tiagomalheiro, apologies for the late response on this. I've released a new version with your PR included. Closing as this should resolve your issue.
Let me know if you encounter anything else, thanks!
thanks,