gpt4 book ai didi

javascript - 使用 TinyMCE 4 或 HTMLPurifier 清理 HTML

转载 作者:行者123 更新时间:2023-12-02 14:51:16 27 4
gpt4 key购买 nike

我有一个带有描述字段的表单,它使用 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);

当在描述中输入数据时,可以有嵌套的跨度标签。例如:

<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>

最佳答案

正如您得到的另一个答案,不可能使用 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>

关于javascript - 使用 TinyMCE 4 或 HTMLPurifier 清理 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36167704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com