【问题标题】:HTMLPurifier allowing specific attribute valuesHTMLPurifier 允许特定的属性值
【发布时间】:2013-09-02 20:45:39
【问题描述】:

允许我的用户使用的文本框编辑器为他们提供了一系列不错的选项,使他们的描述看起来独一无二。其中一个选项是缩进段落的能力,即以下 HTML:

<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"></blockquote>

现在,在 HTMLPurifier 中,您可以允许属性/某些 HTML,例如:

$config->set('HTML.Allowed', 'blockquote[style],a[href]');

Style 和 href 当然是允许的属性。虽然,允许他们使用样式属性可能会导致一些问题。那么有什么方法可以限制它只允许 style 属性,如果它设置为

margin: 0 0 0 40px; border: none; padding: 0px;

编辑

这是一个很好的答案:https://stackoverflow.com/a/6231024/2574433

但是,您能否进一步限制它以支持类似这样的内容:

$config->set('CSS.AllowedProperties', 'margin: 0 0 0 40px;');

【问题讨论】:

    标签: php htmlpurifier


    【解决方案1】:

    K 所以我不得不用这个完全进入野兽模式,并将随机的文档拼凑在一起。这是一个示例,如果您想在 CSS.AllowedProperties 区域中启用弹性框 CSS(甚至不必配置它)。

    $config = HTMLPurifier_Config::createDefault();
    $config->set('CSS.AllowImportant', true);
    $config->set('CSS.AllowTricky', true);
    $config->set('CSS.Proprietary', true);
    $config->set('CSS.Trusted', true);
    
    $css_definition = $config->getDefinition('CSS');
    
    // redefine this to add the flex attribute
    $css_definition->info['display'] = new HTMLPurifier_AttrDef_Enum(
        [
            'inline',
            'block',
            'list-item',
            'run-in',
            'compact',
            'marker',
            'table',
            'inline-block',
            'inline-table',
            'table-row-group',
            'table-header-group',
            'table-footer-group',
            'table-row',
            'table-column-group',
            'table-column',
            'table-cell',
            'table-caption',
            'none',
            'flex'
        ]
    );
    $css_definition->info['flex-direction'] = new HTMLPurifier_AttrDef_Enum(
        [
            'column',
            'column-reverse',
            'row',
            'row-reverse'
        ]
    );
    $css_definition->info['flex-wrap'] = new HTMLPurifier_AttrDef_Enum(
        [
            'wrap',
            'nowrap',
            'wrap-reverse'
        ]
    );
    $css_definition->info['justify-content'] = new HTMLPurifier_AttrDef_Enum(
        [
            'center',
            'flex-start',
            'flex-end',
            'space-around',
            'space-between'
        ]
    );
    $css_definition->info['align-items'] = new HTMLPurifier_AttrDef_Enum(
        [
            'center',
            'flex-start',
            'flex-end',
            'stretch',
            'baseline'
        ]
    );
    $css_definition->info['align-content'] = new HTMLPurifier_AttrDef_Enum(
        [
            'space-between',
            'space-around',
            'stretch',
            'center',
            'flex-start',
            'flex-end'
        ]
    );
    $css_definition->info['flex-basis'] = new HTMLPurifier_AttrDef_CSS_Percentage();
    
    $purifier = new HTMLPurifier($config);
    
    

    【讨论】:

      【解决方案2】:

      不,如果不自己修补库,这种特异性是不可能的。

      如果您可以控制输入 HTML,您可以做的一件事是禁用内联样式,并将这些“预设”样式作为可用于代码的类。

      如果您想修补 HTML Purifier 以获得更高的特异性,您需要查看 AttrDef 类,这些类指定了如何验证诸如边距之类的属性;您可以在 HTMLPurifier_CSSDefinition 中看到对应关系。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 2014-09-10
        • 2011-05-26
        相关资源
        最近更新 更多