【问题标题】:Cleaning up HTML using TinyMCE 4 or HTMLPurifier使用 TinyMCE 4 或 HTMLPurifier 清理 HTML
【发布时间】:2016-03-29 10:29:49
【问题描述】:

我有一个带有描述字段的表单,它使用 TinyMCE 4 来编辑文本和图像。

以下是我对 TinyMCE 的配置:

tinymce.init({
    selector: '.tinymce',
    formats: {
        bold: [
            {inline: 'span', styles: {fontWeight: 'bold'}}
        ],
        italic: [
            {inline: 'span', styles: {fontStyle: 'italic'}}
        ],
        underline: [
            {inline: 'span', styles: {textDecoration: 'underline'}, exact: true}
        ],
        strikethrough: [
            {inline: 'span', styles: {textDecoration: 'line-through'}, exact: true}
        ]
    },
    width: '80%',
    height: 200,
    menubar: false,
    statusbar: false,
    plugins: [
        'advlist autolink save link image lists hr',
        'wordcount visualblocks visualchars code media',
        'table contextmenu directionality textcolor colorpicker'
    ],
    toolbar1: 
        'styleselect | bold italic underline subscript superscript strikethrough removeformat | forecolor backcolor | ' + 
        'fontselect | bullist numlist | alignleft aligncenter alignright alignjustify | table | ' + 
        'link unlink image hr | code',
    toolbar_items_size: 'small',
    style_formats: [
        { title: 'Header 1', block: 'h1' }, { title: 'Header 2', block: 'h2' }, { title: 'Header 3', block: 'h3' },
        { title: 'Header 4', block: 'h4' }, { title: 'Header 5', block: 'h5' }, { title: 'Header 6', block: 'h6' }
    ],
    allow_conditional_comments: false,
    valid_elements: 'a,div,h1,h2,h3,h4,h5,h6,hr,li,ol,p,span[style],sub,sup,table[*],tr[*],td[*],ul,-p',
    extended_valid_elements : 'a[href|target=_blank],img[src|alt|width|height]',
    content_css: [],
    setup: function (editor) {
        // update selector's value when changes are made
        editor.on('change', editor.save);
    }
});

提交表单后,描述字段会使用 HTMLPurifier 进行清理。

以下是我对 HTMLPurifier 的配置:

$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8');
$config->set('HTML.ForbiddenElements', array('applet','embed','iframe','link','script','style','object'));
$config->set('AutoFormat.RemoveEmpty', true);
$config->set('Core.RemoveInvalidImg', true);
$config->set('URI.AllowedSchemes', array('data' => true)); // allow data URIs
$purifier = new HTMLPurifier($config);

在描述中输入数据时,可能会有嵌套的 span 标签。例如:

<h1><span style="text-decoration: underline; color: #ff6600;"><span style="font-weight: bold; font-style: italic;">sddfdsdfdhjhjkhjkh</span></span></h1>

问题: 有没有办法清理 HTML(使用 TinyMCE 或 HTMLPurifier),例如样式是否尽可能折叠?

<h1><span style="text-decoration: underline; color: #ff6600; font-weight: bold; font-style: italic;">sddfdsdfdhjhjkhjkh</span></h1>

或者更好:

<h1 style="text-decoration: underline; color: #ff6600; font-weight: bold; font-style: italic;">sddfdsdfdhjhjkhjkh</h1>

【问题讨论】:

  • 明天我会看看你的问题,可能会对你有所帮助
  • 我已经回答了,你也得到了我的投票

标签: javascript php tinymce-4 htmlpurifier


【解决方案1】:

当您得到另一个答案时,不可能为此使用 HTML Purifier。

但是仍然可以创建一个辅助函数来做你想做的事情。

通过使用preg_replaceregex,我们可以创建以下函数来删除跨度并获得您要求的输出:

function filterSpan($content)
{
    return preg_replace('/(><span)|(<\/span>)/', '', $content);
}

这是您未过滤的输入示例:

$content = '
<h1><span style="text-decoration: underline; color: #ff6600; 
font-weight: bold; font-style: italic;">sddfdsdfdhjhjkhjkh</span></h1>
';

这是调用filterSpan($content)后的输出:

<h1 style="text-decoration: underline; color: #ff6600; 
font-weight: bold; font-style: italic;">sddfdsdfdhjhjkhjkh</h1>

【讨论】:

  • 嗯.. 这是一个非常简单的解决方案。尽管如果跨度具有其他属性,这可能会创建无效标记。幸运的是,我没有。谢谢!
【解决方案2】:

HTML Purifier 没有此功能,抱歉!我什至不确定如何实施。有很多嵌套跨度的组合与单独的样式不能以这种方式折叠。

【讨论】:

  • 这是答案还是评论?
  • 这是一个答案。您无法使用 HTML Purifier 做到这一点。也许你可以自己实现它。
  • 但我不是OP,只是关注这个问题
  • 别抱歉,你写了这个很棒的库,希望有一天它会拥有这种能力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2015-05-13
  • 1970-01-01
  • 2013-07-19
  • 2017-10-10
相关资源
最近更新 更多