HTMLPurifier:自动br [英] HTMLPurifier: auto br
问题描述
如何获得:
< p>第一个< br> p< / p>
< p>第二p< / p>
from:
< p>第一个
p< / p>
< p>第二p< / p>
使用HTMLPurifier?
如果你是这样的人,真正用HTML Purifier来解决这个问题的时候,你可能会写一个textnode转换来完成一个 nl2br
或 str_replace
通过编写一个扩展 HTMLPurifier_AttrDef_Text
的类。伪代码:
class HTMLPurifier_AttrDef_Text_Linebreaks扩展了HTMLPurifier_AttrDef_Text
{
public function validate($ string,$ config,$上下文){
$ string = parent :: validate($ string,$ config,$ context);
返回str_replace(\ n,< br />,$ string);
然后你会将你的类注入到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()
);
警告:
- Text AttrDef可能是
htmlspecialchars()
-ed(它确实是理智的)。那么你用这种方法运气不好。您必须找到注入< br />
节点的方法。 - 这可能太迟了在这个过程中给你你想要的控制 - 改变文本节点的模板(可以这么说),但是实际的HTML定义可能已经开始利用
HTMLPurifier_AttrDef_Text
实例。
看看是否可以帮助你?
在HTML Purifier论坛上也有一个线程(你开始了吗?我不会感到惊讶) - nl2br,auto< br />插入
。如果您找到解决方案,您可以在此发布答案。
How i can get:
<p>first<br>p</p>
<p>second p</p>
from:
<p>first
p</p>
<p>second p</p>
using HTMLPurifier?
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:
- 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. - 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.
这篇关于HTMLPurifier:自动br的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!