Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Configuration Rules are not applied #21

Closed
tiagomalheiro opened this issue Feb 2, 2019 · 2 comments
Closed

Custom Configuration Rules are not applied #21

tiagomalheiro opened this issue Feb 2, 2019 · 2 comments
Labels

Comments

@tiagomalheiro
Copy link
Contributor

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

@stevebauman
Copy link
Owner

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!

@tiagomalheiro
Copy link
Contributor Author

thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants