HTMLPurifier: auto br

試著忘記壹切 提交于 2019-12-11 06:37:39

问题


How i can get:

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

from:

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

using HTMLPurifier?


回答1:


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

If you're really set on solving this with HTML Purifier, you might be able to write a textnode transformation that does an 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'd inject your class into HTML Purifier:

$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()
);

Caveats:

  1. The Text AttrDef may be htmlspecialchars()-ed (it'd be sane, really). Then you're out of luck with that approach. You'd have to find a way to inject a <br /> node, instead.
  2. That might be too late in the process to give you the control you want - that changes the text nodes 'template' (so to speak), but the actual HTML definition might already happily be making use of a HTMLPurifier_AttrDef_Text instance.

See if that helps you at all?

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



来源:https://stackoverflow.com/questions/4839489/htmlpurifier-auto-br

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!