HTMLPurifier: auto br

How can I get:

<p>first<br>p</p> <p>second p</p> 

from

 <p>first p</p> <p>second p</p> 

using HTMLPurifier?

+1
source share
1 answer

I'm not sure about the specifics, but since this question has no answers, see if these pointers help:

If you really want to solve this problem with HTML Purifier, you can write a text code conversion that nl2br or str_replace by writing a class that extends HTMLPurifier_AttrDef_Text . Pseudocode:

 class HTMLPurifier_AttrDef_Text_Linebreaks extends HTMLPurifier_AttrDef_Text { public function validate($string, $config, $context) { $string = parent::validate($string, $config, $context); return str_replace("\n", "<br />", $string); } } 

Then you add your class to the HTML cleaner:

 $purifier->set('HTML.DefinitionID', 'aNameForYourDefinition'); $purifier->set('HTML.DefinitionRev', 1); //$purifier->set('Cache.DefinitionImpl', null); $htmlDef = $purifier->config->getHTMLDefinition(); $htmlDef->manager->attrTypes->set( 'Text', new HTMLPurifier_AttrDef_Text_Linebreaks() ); 

Cautions:

  • AttrDef text could be htmlspecialchars() -ed (that would be reasonable, really). Then you are out of luck with this approach. You will need to find a way to insert a <br /> node.
  • Perhaps it’s too late in this process to get the necessary control - which changes the pattern of the text nodes (so to speak), but the actual HTML definition may already be happy to use an instance of HTMLPurifier_AttrDef_Text .

See if this helps you at all?

There is also a thread on the HTML cleanup forum (did you start it? I wouldn't be surprised) - nl2br, auto <br /> insertion . If you find a solution, you can post your answer there.

+2
source

Source: https://habr.com/ru/post/1491362/


All Articles