Laravel Package Purifer不适用于iframe

时间:2014-09-03 16:39:08

标签: laravel-4 htmlpurifier

似乎即使在添加Config params以启用YouTube和Vimeo Iframe之后,我仍然会收到异常错误。 "元素' iframe'不支持[..]"

return array(
   'encoding' => 'UTF-8',
       'finalize' => true,
       'preload'  => false,
       'settings' => array(
       'default' => array(
              'HTML.Doctype'             => 'XHTML 1.0 Strict',
              'HTML.Allowed'             => 'blockquote,div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]',
              'CSS.AllowedProperties'    => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
              "HTML.SafeIframe"          => 'true',
              "URI.SafeIframeRegexp"     => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/|api.soundcloud.com/tracks/)%",
        'AutoFormat.AutoParagraph' => true,
        'AutoFormat.RemoveEmpty'   => true,
    ),
),

1 个答案:

答案 0 :(得分:5)

您的问题是您使用的是Doctype XHTML 1.0 Strict。 在HTML.SafeIframe的文档中,声明:

  

是否在不受信任的文档中允许iframe标记。此指令必须附带允许的iframe的白名单,例如%URI.SafeIframeRegexp,否则将导致致命错误。 此指令对严格的文档类型没有影响,因为iframe无效。

所以你应该使用Transitional代替。以下配置将正常工作:

return array(
    'encoding' => 'UTF-8',
    'finalize' => true,
    'preload'  => false,
    'settings' => array(
        'default' => array(
            'HTML.Doctype'             => 'XHTML 1.0 Transitional',
            'HTML.Allowed'             => 'iframe[src|width|height|class|frameborder],blockquote,div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]',
            'CSS.AllowedProperties'    => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
            "HTML.SafeIframe"          => true,
            "URI.SafeIframeRegexp"     => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/|api.soundcloud.com/tracks/)%",
            'AutoFormat.AutoParagraph' => true,
            'AutoFormat.RemoveEmpty'   => true,
        ),
    ),
);