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

Registered elements / transforms do not work on first invocation #13

Closed
leewillis77 opened this issue May 4, 2018 · 2 comments
Closed
Labels

Comments

@leewillis77
Copy link

A difficult one to explain, but I'm trying to set up Purify to "allow" <u> tags, and transform them into <span style="text-decoration: underline">.

After I've done what I think is required, it works, but not on the first call to Purify::clean() in a given PHP request cycle, e.g.

$ tinker
Psy Shell v0.9.3 (PHP 7.1.16 — cli) by Justin Hileman
>>> Purify::clean('<u>Foo</u>');
PHP Warning:  Element 'u' is not supported (for information on implementing this, see the support forums)  in /private/tmp/purify-test/vendor/ezyang/htmlpurifier/library/HTMLPurifier/HTMLDefinition.php on line 311
>>> Purify::clean('<u>Foo</u>');
=> "<span style="text-decoration:underline;">Foo</span>"

Whereas I'd expect:

$ tinker
Psy Shell v0.9.3 (PHP 7.1.16 — cli) by Justin Hileman
>>> Purify::clean('<u>Foo</u>');
=> "<span style="text-decoration:underline;">Foo</span>"
>>> Purify::clean('<u>Foo</u>');
=> "<span style="text-decoration:underline;">Foo</span>"

Steps to reproduce:

  • laravel new test-project
  • cd test-project
  • composer require stevebauman/purify
  • php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"
  • Edit config/purify.php and add "u" to HTML.Allowed
  • Create app/Providers/PurifyServiceProvider.php as per https://gist.github.com/leewillis77/6c1fe0ad5448b1fae6b5412a2ee02502
  • Edit config/app.php and add App\Providers\PurifyServiceProvider::class to the list of providers
  • Clear the purify cache (rm -fr storage/purify/)
  • Try Purify::clean('<u>Foo</u>'); in a tinker session

[Sidenote, I'd be happy if I could get it just to allow as an alternative, but the same issue happens with that]

@simonmsara
Copy link

simonmsara commented May 28, 2020

A bit late but I thought I'd add my solution.

Add the element to the definition

if ($def = $config->maybeGetRawHTMLDefinition()) {
    $def->addElement('u', 'Inline', 'Inline', 'Common');
    $def->info_tag_transform['u'] = new HTMLPurifier_TagTransform_Simple('span', 'text-decoration: underline;');
}

@karlshea
Copy link

karlshea commented Jan 2, 2021

I really had a hard time getting this to work, but I finally tracked it down. The issue is the "tidy" functionality. <u> (and in my case, <s>) is being "fixed" into a styled span, even if you're trying to manually allow the tag (link to source).

Using the example service provider, this is what's in my setupDefinitions:

$underline = $def->addElement('u', 'Inline', 'Inline', 'Common');
$underline->formatting = true;

$strike = $def->addElement('s', 'Inline', 'Inline', 'Common');
$strike->formatting = true;

And this is the key step:

config/purify.php


<?php

return [
    ...

    'HTML.TidyLevel' => 'none',

    ...
];

If you don't disable Tidy, those tags will never work, since they're getting cleaned regardless of what your definition is.

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

4 participants