【问题标题】:HTMLPurifier allow attributesHTMLPurifier 允许属性
【发布时间】:2012-05-09 11:24:45
【问题描述】:

我在让 HTMLPurifier 不过滤标签属性时遇到了麻烦,但直到现在都没有成功,我快疯了。

    $config = HTMLPurifier_Config::createDefault();
    $config->set('Core.Encoding', 'UTF-8');
    $config->set('Core.CollectErrors', true);
    $config->set('HTML.TidyLevel', 'medium');
    $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
    $config->set('URI.DisableExternalResources', false);

    $config->set('HTML.Allowed', 'table[border|width|style],tbody,tr,td,th,img[style|src|alt],span[style],p[style],ul,ol,li,strong,em,sup,sub');

    $PHTML = new HTMLPurifier($config);
    echo htmlspecialchars($PHTML->purify($html));

    // The input string:
    "Some <span style="text-decoration: underline;">cool text</span> <img src="http://someurl.com/images/logo.png" alt="" />.

    // The output string:
    "Some <span>cool text</span> <img src="%5C" alt="" />.

我想允许在 HTML.Allowed 选项中定义的指定元素的给定属性。

【问题讨论】:

    标签: attributes htmlpurifier


    【解决方案1】:

    关闭魔术引号。 (注意 %5C)

    【讨论】:

    • 我试了一下,但现在输出:Some cool text
    【解决方案2】:

    有点晚的建议,但我遇到了与 HTMLPurifier 剥离 style 属性类似的问题,即使它们是在 HTML.Allowed 设置中配置的。

    我找到的解决方案要求您还配置 CSS.AllowedProperties,看起来有点像这样:

    $config->set('CSS.AllowedProperties', 'text-align,text-decoration,width,height');
    

    将其与 HTML.Allowed 结合使用:

    $config->set('HTML.Allowed', 'img[src|alt|style],span[style]');
    

    我希望其他人觉得这很有用,you can read more about CSS.AllowedProperties here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多