使用 htmlpurifier 将具有 of 类的元素列入白名单
我只想仅当 span 元素在 htmlpurifier 中具有某个类时才允许
它,有人知道如何执行此操作吗,现在我有,
$config->set('HTML.Allowed','a[href],p,ol,li,ul,img[src],blockquote,em,span[class]');
$config->set('Attr.AllowedClasses',"allowed");
但允许所有 span 并且只允许允许的类我喜欢它只允许“允许”类但我只希望它在其类的值“允许”时允许跨度,
谢谢
I want to only allow the span element only when it has a certain class in htmlpurifier
does anyone know how to do this, right now I have
$config->set('HTML.Allowed','a[href],p,ol,li,ul,img[src],blockquote,em,span[class]');
$config->set('Attr.AllowedClasses',"allowed");
but that allows all spans and only allows class allowed I like that it only allows the "allowed" class but I only want it to allow span when the value of its class is "allowed"
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
临时解决方案:重新定义span中所需的类,并设置它,使其具有N个可能的值。如果标签不存在,则进行必要的创建将导致标签被删除。
Ad hoc solution: redefine the class in span to be required, and set it so it has N possible values. The making it required will cause the tag to be removed if it doesn't exist.
好的,根据Ambush-comander的建议,我能够删除所有没有特定类的跨度是,如果它需要的类,那么如果该元素没有该类,则该元素将被删除。
我做了一些研究,发现 htmlpurifier custom 页面解释了如何在其后面添加属性说明我只需要额外的四行代码,所以这就是我的做法,
* in class 使该类成为必需的,因为我们只允许“允许”的类,其他所有内容都会被条纹化。
现在,这样做有一个警告。如果有人将该类放入其中,那么在我的情况下,它是被允许的,我并没有真正使用“允许”,我正在使用其他东西,这些东西将被
其他人的
html净化器所取代,并且感谢伏击和pinkgothic的所有帮助!
Ok so based on Ambush-comander's suggestion I was able to remove all spans that did not have a specific class the idea is that if the class it required then it the element doesn't have that class the element will be removed.
I did some research and found htmlpurifier customize page which explains how to add an attribute following their instructions i only need an additonal four lines of code so here is what how I did it
the * in class makes the class requried and becuse we only allow the "allowed" class everything else gets striped.
now, there is one caveats to doing it this way. if someone put that class in there span then it would be allowed in my case I'm not really using "allowed" I'm using something else that will be replaced by html purifier
hth someone else
and thanks to ambush and pinkgothic for all their help!
您在我的类似问题。我仍然没有解决方案,因为注射器/纯化顺序在我的情况下至关重要,但注射器解决方案应该为您工作,因为您不依赖于“预处理” ”,可以这么说。
据我所知,您有
两个像样的三个彻底的四个选项:如果您不这样做,您可以使用我问题中的属性解决方案来空白所有属性不必介意 HTML 中残留的
和
标记。如果您确实介意它们,您可以将该解决方案与 空标签剥离结合起来注入器,尽管执行顺序非常非常有可能再次使您瘫痪。 (所以我想。如果不是 - 太棒了,你有答案!:) )
你可以使用注射器。它们是 HTML Purifier 相当广泛和详细的功能,因此我无法想出一个可以立即为您解决问题的示例。但!您可能想查看 HTML Purifier 论坛上的线程,其中注入器“Linkify” ' 被创建了,这是对这个主题的相当彻底的理解,更不用说我在 #1 中提到的注入器也可以帮助您弄清楚它们。
出于我的目的,我现在正在调查的是 PHP 的内置 DOM 方法。如果 #1 和 #2 由于某种原因让您失败,那么这是一个可靠的替代方案,但当然就资源而言成本高昂。
正则表达式。我实际上只是将其作为最后的手段提及,这甚至不是一个严肃的建议。在已经纯化的 HTML 上使用正则表达式可能相当安全,但这似乎浪费了一个好的 HTML 解析器。 (但是,无可否认,#3 也是如此。)
祝你好运。
You left a comment over at my similar question. I still don't have a solution, due to the injector/purification order which in my case is pivotal, but the injector solution should work for you, since you don't depend on 'pre-processing', so to speak.
As far as I can see, you have
two decentthree thoroughfour options:You can use the attribute solution in my question to blank all attributes if you don't mind left-over
<span>
and</span>
tags in your HTML. If you do mind them, you could combine that solution with an empty-tag-stripping injector, though then order of execution is very, very likely to cripple you anew. (So I imagine. If not - superb, you have your answer! :) )You can use injectors. They're a fairly extensive and detailed feature of HTML Purifier, so I can't conjure up an example that'll fix things for you out of the box. But! You might want to look at the thread on the HTML Purifier forum where the injector 'Linkify' was created, that's a fairly thorough take on the subject, not to mention the injector I mentioned in #1 might help you figure them out, too.
Something I'm investigating right now for my purposes are PHP's built-in DOM methods. This is a solid alternative if #1 and #2 fail you for some reason, but of course costly as far as resources go.
Regular expressions. I'm really only mentioning this as a last resort and it's not even a serious suggestion. It'd probably be reasonably safe to use a regex on already purified HTML, but it seems like such a waste of a good HTML parser. (But then, admittedly, so does #3.)
Good luck.
在文档中没有任何关于配置属性值的内容。
但是,如果您仅使用
class
一次,我认为如果您按照您所写的那样进行操作,就不会有任何问题。 ;)In docummentation there is nothing about configuring values of attributes.
However if you use
class
only once I think there will not be any problem if you will do as you wrote. ;)